github.com/comcast/canticle@v0.0.0-20161108184242-c53cface56e8/canticles/command.go (about) 1 package canticles 2 3 import ( 4 "flag" 5 "fmt" 6 "log" 7 "os" 8 ) 9 10 // Runnable is used by Command to call an item with the arguments 11 // pertinent to it. 12 type Runnable interface { 13 Run(args []string) 14 } 15 16 // Command represents a Canticle command to be run including: 17 // * Save 18 // * Build 19 // * Test 20 // * Update 21 type Command struct { 22 Name string 23 UsageLine string 24 ShortDescription string 25 LongDescription string 26 Flags *flag.FlagSet 27 Cmd Runnable 28 } 29 30 // Commands is the prebuild list of Canticle commands. 31 var Commands = map[string]*Command{ 32 "get": GetCommand, 33 "save": SaveCommand, 34 "vendor": VendorCommand, 35 "genversion": GenVersionCommand, 36 } 37 38 // Usage will print the commands UsageLine and LongDescription and 39 // then os.Exit(2). 40 func (c *Command) Usage() { 41 fmt.Fprintf(os.Stderr, "usage %s\n", c.UsageLine) 42 fmt.Fprintf(os.Stderr, "%s\n", c.LongDescription) 43 os.Exit(2) 44 } 45 46 // GetCurrentPackage returns the "package name" of the current working 47 // directory if you the cwd is in the goroot. 48 func GetCurrentPackage() (string, error) { 49 cwd, err := os.Getwd() 50 if err != nil { 51 return "", err 52 } 53 gopath, err := EnvGoPath() 54 if err != nil { 55 return "", err 56 } 57 58 return PackageName(gopath, cwd) 59 } 60 61 // ParseCmdLineDeps parses an array of packages or grabs the current 62 // package if none are present. 63 func ParseCmdLinePackages(args []string) []string { 64 if len(args) == 0 { 65 pkg, err := os.Getwd() 66 if err != nil { 67 log.Fatalf("cant get current package: %s", err.Error()) 68 } 69 return []string{pkg} 70 } 71 return args 72 }