Add --exit-code and --quiet flags to status command (#124)

Extending the `dbmate status` command with the ability to set an exit code or quiet output, for use in scripts.

Flag names were copied from `git diff` command.
This commit is contained in:
Adrian Macneil 2020-03-15 11:32:48 -07:00 committed by GitHub
parent 256f92ad19
commit 9d2ec369d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

30
main.go
View file

@ -17,9 +17,10 @@ func main() {
app := NewApp()
err := app.Run(os.Args)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
os.Exit(2)
}
}
@ -105,8 +106,33 @@ func NewApp() *cli.App {
{
Name: "status",
Usage: "List applied and pending migrations",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "exit-code",
Usage: "return 1 if there are pending migrations",
},
cli.BoolFlag{
Name: "quiet",
Usage: "don't output any text (implies --exit-code)",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
return db.Status()
setExitCode := c.Bool("exit-code")
quiet := c.Bool("quiet")
if quiet {
setExitCode = true
}
pending, err := db.Status(quiet)
if err != nil {
return err
}
if pending > 0 && setExitCode {
return cli.NewExitError("", 1)
}
return nil
}),
},
{