/
root.go
320 lines (271 loc) · 8.43 KB
/
root.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package cmd
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/spirius/clon/pkg/clon"
"github.com/Masterminds/semver"
"github.com/fatih/color"
"github.com/google/go-github/github"
"github.com/juju/errors"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/yaml.v2"
)
// Revision is the revision number of build (commit Id).
var Revision = ""
// Version is the version of the curren build.
var Version = "v0.0.6"
var currentVersion = semver.MustParse(Version)
type errorCode struct {
err error
code int
}
func (e errorCode) Error() string {
if e.err != nil {
return e.err.Error()
}
return "nil"
}
var config clon.Config
var configFlags struct {
// Automatically approve changes
autoApprove bool
// Enables debug logging
debug bool
// Enabled tracing on errors
trace bool
// Indicates if user-input is available.
// Defaults to true, if stdin is terminal.
input bool
config string
configOverride string
ignoreNestedUpdates bool
verifyParentStacks bool
}
// use wrapped stdout and stderr, so that
// colors will work on windows properly.
var stdout = color.Output
var stderr = color.Error
var stackHandler *stackCmdHandler
func decodeConfig(config *clon.Config, r io.Reader) error {
var err error
config.IgnoreNestedUpdates = configFlags.ignoreNestedUpdates
m := make(map[string]interface{})
if err = yaml.NewDecoder(r).Decode(m); err != nil {
return errors.Annotatef(err, "syntax error")
}
if err = mapstructure.WeakDecode(m, &config); err != nil {
return errors.Annotatef(err, "cannot parse config")
}
return nil
}
var rootCmd = &cobra.Command{
Use: "clon",
Short: "clon is a CLoudFormatiON stack management tool",
SilenceErrors: true,
SilenceUsage: true,
DisableFlagsInUseLine: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if configFlags.debug {
log.SetLevel(log.DebugLevel)
}
githubClient := github.NewClient(nil)
ctx := context.Background()
release, _, err := githubClient.Repositories.GetLatestRelease(ctx, "spirius", "clon")
if err != nil {
log.Warnf("cannot verify latest release version: %s", err)
} else if release.TagName == nil {
log.Warnf("no release version information available")
} else if *release.TagName != Version {
relVer, err := semver.NewVersion(*release.TagName)
if err != nil {
log.Warnf("cannot parse release version: %s", err)
} else if currentVersion.Compare(relVer) < 0 {
log.Warnf(`newer version of clon (%s) is available, your version is %s, `+
`please update to the latest version: `+
`https://github.com/spirius/clon/releases/latest`,
*release.TagName, Version)
}
}
c, err := os.Open(configFlags.config)
if err != nil {
return errors.Annotatef(err, "cannot open config")
}
if err = decodeConfig(&config, c); err != nil {
return errors.Annotatef(err, "cannot parse config")
}
if configFlags.configOverride != "" {
c, err = os.Open(configFlags.configOverride)
if err != nil {
errors.Annotatef(err, "cannot open config")
}
if err = decodeConfig(&config, c); err != nil {
return errors.Annotatef(err, "cannot parse config")
}
}
if stackHandler, err = newStackCmdHandler(config); err != nil {
return errors.Annotatef(err, "cannot initialize clon")
}
return nil
},
}
func flagAutoApprove(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&configFlags.autoApprove, "auto-approve", "a", false, "Auto-approve changes")
}
func flagIgnoreNestedUpdates(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(
&configFlags.ignoreNestedUpdates,
"ignore-nested-updates",
"",
false,
"Do not consider stack changed, if only nested stack automatics updates are performed",
)
}
func flagVerifyParentStacks(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(
&configFlags.verifyParentStacks,
"verify-parent-stacks",
"",
true,
"Check, if parent stacks are up-to-date. Try to deploy if needed.",
)
}
func init() {
log.SetFormatter(&logFormatter{})
log.SetOutput(stderr)
// global flags
rootCmd.PersistentFlags().BoolVarP(&configFlags.debug, "debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().BoolVarP(&configFlags.trace, "trace", "t", false, "Enable error tracing output")
rootCmd.PersistentFlags().BoolVarP(&configFlags.input, "input", "i", terminal.IsTerminal(int(os.Stdin.Fd())), "User input availability. If not specified, value is identified from terminal.")
rootCmd.PersistentFlags().StringVarP(&configFlags.config, "config", "c", "clon.yml", "Config file")
rootCmd.PersistentFlags().StringVarP(&configFlags.configOverride, "config-override", "e", "", "Override config file")
// list
newCmd(rootCmd, &cobra.Command{
Use: "list",
Short: "List stacks",
Long: `List short status information of all stacks.`,
Args: exactArgs(0),
}, func(_ *cobra.Command, _ []string) (interface{}, error) {
return stackHandler.list()
})
// status
newCmd(rootCmd, &cobra.Command{
Use: "status stack-name",
Short: "Show stack status",
Long: `Show status of the stack.`,
Args: exactArgs(1),
}, func(_ *cobra.Command, args []string) (interface{}, error) {
return stackHandler.status(args[0])
})
// init
newCmd(rootCmd, &cobra.Command{
Use: "init",
Short: "Initialize bootstrap stack",
Long: `Initialize bootstrap stack.`,
Args: exactArgs(0),
}, func(_ *cobra.Command, _ []string) (interface{}, error) {
return stackHandler.init()
}, flagAutoApprove)
// plan
newCmd(rootCmd, &cobra.Command{
Use: "plan stack-name [plan-id]",
Short: "Plan stack changes",
Long: `Plan the changes on stack using change set.
If plan-id is specified, displays previously planned change.
exit codes are following:
0 - no changes on stack
1 - error occurred
2 - contains changes
`,
Args: rangeArgs(1, 2),
}, func(_ *cobra.Command, args []string) (interface{}, error) {
if len(args) == 1 {
return stackHandler.plan(args[0])
}
return stackHandler.planStatus(args[0], args[1])
}, flagIgnoreNestedUpdates, flagVerifyParentStacks)
// execute
newCmd(rootCmd, &cobra.Command{
Use: "execute stack-name {plan-id}",
Short: "Execute previously planned change",
Long: `Execute previously planned change on stack.`,
Args: exactArgs(2),
}, func(_ *cobra.Command, args []string) (interface{}, error) {
return stackHandler.execute(args[0], args[1])
})
// destroy
newCmd(rootCmd, &cobra.Command{
Use: "destroy stack-name",
Short: "Destroy stack",
Long: `Destroy AWS CloudFormation stack.
This command requires interactive shell or -a flag to be specified.`,
Args: exactArgs(1),
}, func(_ *cobra.Command, args []string) (interface{}, error) {
return stackHandler.destroy(args[0])
}, flagAutoApprove)
// deploy
newCmd(rootCmd, &cobra.Command{
Use: "deploy stack-name",
Short: "Deploy stack",
Long: `Deploy AWS CloudFormation stack.
This command requires interactive shell or -a flag to be specified.`,
Args: exactArgs(1),
}, func(_ *cobra.Command, args []string) (interface{}, error) {
return stackHandler.deploy(args[0])
}, flagAutoApprove, flagIgnoreNestedUpdates, flagVerifyParentStacks)
// version
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "show version information",
Args: exactArgs(0),
PersistentPreRunE: func(_ *cobra.Command, _ []string) error { return nil },
RunE: func(_ *cobra.Command, _ []string) error {
if configFlags.debug {
fmt.Printf("clon %s (commit #%s)\n", Version, Revision)
} else {
fmt.Printf("clon %s\n", Version)
}
return nil
},
})
}
// Execute will execute the root command and output.
func Execute() {
if err := rootCmd.Execute(); err != nil {
traceableError, ok := err.(*errors.Err)
var stackTrace []string
if ok {
err = errors.Cause(traceableError)
stackTrace = traceableError.StackTrace()
}
if e, ok := err.(argsError); ok {
log.Error(e)
e.cmd.Usage()
os.Exit(1)
return
}
code := 1
if e, ok := err.(*errorCode); ok {
code = e.code
err = e.err
}
if err != nil && code == 0 {
fmt.Fprintln(stderr, err)
} else if err != nil {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s", err)
if stackTrace != nil && configFlags.trace {
fmt.Fprintf(&buf, "\n%s", strings.Join(stackTrace[1:], "\n"))
fmt.Fprintf(&buf, "\nclon %s (commit %s)", Version, Revision)
}
log.Error(buf.String())
}
os.Exit(code)
}
}