-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
226 lines (210 loc) · 5.95 KB
/
config.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
package config
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/totmicro/atlantis-yaml-generator/pkg/helpers"
)
type Config struct {
Parameters map[string]string
}
var GlobalConfig Config
type Parameter struct {
Name string
Description string
Required bool
Dependencies DependentParameters
DefaultValue string
Shorthand string
Value string
}
type DependentParameters struct {
WhenParentParameterIs string
ParameterList []string
}
// ParameterList is the source of truth list for all parameters
var ParameterList = []Parameter{
{
Name: "automerge",
Description: "Atlantis automerge config value.",
Required: false,
DefaultValue: "false",
Shorthand: "",
},
{
Name: "parallel-apply",
Description: "Atlantis parallel apply config value.",
Required: false,
DefaultValue: "true",
Shorthand: "",
},
{
Name: "parallel-plan",
Description: "Atlantis parallel plan config value.",
Required: false,
DefaultValue: "true",
Shorthand: "",
},
{
Name: "terraform-base-dir",
Description: "Basedir for terraform resources.",
Required: false,
DefaultValue: "./",
Shorthand: "",
},
{
Name: "output-file",
Description: "Atlantis YAML output file name.",
Required: false,
DefaultValue: "atlantis.yaml",
Shorthand: "f",
},
{
Name: "output-type",
Description: "Atlantis YAML output type. [file|stdout].",
Required: false,
DefaultValue: "file",
Shorthand: "e",
},
{
Name: "workflow",
Description: "Atlantis Workflow to be used.",
Required: false,
DefaultValue: "",
Shorthand: "w",
},
{
Name: "when-modified",
Description: "Atlantis will trigger an autoplan when these modifications occur (list of strings).",
Required: false,
DefaultValue: "**/*.tf,**/*.tfvars,**/*.json,**/*.tpl,**/*.tmpl,**/*.xml",
Shorthand: "m",
},
{
Name: "excluded-projects",
Description: "Atlantis regex filter to exclude projects.",
Required: false,
DefaultValue: "",
Shorthand: "x",
},
{
Name: "included-projects",
Description: "Atlantis regex filter to only include projects.",
Required: false,
DefaultValue: "",
Shorthand: "z",
},
{
Name: "pattern-detector",
Description: "discover projects based on files or directories names.",
Required: false,
DefaultValue: "main.tf",
Shorthand: "q",
},
{
Name: "discovery-mode",
Description: "mode used to discover projects [single-workspace|multi-workspace]",
Required: false,
DefaultValue: "single-workspace",
Shorthand: "d",
},
{
Name: "pull-num",
Description: "Github Pull Request Number to check diffs.",
Required: false,
DefaultValue: "",
Shorthand: "p",
},
{
Name: "base-repo-name",
Description: "Github Repo Name.",
Required: false,
DefaultValue: "",
Shorthand: "r",
},
{
Name: "base-repo-owner",
Description: "Github Repo Owner Name.",
Required: false,
DefaultValue: "",
Shorthand: "o",
},
{
Name: "gh-token",
Description: "Specify the GitHub token when automatic detection is not possible.",
Required: false,
DefaultValue: "",
Shorthand: "t",
},
{
Name: "pr-filter",
Description: "Filter projects based on the PR changes (Only for github SCM).",
Required: false,
Dependencies: DependentParameters{
WhenParentParameterIs: "true",
ParameterList: []string{"pull-num", "base-repo-name", "base-repo-owner"}},
DefaultValue: "false",
Shorthand: "u",
},
}
// Init generates the config Parameters object and checks for missing required parameters
func Init(ccmd *cobra.Command) (err error) {
GlobalConfig.Parameters = make(map[string]string)
for i, param := range ParameterList {
GlobalConfig.Parameters[param.Name] = getFlagOrEnv(ccmd, ParameterList[i].Name,
generateEnvVarName(ParameterList[i].Name), ParameterList[i].DefaultValue)
}
err = CheckRequiredParameters(ParameterList)
return err
}
// GetFlagOrEnv gets the value of a flag or environment variable and if both are empty, returns the default value
func getFlagOrEnv(ccmd *cobra.Command, flagName, envVar string, defaultValue string) string {
val, _ := ccmd.Flags().GetString(flagName)
if val != "" {
return val
}
val = helpers.LookupEnvString(envVar)
if val != "" {
return val
} else {
return defaultValue
}
}
func CheckRequiredParameters(parameterList []Parameter) error {
missingParams := []string{}
// Iterate through the list of parameters
for _, param := range parameterList {
if param.Required && GlobalConfig.Parameters[param.Name] == "" {
missingParams = append(missingParams, param.Name)
} else if len(param.Dependencies.ParameterList) > 0 {
// Check if the parameter has dependent parameters
parentValue := GlobalConfig.Parameters[param.Name]
expectedParentValue := param.Dependencies.WhenParentParameterIs
if parentValue == expectedParentValue {
// Iterate through the dependent parameters
for _, dependentParameter := range param.Dependencies.ParameterList {
if GlobalConfig.Parameters[dependentParameter] == "" {
missingParams = append(missingParams, dependentParameter)
}
}
}
}
}
// Check if any missing parameters were found
if len(missingParams) > 0 {
return fmt.Errorf("Missing required parameters: %s", strings.Join(missingParams, ", "))
}
return nil
}
// GenerateDescription generates the description for a parameter
func GenerateDescription(param string, description string) (updatedDescription string) {
envVar := generateEnvVarName(param)
updatedDescription = fmt.Sprintf("%s (Equivalent envVar: [%s])", description, envVar)
return updatedDescription
}
// generateEnvVarName generates the environment variable name for a parameter
func generateEnvVarName(param string) string {
param = strings.ReplaceAll(param, "-", "_")
param = strings.ToUpper(param)
return param
}