From 0775179987987dd70ae1e952c1360ebf70c1adc0 Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Fri, 7 Aug 2020 18:09:03 -0700 Subject: [PATCH] Update to urfave/cli v2 (#148) --- go.mod | 2 +- go.sum | 4 +-- main.go | 74 ++++++++++++++++++++++++++++------------------------ main_test.go | 4 +-- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index a67ca27..fb1d76f 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,6 @@ require ( github.com/lib/pq v1.5.2 github.com/mattn/go-sqlite3 v1.13.0 github.com/stretchr/testify v1.4.0 - github.com/urfave/cli v1.22.4 + github.com/urfave/cli/v2 v2.2.0 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index 19df7c5..d5bcc0c 100644 --- a/go.sum +++ b/go.sum @@ -42,8 +42,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA= -github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 99909f7..39f4357 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "regexp" "github.com/joho/godotenv" - "github.com/urfave/cli" + "github.com/urfave/cli/v2" "github.com/amacneil/dbmate/pkg/dbmate" ) @@ -34,37 +34,40 @@ func NewApp() *cli.App { app.Version = dbmate.Version app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "env, e", - Value: "DATABASE_URL", - Usage: "specify an environment variable containing the database URL", + &cli.StringFlag{ + Name: "env", + Aliases: []string{"e"}, + Value: "DATABASE_URL", + Usage: "specify an environment variable containing the database URL", }, - cli.StringFlag{ - Name: "migrations-dir, d", - Value: dbmate.DefaultMigrationsDir, - Usage: "specify the directory containing migration files", + &cli.StringFlag{ + Name: "migrations-dir", + Aliases: []string{"d"}, + Value: dbmate.DefaultMigrationsDir, + Usage: "specify the directory containing migration files", }, - cli.StringFlag{ - Name: "schema-file, s", - Value: dbmate.DefaultSchemaFile, - Usage: "specify the schema file location", + &cli.StringFlag{ + Name: "schema-file", + Aliases: []string{"s"}, + Value: dbmate.DefaultSchemaFile, + Usage: "specify the schema file location", }, - cli.BoolFlag{ + &cli.BoolFlag{ Name: "no-dump-schema", Usage: "don't update the schema file on migrate/rollback", }, - cli.BoolFlag{ + &cli.BoolFlag{ Name: "wait", Usage: "wait for the db to become available before executing the subsequent command", }, - cli.DurationFlag{ + &cli.DurationFlag{ Name: "wait-timeout", Usage: "timeout for --wait flag", Value: dbmate.DefaultWaitTimeout, }, } - app.Commands = []cli.Command{ + app.Commands = []*cli.Command{ { Name: "new", Aliases: []string{"n"}, @@ -78,9 +81,10 @@ func NewApp() *cli.App { Name: "up", Usage: "Create database (if necessary) and migrate to the latest version", Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "verbose, v", - Usage: "print the result of each statement execution", + &cli.BoolFlag{ + Name: "verbose", + Aliases: []string{"v"}, + Usage: "print the result of each statement execution", }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { @@ -106,9 +110,10 @@ func NewApp() *cli.App { Name: "migrate", Usage: "Migrate to the latest version", Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "verbose, v", - Usage: "print the result of each statement execution", + &cli.BoolFlag{ + Name: "verbose", + Aliases: []string{"v"}, + Usage: "print the result of each statement execution", }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { @@ -121,9 +126,10 @@ func NewApp() *cli.App { Aliases: []string{"down"}, Usage: "Rollback the most recent migration", Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "verbose, v", - Usage: "print the result of each statement execution", + &cli.BoolFlag{ + Name: "verbose", + Aliases: []string{"v"}, + Usage: "print the result of each statement execution", }, }, Action: action(func(db *dbmate.DB, c *cli.Context) error { @@ -135,11 +141,11 @@ func NewApp() *cli.App { Name: "status", Usage: "List applied and pending migrations", Flags: []cli.Flag{ - cli.BoolFlag{ + &cli.BoolFlag{ Name: "exit-code", Usage: "return 1 if there are pending migrations", }, - cli.BoolFlag{ + &cli.BoolFlag{ Name: "quiet", Usage: "don't output any text (implies --exit-code)", }, @@ -201,11 +207,11 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { return err } db := dbmate.New(u) - db.AutoDumpSchema = !c.GlobalBool("no-dump-schema") - db.MigrationsDir = c.GlobalString("migrations-dir") - db.SchemaFile = c.GlobalString("schema-file") - db.WaitBefore = c.GlobalBool("wait") - overrideTimeout := c.GlobalDuration("wait-timeout") + db.AutoDumpSchema = !c.Bool("no-dump-schema") + db.MigrationsDir = c.String("migrations-dir") + db.SchemaFile = c.String("schema-file") + db.WaitBefore = c.Bool("wait") + overrideTimeout := c.Duration("wait-timeout") if overrideTimeout != 0 { db.WaitTimeout = overrideTimeout } @@ -216,7 +222,7 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { // getDatabaseURL returns the current environment database url func getDatabaseURL(c *cli.Context) (u *url.URL, err error) { - env := c.GlobalString("env") + env := c.String("env") value := os.Getenv(env) return url.Parse(value) diff --git a/main_test.go b/main_test.go index d6922db..2aa3ab3 100644 --- a/main_test.go +++ b/main_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/urfave/cli" + "github.com/urfave/cli/v2" ) func testContext(t *testing.T, u *url.URL) *cli.Context { @@ -17,7 +17,7 @@ func testContext(t *testing.T, u *url.URL) *cli.Context { app := NewApp() flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError) for _, f := range app.Flags { - f.Apply(flagset) + _ = f.Apply(flagset) } return cli.NewContext(app, flagset, nil)