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

View file

@ -492,26 +492,33 @@ func checkMigrationsStatus(db *DB) ([]statusResult, error) {
}
// Status shows the status of all migrations
func (db *DB) Status() error {
func (db *DB) Status(quiet bool) (int, error) {
results, err := checkMigrationsStatus(db)
if err != nil {
return err
return -1, err
}
var totalApplied int
var line string
for _, res := range results {
if res.applied {
fmt.Println("[X]", res.filename)
line = fmt.Sprintf("[X] %s", res.filename)
totalApplied++
} else {
fmt.Println("[ ]", res.filename)
line = fmt.Sprintf("[ ] %s", res.filename)
}
if !quiet {
fmt.Println(line)
}
}
fmt.Println()
fmt.Printf("Applied: %d\n", totalApplied)
fmt.Printf("Pending: %d\n", len(results)-totalApplied)
totalPending := len(results) - totalApplied
if !quiet {
fmt.Println()
fmt.Printf("Applied: %d\n", totalApplied)
fmt.Printf("Pending: %d\n", totalPending)
}
return nil
return totalPending, nil
}