Skip to content

Commit

Permalink
Add link subcommand. Fixes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Aug 12, 2016
1 parent 6439891 commit cb4addc
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cmd/puma-dev/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/vektra/errors"
)

func command() error {
switch flag.Arg(0) {
case "link":
return link()
default:
return fmt.Errorf("Unknown command: %s\n", flag.Arg(0))
}
}

func link() error {
fs := flag.NewFlagSet("link", flag.ExitOnError)
name := fs.String("n", "", "name to link app as")

err := fs.Parse(flag.Args()[1:])
if err != nil {
return err
}

var dir string

if fs.NArg() == 0 {
dir, err = os.Getwd()
if err != nil {
return err
}

} else {
dir, err = filepath.Abs(fs.Arg(0))
if err != nil {
return err
}

stat, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("Invalid directory: %s", dir)
}

return err
}

if !stat.IsDir() {
return fmt.Errorf("Invalid directory: %s", dir)
}
}

if *name == "" {
*name = strings.Replace(filepath.Base(dir), ".", "_", -1)
}

dest, err := homedir.Expand(filepath.Join(*fDir, *name))
if err != nil {
return err
}

_, err = os.Stat(dest)
if err == nil {
dest, err := os.Readlink(dest)
if err == nil {
fmt.Printf("! App '%s' already exists, pointed at '%s'\n", *name, dest)
} else {
fmt.Printf("! App '%s' already exists'\n", *name)
}
return nil
}

err = os.Symlink(dir, dest)
if err != nil {
return errors.Context(err, "creating symlink")
}

fmt.Printf("+ App '%s' created, linked to '%s'\n", *name, dest)

return nil
}
16 changes: 16 additions & 0 deletions cmd/puma-dev/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"flag"
"fmt"
"os"
)

func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()

fmt.Fprintf(os.Stderr, "\nAvailable subcommands: link\n")
}
}
10 changes: 10 additions & 0 deletions cmd/puma-dev/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var (
func main() {
flag.Parse()

if flag.NArg() > 0 {
err := command()
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

return
}

if *fPow {
*fDomains = "dev"
*fDir = "~/.pow"
Expand Down
10 changes: 10 additions & 0 deletions cmd/puma-dev/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ var (
func main() {
flag.Parse()

if flag.NArg() > 0 {
err := command()
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}

return
}

domains := strings.Split(*fDomains, ":")

if *fSysBind {
Expand Down

0 comments on commit cb4addc

Please sign in to comment.