Update CLI action signatures. Closes #1

This commit is contained in:
Adrian Macneil 2016-08-07 22:40:59 -07:00
parent 252cd4bdf1
commit c0f0a5db8b

65
main.go
View file

@ -13,7 +13,10 @@ func main() {
app := NewApp() app := NewApp()
err := app.Run(os.Args) err := app.Run(os.Args)
checkErr(err) if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
} }
// NewApp creates a new command line app // NewApp creates a new command line app
@ -38,46 +41,34 @@ func NewApp() *cli.App {
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "new", Name: "new",
Usage: "Generate a new migration file", Usage: "Generate a new migration file",
Action: func(ctx *cli.Context) { Action: NewCommand,
runCommand(NewCommand, ctx)
},
}, },
{ {
Name: "up", Name: "up",
Usage: "Create database (if necessary) and migrate to the latest version", Usage: "Create database (if necessary) and migrate to the latest version",
Action: func(ctx *cli.Context) { Action: UpCommand,
runCommand(UpCommand, ctx)
},
}, },
{ {
Name: "create", Name: "create",
Usage: "Create database", Usage: "Create database",
Action: func(ctx *cli.Context) { Action: CreateCommand,
runCommand(CreateCommand, ctx)
},
}, },
{ {
Name: "drop", Name: "drop",
Usage: "Drop database (if it exists)", Usage: "Drop database (if it exists)",
Action: func(ctx *cli.Context) { Action: DropCommand,
runCommand(DropCommand, ctx)
},
}, },
{ {
Name: "migrate", Name: "migrate",
Usage: "Migrate to the latest version", Usage: "Migrate to the latest version",
Action: func(ctx *cli.Context) { Action: MigrateCommand,
runCommand(MigrateCommand, ctx)
},
}, },
{ {
Name: "rollback", Name: "rollback",
Usage: "Rollback the most recent migration", Usage: "Rollback the most recent migration",
Action: func(ctx *cli.Context) { Action: RollbackCommand,
runCommand(RollbackCommand, ctx)
},
}, },
} }
@ -86,11 +77,6 @@ func NewApp() *cli.App {
type command func(*cli.Context) error type command func(*cli.Context) error
func runCommand(cmd command, ctx *cli.Context) {
err := cmd(ctx)
checkErr(err)
}
func loadDotEnv() { func loadDotEnv() {
if _, err := os.Stat(".env"); err != nil { if _, err := os.Stat(".env"); err != nil {
return return
@ -100,10 +86,3 @@ func loadDotEnv() {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
} }
} }
func checkErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}