-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_web.go
118 lines (97 loc) · 2.92 KB
/
update_web.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package commands
import (
"context"
"fmt"
"strings"
"github.com/cuvva/cuvva-public-go/lib/cher"
"github.com/cuvva/cuvva-public-go/tools/cdep"
"github.com/cuvva/cuvva-public-go/tools/cdep/app"
"github.com/cuvva/cuvva-public-go/tools/cdep/parsers"
"github.com/spf13/cobra"
)
func init() {
UpdateWebCmd.Flags().StringP("branch", "b", cdep.DefaultBranch, "Branch to deploy")
UpdateWebCmd.Flags().BoolP("prod", "", false, "Work on prod")
UpdateWebCmd.Flags().BoolP("dry-run", "", false, "Dry run only?")
UpdateWebCmd.Flags().StringSliceP("overrule-checks", "", []string{}, "Overrule checks the tool does")
UpdateWebCmd.Flags().MarkHidden("overrule-checks")
var envs string
types := strings.Join(cdep.ListWebTypes(), ", ")
for _, env := range cdep.ListEnvironments("*") {
envs = fmt.Sprintf("%s - %s\n", envs, env)
}
UpdateWebCmd.SetHelpTemplate(fmt.Sprintf(helpTemplateUpdateWeb, types, envs))
}
// UpdateWebCmd is the initiator for the update command
var UpdateWebCmd = &cobra.Command{
Use: "update-web [type] [env] [services ...]",
Short: "Update the deployment identifier for web applications ",
Long: "Please read the README.md file",
Example: strings.Join([]string{
"update-web cloudfront all website",
"uw cf prod website --prod",
"uw cf prod website -b new-landing-page --prod",
}, "\n"),
Aliases: []string{"uw"},
Args: updateWebArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
branch, err := cmd.Flags().GetString("branch")
if err != nil {
return err
}
useProd, err := cmd.Flags().GetBool("prod")
if err != nil {
return err
}
dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
return nil
}
params, err := parsers.ParseWeb(args, branch, useProd)
if err != nil {
return err
}
a := &app.App{
DryRun: dryRun,
}
overruleChecks, err := cmd.Flags().GetStringSlice("overrule-checks")
if err != nil {
return err
}
return a.UpdateWeb(ctx, params, overruleChecks)
},
}
func updateWebArgs(cmd *cobra.Command, args []string) error {
system := "nonprod"
if useProd, err := cmd.Flags().GetBool("prod"); err == nil && useProd {
system = "prod"
}
switch len(args) {
case 1:
if _, err := cdep.ParseWebTypeArg(args[0]); err != nil {
return err
}
case 2:
if err := cdep.ValidateSystemEnvironment(system, args[1]); err != nil {
return err
}
}
switch true {
case len(args) == 0:
return cher.New("missing_type", cher.M{"allowed": cdep.ListWebTypes()})
case len(args) == 1:
return cher.New("missing_environment", cher.M{"allowed": cdep.ListEnvironments(system)})
case len(args) == 2:
return cher.New("missing_app", nil)
case len(args) >= 3:
return nil
default:
return cher.New("unknown_arguments", nil)
}
}
const helpTemplateUpdateWeb = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}
Allowed types: %s
Allowed environments:
%s`