-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmd.go
181 lines (161 loc) · 4.76 KB
/
cmd.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
/*
Copyright 2017, 2018 Ankyra
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/spf13/cobra"
)
var state, environment, deployment, escapePlanLocation, remoteState string
var useProfileState bool
func LoadState() error {
context.SetRootDeploymentName(deployment)
if remoteState != "" {
if err := context.LoadRemoteState(remoteState, environment); err != nil {
return err
}
return nil
}
return context.LoadLocalState(state, environment, useProfileState)
}
func ProcessFlagsForContextAndLoadEscapePlanWithVersionOverride(versionOverride string) error {
return processFlagsForContext(true, versionOverride)
}
func ProcessFlagsForContextAndLoadEscapePlan() error {
return processFlagsForContext(true, "")
}
func ProcessFlagsForContext() error {
return processFlagsForContext(false, "")
}
func processFlagsForContext(loadLocalEscapePlan bool, versionOverride string) error {
if environment == "" {
return fmt.Errorf("Missing 'environment'")
}
if err := LoadState(); err != nil {
return err
}
if loadLocalEscapePlan {
if err := context.LoadEscapePlan(escapePlanLocation); err != nil {
return err
}
if versionOverride != "" {
context.GetEscapePlan().Version = versionOverride
}
if err := context.CompileEscapePlan(); err != nil {
return err
}
}
return nil
}
func setEscapePlanLocationFlag(c *cobra.Command) {
c.Flags().StringVarP(&escapePlanLocation,
"plan", "", "escape.yml",
"The location of the Escape plan.",
)
}
func setEscapeStateLocationFlag(c *cobra.Command) {
c.Flags().StringVarP(&state,
"state", "s", "escape_state.json",
"Location of the Escape state file (ignored when --remote-state is set)",
)
c.Flags().BoolVarP(&useProfileState,
"use-profile-state", "", false,
"Instead of using the Escape state file specified in --state, read the 'state_path' value from the configuration profile.")
}
func setEscapeStateEnvironmentFlag(c *cobra.Command) {
c.Flags().StringVarP(&environment,
"environment", "e", "dev",
"The logical environment to target",
)
}
func setEscapeDeploymentFlag(c *cobra.Command) {
c.Flags().StringVarP(&deployment,
"deployment", "d", "",
"Deployment name (default is the package's \"project/name\")",
)
}
func setEscapeRemoteStateFlag(c *cobra.Command) {
c.Flags().StringVarP(&remoteState,
"remote-state", "r", "",
"Use remote state project.")
}
func setPlanAndStateFlags(c *cobra.Command) {
setEscapePlanLocationFlag(c)
setEscapeStateLocationFlag(c)
setEscapeStateEnvironmentFlag(c)
setEscapeDeploymentFlag(c)
setEscapeRemoteStateFlag(c)
}
func ParseExtraVars(extraVars []string) (result map[string]interface{}, err error) {
result = map[string]interface{}{}
for _, extraVar := range extraVars {
err = fmt.Errorf("Invalid extra variable format '%s'", extraVar)
parts := strings.Split(extraVar, "=")
if len(parts) == 0 {
return nil, err
}
key := parts[0]
value := strings.Join(parts[1:], "=")
if value == "" {
if strings.HasPrefix(key, "@") {
v, err := ioutil.ReadFile(key[1:])
if err != nil {
return nil, fmt.Errorf("Coulnd't read file '%s': %s", key[1:], err.Error())
}
unmarshalled := map[string]interface{}{}
err = json.Unmarshal(v, &unmarshalled)
if err != nil {
return nil, fmt.Errorf("Coulnd't read file '%s' into JSON map: %s", key[1:], err.Error())
}
for key, val := range unmarshalled {
result[key] = val
}
} else {
result[key] = ""
}
} else if strings.HasPrefix(value, "@") {
v, err := ioutil.ReadFile(value[1:])
if err != nil {
return nil, fmt.Errorf("Coulnd't read file '%s': %s", value[1:], err.Error())
}
result[key] = string(v)
} else {
result[key] = value
}
}
return result, nil
}
func ParseExtraProviders(extraVars []string) (map[string]string, error) {
result := map[string]string{}
parsed, err := ParseExtraVars(extraVars)
if err != nil {
return nil, err
}
for key, val := range parsed {
switch val.(type) {
case string:
result[key] = val.(string)
default:
return nil, fmt.Errorf("Expecting a string value for extra provider '%s' got %v", key, val)
}
}
return result, nil
}
func NoExtraArgsPreRunE(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("Unknown command '%s'", args[0])
}
return nil
}