dbmate/main.go

90 lines
1.6 KiB
Go
Raw Normal View History

2015-11-25 10:57:58 -08:00
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
loadDotEnv()
app := NewApp()
2016-05-21 22:33:42 -07:00
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
// NewApp creates a new command line app
func NewApp() *cli.App {
2015-11-25 10:57:58 -08:00
app := cli.NewApp()
app.Name = "dbmate"
app.Usage = "A lightweight, framework-independent database migration tool."
2015-12-01 18:17:38 -08:00
app.Version = Version
2015-11-25 10:57:58 -08:00
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "migrations-dir, d",
Value: "./db/migrations",
Usage: "specify the directory containing migration files",
},
cli.StringFlag{
Name: "env, e",
Value: "DATABASE_URL",
Usage: "specify an environment variable containing the database URL",
},
2015-11-25 10:57:58 -08:00
}
app.Commands = []cli.Command{
{
Name: "new",
Usage: "Generate a new migration file",
Action: NewCommand,
2015-11-25 12:26:57 -08:00
},
2015-11-25 10:57:58 -08:00
{
Name: "up",
Usage: "Create database (if necessary) and migrate to the latest version",
Action: UpCommand,
2015-11-25 10:57:58 -08:00
},
{
Name: "create",
Usage: "Create database",
Action: CreateCommand,
2015-11-25 10:57:58 -08:00
},
{
Name: "drop",
Usage: "Drop database (if it exists)",
Action: DropCommand,
2015-11-25 10:57:58 -08:00
},
{
Name: "migrate",
Usage: "Migrate to the latest version",
Action: MigrateCommand,
},
{
2016-07-27 08:44:26 -07:00
Name: "rollback",
Aliases: []string{"down"},
Usage: "Rollback the most recent migration",
Action: RollbackCommand,
},
2015-11-25 10:57:58 -08:00
}
return app
2015-11-25 10:57:58 -08:00
}
type command func(*cli.Context) error
func loadDotEnv() {
if _, err := os.Stat(".env"); err != nil {
return
}
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
}