forked from imjerrybao/apex
/
root.go
175 lines (141 loc) · 3.59 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
package root
import (
"os"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/pkg/errors"
"github.com/tj/cobra"
"github.com/apex/apex/dryrun"
"github.com/apex/apex/project"
"github.com/apex/apex/utils"
)
// environment for project.
var environment string
// chdir working directory.
var chdir string
// dryRun enabled.
var dryRun bool
// logLevel specified.
var logLevel string
// profile for AWS.
var profile string
// iamrole for AWS.
var iamrole string
// region for AWS.
var region string
// Session instance.
var Session *session.Session
// Project instance.
var Project *project.Project
// Config for AWS.
var Config *aws.Config
// Register `cmd`.
func Register(cmd *cobra.Command) {
Command.AddCommand(cmd)
}
// Command config.
var Command = &cobra.Command{
Use: "apex",
PersistentPreRunE: preRun,
SilenceErrors: true,
SilenceUsage: true,
}
// Initialize.
func init() {
f := Command.PersistentFlags()
f.StringVarP(&environment, "env", "e", "", "Environment name")
f.StringVarP(&chdir, "chdir", "C", "", "Working directory")
f.BoolVarP(&dryRun, "dry-run", "D", false, "Perform a dry-run")
f.StringVarP(&logLevel, "log-level", "l", "info", "Log severity level")
f.StringVarP(&profile, "profile", "p", "", "AWS profile")
f.StringVarP(&iamrole, "iamrole", "i", "", "AWS iamrole")
f.StringVarP(®ion, "region", "r", "", "AWS region")
}
// PreRunNoop noop for other commands.
func PreRunNoop(c *cobra.Command, args []string) {
// TODO: ew... better way to disable in cobra?
}
// preRun sets up global tasks used for most commands, some use PreRunNoop
// to remove this default behaviour.
func preRun(c *cobra.Command, args []string) error {
err := Prepare(c, args)
if err != nil {
return err
}
return Project.Open()
}
// Prepare handles the global CLI flags and shared functionality without
// the assumption that a Project has already been initialized.
//
// Precedence is currently:
//
// - flags such as --profile
// - env vars such as AWS_PROFILE
// - files such as ~/.aws/config
//
func Prepare(c *cobra.Command, args []string) error {
if l, err := log.ParseLevel(logLevel); err == nil {
log.SetLevel(l)
}
// config defaults
Config = aws.NewConfig()
if chdir != "" {
if err := os.Chdir(chdir); err != nil {
return err
}
}
// profile from flag, config, env, "default"
if profile == "" {
profile, _ = utils.ProfileFromConfig(environment)
if profile == "" {
profile = os.Getenv("AWS_PROFILE")
if profile == "" {
profile = "default"
}
}
}
// the default SharedCredentialsProvider checks the env
os.Setenv("AWS_PROFILE", profile)
// region from flag, env, file
if region == "" {
region = os.Getenv("AWS_REGION")
if region == "" {
region, _ = utils.GetRegion(profile)
}
}
if region != "" {
Config = Config.WithRegion(region)
}
// environment from flag or env
if environment == "" {
environment = os.Getenv("APEX_ENVIRONMENT")
}
// iamrole from flag, env
if iamrole == "" {
iamrole = os.Getenv("AWS_ROLE")
}
if iamrole != "" {
config, err := utils.AssumeRole(iamrole, Config)
if err != nil {
return errors.Wrap(err, "assuming role")
}
Config = config
}
Session = session.New(Config)
Project = &project.Project{
Environment: environment,
InfraEnvironment: environment,
Log: log.Log,
Path: ".",
}
if dryRun {
log.SetLevel(log.WarnLevel)
Project.Service = dryrun.New(Session)
Project.Concurrency = 1
} else {
Project.Service = lambda.New(Session)
}
return nil
}