-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters