github.com/naoina/kocha@v0.7.1-0.20171129072645-78c7a531f799/cmd/kocha-build/skeleton/build/main.go.tmpl (about) 1 // AUTO-GENERATED BY kocha build 2 // DO NOT EDIT THIS FILE 3 package main 4 5 import ( 6 "flag" 7 "fmt" 8 config "{{.configImportPath}}" 9 {{if and .dbImportPath .migrationImportPath}} 10 db "{{.dbImportPath}}" 11 migration "{{.migrationImportPath}}" 12 {{end}} 13 "os" 14 "path/filepath" 15 16 "github.com/naoina/kocha" 17 {{if .resources}} 18 "github.com/naoina/kocha/util" 19 {{end}} 20 ) 21 22 const Version = "{{.version}}" 23 24 func main() { 25 progName := filepath.Base(os.Args[0]) 26 showVersion := flag.Bool("v", false, "show version") 27 flag.Usage = func() { 28 fmt.Fprintf(os.Stderr, "usage: %s [-v] [migrate [-db confname] [-n n] {up|down}]\n", progName) 29 os.Exit(1) 30 } 31 flag.Parse() 32 if *showVersion { 33 fmt.Printf("%s version %s\n", progName, Version) 34 os.Exit(0) 35 } 36 migrate() 37 config.AppConfig.ResourceSet = {{.app.ResourceSet|goString}} 38 if len(config.AppConfig.RouteTable) != {{.app.Config.RouteTable|len}} { 39 fmt.Fprintf(os.Stderr, "abort: length of config.AppConfig.RouteTable is mismatched between build-time and run-time") 40 os.Exit(1) 41 } 42 {{range $name, $data := .resources}} 43 config.AppConfig.ResourceSet.Add("{{$name}}", util.Gunzip({{$data|printf "%q"}})) 44 {{end}} 45 if err := kocha.Run(config.AppConfig); err != nil { 46 panic(err) 47 } 48 } 49 50 func migrate() { 51 if flag.NArg() > 0 { 52 switch flag.Arg(0) { 53 case "migrate": 54 {{if and .dbImportPath .migrationImportPath}} 55 fs := flag.NewFlagSet("migrate", flag.ExitOnError) 56 dbconf := fs.String("db", "default", "name of a database config") 57 n := fs.Int("n", -1, "number of migrations to be run") 58 if err := fs.Parse(flag.Args()[1:]); err != nil { 59 panic(err) 60 } 61 config, found := db.DatabaseMap[*dbconf] 62 if !found { 63 fmt.Fprintf(os.Stderr, "abort: database config `%v' is undefined\n", *dbconf) 64 flag.Usage() 65 } 66 var err error 67 mig := kocha.Migrate(config, &migration.Migration{}) 68 switch fs.Arg(0) { 69 case "up": 70 err = mig.Up(*n) 71 case "down": 72 err = mig.Down(*n) 73 default: 74 flag.Usage() 75 } 76 if err != nil { 77 panic(err) 78 } 79 os.Exit(0) 80 {{else}} 81 fmt.Fprintf(os.Stderr, "abort: `migrate' is unsupported in this app binary\n") 82 os.Exit(1) 83 {{end}} 84 default: 85 flag.Usage() 86 } 87 } 88 }