/
update.go
145 lines (121 loc) · 3.67 KB
/
update.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package commands
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/cuvva/cuvva-public-go/lib/cher"
"github.com/cuvva/cuvva-public-go/lib/config"
"github.com/cuvva/cuvva-public-go/lib/ptr"
"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/pkg/errors"
"github.com/spf13/cobra"
)
func init() {
UpdateCmd.Flags().StringP("branch", "b", cdep.DefaultBranch, "Branch to deploy")
UpdateCmd.Flags().BoolP("prod", "", false, "Work on prod")
UpdateCmd.Flags().BoolP("dry-run", "", false, "Dry run only?")
UpdateCmd.Flags().StringSliceP("overrule-checks", "", []string{}, "Overrule checks the tool does")
UpdateCmd.Flags().StringP("message", "m", "", "More details about the deployment")
UpdateCmd.Flags().MarkHidden("overrule-checks")
var envs string
types := strings.Join(cdep.ListTypes(), ", ")
for _, env := range cdep.ListEnvironments("*") {
envs = fmt.Sprintf("%s - %s\n", envs, env)
}
UpdateCmd.SetHelpTemplate(fmt.Sprintf(helpTemplateUpdate, types, envs))
}
// UpdateCmd is the initiator for the update command
var UpdateCmd = &cobra.Command{
Use: "update [type] [env] [services ...]",
Short: "Update the branch and commit for a selection of services, lambdas, or cloudfront distributions",
Long: "Please read the README.md file",
Example: strings.Join([]string{
"update service avocado sms email -b extra-logging",
"update lambda basil ltm-proxy",
"update service prod email --prod",
"update cloudfront prod website --prod",
}, "\n"),
Aliases: []string{"u"},
Args: updateArgs,
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
}
message, err := cmd.Flags().GetString("message")
if err != nil {
return err
}
params, err := parsers.Parse(args, branch, useProd, message)
if err != nil {
return err
}
awsSession, err := session.NewSessionWithOptions(session.Options{
Profile: "root",
Config: aws.Config{
Region: ptr.String("eu-west-1"),
Credentials: config.AWS{}.Credentials(),
},
})
if err != nil {
return errors.Wrap(err, "aws:")
}
sns := sns.New(awsSession)
a := &app.App{
DryRun: dryRun,
SNS: sns,
}
overruleChecks, err := cmd.Flags().GetStringSlice("overrule-checks")
if err != nil {
return err
}
return a.Update(ctx, params, overruleChecks)
},
}
func updateArgs(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.ParseTypeArg(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.ListTypes()})
case len(args) == 1:
return cher.New("missing_environment", cher.M{"allowed": cdep.ListEnvironments(system)})
case len(args) == 2:
return cher.New("missing_services", nil)
case len(args) >= 3:
return nil
default:
return cher.New("unknown_arguments", nil)
}
}
const helpTemplateUpdate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}
Allowed types: %s
Allowed environments:
%s`