-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
97 lines (80 loc) · 3.51 KB
/
Copy pathcli.go
File metadata and controls
97 lines (80 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package imigrate
import (
"errors"
"flag"
"os"
)
// HelpText is printed when no command is specified.
const HelpText = "Please specify up, down, redo, rollback, status, dry-run, or create."
// CLIErr is returned when no command is specified.
var CLIErr error = errors.New(HelpText)
// CLI parses os.Args[1:] and runs the appropriate migration command.
// It is a convenience wrapper around CLIWithArgs.
func CLI(migrator Migrator) error {
return CLIWithArgs(migrator, os.Args[1:])
}
// CLIWithArgs parses the provided args and runs the appropriate migration command.
// Commands available are up, down, redo, rollback, status, dry-run, and create.
// Most commands accept a "steps" flag which is parsed as an int. Use -steps=1
// to set it. Up, down, and redo accept a "version" flag which is parsed as
// int64. Use --version=1610069160 to set it.
func CLIWithArgs(migrator Migrator, args []string) error {
type runner struct {
cmd *flag.FlagSet
silent *bool
run func() error
}
upCmd := flag.NewFlagSet("up", flag.ContinueOnError)
upSteps := upCmd.Int("steps", -1, "how many migrations to execute forward")
upVersion := upCmd.Int64("version", 0, "which version to migrate")
upSilent := upCmd.Bool("silent", false, "Do not print messages")
dnCmd := flag.NewFlagSet("down", flag.ContinueOnError)
dnSteps := dnCmd.Int("steps", -1, "how many migrations to execute backward")
dnVersion := dnCmd.Int64("version", 0, "which version to migrate")
dnSilent := dnCmd.Bool("silent", false, "Do not print messages")
redoCmd := flag.NewFlagSet("redo", flag.ContinueOnError)
redoSteps := redoCmd.Int("steps", 1, "how many migrations to redo")
redoVersion := redoCmd.Int64("version", 0, "which version to migrate")
redoSilent := redoCmd.Bool("silent", false, "Do not print messages")
rollbackCmd := flag.NewFlagSet("rollback", flag.ContinueOnError)
rollbackSteps := rollbackCmd.Int("steps", 1, "how many migrations to rollback")
rollbackSilent := rollbackCmd.Bool("silent", false, "Do not print messages")
statusCmd := flag.NewFlagSet("status", flag.ContinueOnError)
statusSilent := statusCmd.Bool("silent", false, "Do not print messages")
createCmd := flag.NewFlagSet("create", flag.ContinueOnError)
createSilent := createCmd.Bool("silent", false, "Do not print messages")
dryRunCmd := flag.NewFlagSet("dry-run", flag.ContinueOnError)
dryRunSteps := dryRunCmd.Int("steps", -1, "how many migrations to preview")
dryRunVersion := dryRunCmd.Int64("version", 0, "which version to preview")
dryRunSilent := dryRunCmd.Bool("silent", false, "Do not print messages")
runners := []runner{
{upCmd, upSilent, func() error { return migrator.Up(*upSteps, *upVersion) }},
{dnCmd, dnSilent, func() error { return migrator.Down(*dnSteps, *dnVersion) }},
{redoCmd, redoSilent, func() error { return migrator.Redo(*redoSteps, *redoVersion) }},
{rollbackCmd, rollbackSilent, func() error { return migrator.Rollback(*rollbackSteps) }},
{statusCmd, statusSilent, func() error { return migrator.Status() }},
{createCmd, createSilent, func() error { return migrator.Create(createCmd.Arg(0)) }},
{dryRunCmd, dryRunSilent, func() error { return migrator.DryRun(*dryRunSteps, *dryRunVersion) }},
}
if len(args) < 1 {
return CLIErr
}
for _, r := range runners {
if args[0] == r.cmd.Name() {
err := r.cmd.Parse(args[1:])
if err == flag.ErrHelp {
return nil
}
if err != nil {
return err
}
if *r.silent {
if m, ok := migrator.(*IMigrator); ok {
m.Logger = DiscardLogger
}
}
return r.run()
}
}
return CLIErr
}