/
add_slot_prefix.go
68 lines (58 loc) · 1.85 KB
/
add_slot_prefix.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
package command
import (
"fmt"
"strings"
"github.com/TimeIncOSS/ape-dev-rt/clippy"
"github.com/TimeIncOSS/ape-dev-rt/commons"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate/schema"
)
func AddSlotPrefix(c *commons.Context) error {
ds, ok := c.CliContext.App.Metadata["ds"].(*deploymentstate.DeploymentState)
if !ok {
return fmt.Errorf("Unable to find Deployment State in metadata")
}
if c.CliContext.NArg() < 1 {
return fmt.Errorf("You need to supply the name of slot prefix to create for %q / %q.",
c.String("app"), c.String("env"))
}
prefix := strings.TrimSpace(c.CliContext.Args().First())
appData, exists, err := BeginApplicationOperation(c.String("env"), c.String("app"), ds)
if err != nil {
return err
}
if !exists {
return nil
}
if appData.UseCentralGitRepo {
return fmt.Errorf("You cannot use slot prefixes for %q as it comes from the central git repo",
c.String("app"))
}
_, prefixExists, err := ds.GetSlotCounter(prefix, appData)
if err != nil {
return err
}
if prefixExists {
return fmt.Errorf("Slot prefix %q already exists for %q / %q,",
prefix, c.String("app"), c.String("env"))
}
note := fmt.Sprintf("It looks like you want to create a new prefix %q for %q in %q",
prefix, c.String("app"), c.String("env"))
isSensitive := isEnvironmentSensitive(c.String("env"))
out, confirmed, err := clippy.BoolPrompt(note, c.Bool("y"), isSensitive, func() (interface{}, error) {
return ds.AddSlotCounter(prefix, appData)
}, nil)
if err != nil {
return err
}
if confirmed {
appData = out.(*schema.ApplicationData)
err = FinishApplicationOperation(c.String("app"), appData, appData.IsActive, nil, ds)
if err != nil {
return err
}
fmt.Printf(colour.boldGreen("Slot prefix %q created for %q / %q.\n"),
prefix, c.String("app"), c.String("env"))
}
return nil
}