-
Notifications
You must be signed in to change notification settings - Fork 8
/
stack.go
269 lines (211 loc) · 5.75 KB
/
stack.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
// Copyright 2023 Google LLC
//
// 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 config
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
// Stack represents the input config and output settings for this DeployStack
type Stack struct {
Settings Settings
Config Config
}
// NewStack returns an initialized Stack
func NewStack() Stack {
s := Stack{}
s.Settings = Settings{}
return s
}
func (s *Stack) findAndReadConfig() (Config, error) {
config := Config{}
candidates := []string{
".deploystack/deploystack.yaml",
".deploystack/deploystack.json",
"deploystack.json",
}
configPath := ""
wd, _ := os.Getwd()
for _, v := range candidates {
file := fmt.Sprintf("%s/%s", wd, v)
if _, err := os.Stat(file); err == nil {
configPath = file
break
}
}
if configPath == "" {
return config, ErrConfigNotExist
}
content, err := ioutil.ReadFile(configPath)
if err != nil {
return config, fmt.Errorf("unable to find or read config (%s) file: %s", configPath, err)
}
switch filepath.Ext(configPath) {
case ".yaml":
config, err = NewConfigYAML(content)
if err != nil {
return config, fmt.Errorf("unable to parse config file: %s", err)
}
return config, nil
default:
config, err = NewConfigJSON(content)
if err != nil {
return config, fmt.Errorf("unable to parse config file: %s", err)
}
}
return config, nil
}
// ErrConfigNotExist is what happens when a config file either does not exist
// or exists but is not readable.
var ErrConfigNotExist = fmt.Errorf("could not find and parse a config file")
func (s *Stack) findDSFolder(c Config, folder string) (string, error) {
switch folder {
case "messages":
if c.PathMessages != "" {
return c.PathMessages, nil
}
case "scripts":
if c.PathScripts != "" {
return c.PathScripts, nil
}
}
path := fmt.Sprintf(".deploystack/%s", folder)
if _, err := os.Stat(path); err == nil {
return path, nil
}
path = folder
if _, err := os.Stat(path); err == nil {
return path, nil
}
return fmt.Sprintf("./%s", folder), fmt.Errorf("requirement (%s) was not found either in the root, or in .deploystack folder nor was it set in deploystack.json", folder)
}
func (s *Stack) findTFFolder(c Config) (string, error) {
if c.PathTerraform != "" {
return c.PathTerraform, nil
}
result := "terraform"
if _, err := os.Stat(result); err == nil {
return result, nil
}
wd, err := os.Getwd()
if err != nil {
return "", err
}
err = filepath.Walk(wd, func(path string, info os.FileInfo, err error) error {
if info.Name() == "main.tf" {
result = path
result = filepath.Dir(result)
result, err = filepath.Rel(wd, result)
return err
}
return nil
})
return result, nil
}
// FindAndReadRequired finds and reads in a Config from a json file.
func (s *Stack) FindAndReadRequired() error {
config, err := s.findAndReadConfig()
if err != nil {
return fmt.Errorf("unable to parse config file: %s", err)
}
tfPath, err := s.findTFFolder(config)
if err != nil {
return fmt.Errorf("unable to locate terraform folder: %s", err)
}
config.PathTerraform = tfPath
scriptPath, _ := s.findDSFolder(config, "scripts")
if err != nil {
log.Printf("WARNING - unable to locate scripts folder, folder not required, : %s", err)
}
config.PathScripts = scriptPath
messagePath, err := s.findDSFolder(config, "messages")
if err != nil {
log.Printf("WARNING - unable to locate messages folder, folder not required, : %s", err)
}
config.PathMessages = messagePath
descText := fmt.Sprintf("%s/description.txt", messagePath)
if _, err := os.Stat(descText); err == nil {
description, err := ioutil.ReadFile(descText)
if err != nil {
return fmt.Errorf("unable to read description file: %s", err)
}
config.Description = string(description)
}
s.Config = config
s.Config.convertHardset()
s.Config.defaultAuthorSettings()
return nil
}
// AddSetting stores a setting key/value pair.
func (s *Stack) AddSetting(key, value string) {
s.Settings.Add(key, value)
}
// AddSettingComplete passes a completely intact setting to the underlying
// setting structure
func (s *Stack) AddSettingComplete(set Setting) {
s.Settings.AddComplete(set)
}
// GetSetting returns a setting value.
func (s *Stack) GetSetting(key string) string {
set := s.Settings.Find(key)
if set != nil {
return set.Value
}
return ""
}
// DeleteSetting removes a setting value.
func (s *Stack) DeleteSetting(key string) {
for i, v := range s.Settings {
if v.Name == key {
s.Settings = append(s.Settings[:i], s.Settings[i+1:]...)
}
}
}
// Terraform returns all of the settings as a Terraform variables format.
func (s Stack) Terraform() string {
result := strings.Builder{}
s.Settings.Sort()
for _, v := range s.Settings {
if v.Name == "" {
continue
}
label := v.TFvarsName()
if label == "project_name" {
continue
}
if label == "stack_name" {
continue
}
if len(v.Value) == 0 && len(v.List) == 0 && v.Map == nil {
continue
}
result.WriteString(v.TFVars())
}
return result.String()
}
// TerraformFile exports TFVars format to input file.
func (s Stack) TerraformFile(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(s.Terraform()); err != nil {
return err
}
return nil
}