-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcommands_force.go
77 lines (71 loc) · 1.85 KB
/
commands_force.go
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
package main
import (
"fmt"
"path/filepath"
"github.com/urfave/cli"
)
func cmdMigrationsSetState(c *cli.Context) error {
mtx, err := getMigrationContext(c)
if err != nil {
printWarning(err.Error())
return nil
}
filename := c.Args().Get(1) // 0=path, 1=relative filename
if !c.GlobalBool("q") { // be quiet
if !promptForYes(fmt.Sprintf("Are you sure to overwrite the current last applied migration [%s -> %s] (y/N)? ", mtx.lastApplied, filename)) {
return errAbort
}
}
if err := checkExists(filepath.Join(mtx.migrationsPath, filename)); err != nil {
printError(err.Error())
return errAbort
}
if err := mtx.stateProvider.SaveState(filename); err != nil {
printError(err.Error())
return errAbort
}
return nil
}
func cmdRundoOnly(c *cli.Context) error {
return runSectionOnly(c, true)
}
func cmdRunUndoOnly(c *cli.Context) error {
return runSectionOnly(c, false)
}
func runSectionOnly(c *cli.Context, isDo bool) error {
mtx, err := getMigrationContext(c)
if err != nil {
printWarning(err.Error())
return nil
}
section := "do"
if !isDo {
section = "undo"
}
filename := c.Args().Get(1) // 0=path, 1=relative filename
if !c.GlobalBool("q") { // be quiet
if !promptForYes(fmt.Sprintf("Are you sure to run the [%s] section of migration [%s] for config [%s] (y/N)? ", section, filename, c.Args().Get(0))) {
return errAbort
}
}
full := filepath.Join(mtx.migrationsPath, filename)
if err := checkExists(full); err != nil {
printError(err.Error())
return errAbort
}
m, err := LoadMigration(full)
if err != nil {
printError(err.Error())
return errAbort
}
lines := m.DoSection
if !isDo {
lines = m.UndoSection
}
envs := mtx.shellEnv()
if err := ExecuteAll(m.IfExpression, lines, envs, c.GlobalBool("v")); err != nil {
reportError(mtx.stateProvider.Config(), envs, section, err)
return errAbort
}
return nil
}