Update to urfave/cli v2 (#148)

This commit is contained in:
Adrian Macneil 2020-08-07 18:09:03 -07:00 committed by GitHub
parent 23aa907644
commit 0775179987
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 39 deletions

2
go.mod
View file

@ -13,6 +13,6 @@ require (
github.com/lib/pq v1.5.2 github.com/lib/pq v1.5.2
github.com/mattn/go-sqlite3 v1.13.0 github.com/mattn/go-sqlite3 v1.13.0
github.com/stretchr/testify v1.4.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 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
) )

4
go.sum
View file

@ -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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 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 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 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

56
main.go
View file

@ -8,7 +8,7 @@ import (
"regexp" "regexp"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/urfave/cli" "github.com/urfave/cli/v2"
"github.com/amacneil/dbmate/pkg/dbmate" "github.com/amacneil/dbmate/pkg/dbmate"
) )
@ -34,37 +34,40 @@ func NewApp() *cli.App {
app.Version = dbmate.Version app.Version = dbmate.Version
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{ &cli.StringFlag{
Name: "env, e", Name: "env",
Aliases: []string{"e"},
Value: "DATABASE_URL", Value: "DATABASE_URL",
Usage: "specify an environment variable containing the database URL", Usage: "specify an environment variable containing the database URL",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "migrations-dir, d", Name: "migrations-dir",
Aliases: []string{"d"},
Value: dbmate.DefaultMigrationsDir, Value: dbmate.DefaultMigrationsDir,
Usage: "specify the directory containing migration files", Usage: "specify the directory containing migration files",
}, },
cli.StringFlag{ &cli.StringFlag{
Name: "schema-file, s", Name: "schema-file",
Aliases: []string{"s"},
Value: dbmate.DefaultSchemaFile, Value: dbmate.DefaultSchemaFile,
Usage: "specify the schema file location", Usage: "specify the schema file location",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "no-dump-schema", Name: "no-dump-schema",
Usage: "don't update the schema file on migrate/rollback", Usage: "don't update the schema file on migrate/rollback",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "wait", Name: "wait",
Usage: "wait for the db to become available before executing the subsequent command", Usage: "wait for the db to become available before executing the subsequent command",
}, },
cli.DurationFlag{ &cli.DurationFlag{
Name: "wait-timeout", Name: "wait-timeout",
Usage: "timeout for --wait flag", Usage: "timeout for --wait flag",
Value: dbmate.DefaultWaitTimeout, Value: dbmate.DefaultWaitTimeout,
}, },
} }
app.Commands = []cli.Command{ app.Commands = []*cli.Command{
{ {
Name: "new", Name: "new",
Aliases: []string{"n"}, Aliases: []string{"n"},
@ -78,8 +81,9 @@ func NewApp() *cli.App {
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",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose, v", Name: "verbose",
Aliases: []string{"v"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },
@ -106,8 +110,9 @@ func NewApp() *cli.App {
Name: "migrate", Name: "migrate",
Usage: "Migrate to the latest version", Usage: "Migrate to the latest version",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose, v", Name: "verbose",
Aliases: []string{"v"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },
@ -121,8 +126,9 @@ func NewApp() *cli.App {
Aliases: []string{"down"}, Aliases: []string{"down"},
Usage: "Rollback the most recent migration", Usage: "Rollback the most recent migration",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose, v", Name: "verbose",
Aliases: []string{"v"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },
@ -135,11 +141,11 @@ func NewApp() *cli.App {
Name: "status", Name: "status",
Usage: "List applied and pending migrations", Usage: "List applied and pending migrations",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{ &cli.BoolFlag{
Name: "exit-code", Name: "exit-code",
Usage: "return 1 if there are pending migrations", Usage: "return 1 if there are pending migrations",
}, },
cli.BoolFlag{ &cli.BoolFlag{
Name: "quiet", Name: "quiet",
Usage: "don't output any text (implies --exit-code)", 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 return err
} }
db := dbmate.New(u) db := dbmate.New(u)
db.AutoDumpSchema = !c.GlobalBool("no-dump-schema") db.AutoDumpSchema = !c.Bool("no-dump-schema")
db.MigrationsDir = c.GlobalString("migrations-dir") db.MigrationsDir = c.String("migrations-dir")
db.SchemaFile = c.GlobalString("schema-file") db.SchemaFile = c.String("schema-file")
db.WaitBefore = c.GlobalBool("wait") db.WaitBefore = c.Bool("wait")
overrideTimeout := c.GlobalDuration("wait-timeout") overrideTimeout := c.Duration("wait-timeout")
if overrideTimeout != 0 { if overrideTimeout != 0 {
db.WaitTimeout = overrideTimeout 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 // getDatabaseURL returns the current environment database url
func getDatabaseURL(c *cli.Context) (u *url.URL, err error) { func getDatabaseURL(c *cli.Context) (u *url.URL, err error) {
env := c.GlobalString("env") env := c.String("env")
value := os.Getenv(env) value := os.Getenv(env)
return url.Parse(value) return url.Parse(value)

View file

@ -7,7 +7,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/urfave/cli" "github.com/urfave/cli/v2"
) )
func testContext(t *testing.T, u *url.URL) *cli.Context { 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() app := NewApp()
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError) flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError)
for _, f := range app.Flags { for _, f := range app.Flags {
f.Apply(flagset) _ = f.Apply(flagset)
} }
return cli.NewContext(app, flagset, nil) return cli.NewContext(app, flagset, nil)