forked from daidokoro/qaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
108 lines (86 loc) · 3.45 KB
/
init.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
package commands
// Init & Logging sit here
import (
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
// Used for mapping log level, may or may not expand in the future..
var level = struct {
debug string
warn string
err string
info string
}{"debug", "warn", "error", "info"}
// handleError - handleError logs the err and exits the app if err not nil
func handleError(e error) {
if e != nil {
log.WithFields(log.Fields{
"request": job.request,
}).Errorln(e.Error()) // logrus error calls os.exit after logging
}
}
// Log - Handles all logging accross app
func Log(msg, lvl string) {
// TODO: Get this to only check once.
if job.debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
l := log.WithFields(log.Fields{
"request": job.request,
})
switch lvl {
case "debug":
l.Debugln(msg)
case "warn":
l.Warnln(msg)
case "error":
l.Errorln(msg)
default:
l.Infoln(msg)
}
}
func init() {
// Define Deploy Flags
deployCmd.Flags().StringArrayVarP(&job.tplFiles, "template", "t", []string{`./templates/*`}, "path to template file(s) Or stack::url")
deployCmd.Flags().BoolVarP(&job.rollback, "rollback", "R", false, "Set Stack to rollback on deployment failures")
// Define Terminate Flags
terminateCmd.Flags().BoolVarP(&job.terminateAll, "all", "A", false, "terminate all stacks")
// Define Output Flags
outputsCmd.Flags().StringVarP(&job.profile, "profile", "p", "default", "configured aws profile")
// Define Exports Flags
exportsCmd.Flags().StringVarP(®ion, "region", "r", "eu-west-1", "AWS Region")
// Define Root Flags
RootCmd.Flags().BoolVarP(&job.version, "version", "", false, "print current/running version")
RootCmd.PersistentFlags().StringVarP(&job.profile, "profile", "p", "default", "configured aws profile")
RootCmd.PersistentFlags().BoolVarP(&job.debug, "debug", "", false, "Run in debug mode...")
// Define Invoke Flags
invokeCmd.Flags().StringVarP(®ion, "region", "r", "eu-west-1", "AWS Region")
invokeCmd.Flags().StringVarP(&job.funcEvent, "event", "e", "", "JSON Event data for AWS Lambda invoke")
// Define Changes Command
changeCmd.AddCommand(create, rm, list, execute, desc)
// Add Config --config common flag
for _, cmd := range []interface{}{tailCmd, checkCmd, updateCmd, outputsCmd, statusCmd, terminateCmd, generateCmd, deployCmd} {
cmd.(*cobra.Command).Flags().StringVarP(&job.cfgFile, "config", "c", "config.yml", "path to config file")
}
// Add Template --template common flag
for _, cmd := range []interface{}{generateCmd, updateCmd, checkCmd} {
cmd.(*cobra.Command).Flags().StringVarP(&job.tplFile, "template", "t", "template", "path to template file Or stack::url")
}
for _, cmd := range []interface{}{create, list, rm, execute, desc} {
cmd.(*cobra.Command).Flags().StringVarP(&job.cfgFile, "config", "c", "config.yml", "path to config file [Required]")
cmd.(*cobra.Command).Flags().StringVarP(&job.stackName, "stack", "s", "", "Qaz local project Stack Name [Required]")
}
create.Flags().StringVarP(&job.tplFile, "template", "t", "template", "path to template file Or stack::url")
changeCmd.Flags().StringVarP(&job.cfgFile, "config", "c", "config.yml", "path to config file")
RootCmd.AddCommand(
generateCmd, deployCmd, terminateCmd,
statusCmd, outputsCmd, initCmd,
updateCmd, checkCmd, exportsCmd,
invokeCmd, tailCmd, changeCmd,
)
// Setup logging
log.SetFormatter(new(prefixed.TextFormatter))
}