github.com/naoina/kocha@v0.7.1-0.20171129072645-78c7a531f799/cmd/kocha-generate/kocha-generate-unit/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "path/filepath" 7 "runtime" 8 9 "github.com/naoina/kocha/util" 10 ) 11 12 type generateUnitCommand struct { 13 option struct { 14 Help bool `short:"h" long:"help"` 15 } 16 } 17 18 func (c *generateUnitCommand) Name() string { 19 return "kocha generate unit" 20 } 21 22 func (c *generateUnitCommand) Usage() string { 23 return fmt.Sprintf(`Usage: %s [OPTIONS] NAME 24 25 Generate the skeleton files of unit. 26 27 Options: 28 -h, --help display this help and exit 29 30 `, c.Name()) 31 } 32 33 func (c *generateUnitCommand) Option() interface{} { 34 return &c.option 35 } 36 37 // Run generates unit skeleton files. 38 func (c *generateUnitCommand) Run(args []string) error { 39 if len(args) < 1 || args[0] == "" { 40 return fmt.Errorf("no NAME given") 41 } 42 name := args[0] 43 camelCaseName := util.ToCamelCase(name) 44 snakeCaseName := util.ToSnakeCase(name) 45 data := map[string]interface{}{ 46 "Name": camelCaseName, 47 } 48 if err := util.CopyTemplate( 49 filepath.Join(skeletonDir("unit"), "unit.go"+util.TemplateSuffix), 50 filepath.Join("app", "unit", snakeCaseName+".go"), data); err != nil { 51 return err 52 } 53 return nil 54 } 55 56 func skeletonDir(name string) string { 57 _, filename, _, _ := runtime.Caller(0) 58 baseDir := filepath.Dir(filename) 59 return filepath.Join(baseDir, "skeleton", name) 60 } 61 62 func main() { 63 util.RunCommand(&generateUnitCommand{}) 64 }