github.com/naoina/kocha@v0.7.1-0.20171129072645-78c7a531f799/cmd/kocha-generate/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "strings" 9 10 "go/build" 11 12 "github.com/naoina/kocha/util" 13 ) 14 15 const ( 16 generatorPrefix = "kocha-generate-" 17 ) 18 19 type generateCommand struct { 20 option struct { 21 Help bool `short:"h" long:"help"` 22 } 23 } 24 25 func (c *generateCommand) Name() string { 26 return "kocha generate" 27 } 28 29 func (c *generateCommand) Usage() string { 30 return fmt.Sprintf(`Usage: %s [OPTIONS] GENERATOR [argument...] 31 32 Generate the skeleton files. 33 34 Generators: 35 controller 36 migration 37 model 38 unit 39 40 Options: 41 -h, --help display this help and exit 42 43 `, c.Name()) 44 } 45 46 func (c *generateCommand) Option() interface{} { 47 return &c.option 48 } 49 50 // Run execute the process for `generate` command. 51 func (c *generateCommand) Run(args []string) error { 52 if len(args) < 1 || args[0] == "" { 53 return fmt.Errorf("no GENERATOR given") 54 } 55 name := args[0] 56 var paths []string 57 for _, dir := range build.Default.SrcDirs() { 58 paths = append(paths, filepath.Clean(filepath.Join(filepath.Dir(dir), "bin"))) 59 } 60 paths = append(paths, filepath.SplitList(os.Getenv("PATH"))...) 61 if err := os.Setenv("PATH", strings.Join(paths, string(filepath.ListSeparator))); err != nil { 62 return err 63 } 64 filename, err := exec.LookPath(generatorPrefix + name) 65 if err != nil { 66 return fmt.Errorf("could not found generator: %s", name) 67 } 68 cmd := exec.Command(filename, args[1:]...) 69 cmd.Stdin = os.Stdin 70 cmd.Stdout = os.Stdout 71 cmd.Stderr = os.Stderr 72 return cmd.Run() 73 } 74 75 func main() { 76 util.RunCommand(&generateCommand{}) 77 }