forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generated_import_files.go
191 lines (169 loc) · 4.93 KB
/
generated_import_files.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
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
package config
import (
"fmt"
"strings"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/shell"
)
// ImportFilesList represents an array of ImportFiles
type ImportFilesList []ImportFiles
// IImportFiles returns TerragruntExtensioner from the supplied type
func IImportFiles(item interface{}) TerragruntExtensioner {
return item.(TerragruntExtensioner)
}
func (list ImportFilesList) init(config *TerragruntConfigFile) {
for i := range list {
IImportFiles(&list[i]).init(config)
}
}
// Merge elements from an imported list to the current list priorising those already existing
func (list *ImportFilesList) merge(imported ImportFilesList, mode mergeMode, argName string) {
if len(imported) == 0 {
return
} else if len(*list) == 0 {
*list = imported
return
}
log := IImportFiles(&(*list)[0]).logger().Debugf
// Create a map with existing elements
index := make(map[string]int, len(*list))
for i, item := range *list {
index[IImportFiles(&item).id()] = i
}
// Create a list of the hooks that should be added to the list
newList := make(ImportFilesList, 0, len(imported))
for _, item := range imported {
name := IImportFiles(&item).id()
if pos, exist := index[name]; exist {
// It already exist in the list, so is is an override
// We remove it from its current position and add it to the list of newly added elements to keep its original declaration ordering.
newList = append(newList, (*list)[pos])
delete(index, name)
log("Skipping %s %v as it is overridden in the current config", argName, name)
continue
}
newList = append(newList, item)
}
if len(index) != len(*list) {
// Some elements must be removed from the original list, we simply regenerate the list
// including only elements that are still in the index.
newList := make(ImportFilesList, 0, len(index))
for _, item := range *list {
name := IImportFiles(&item).id()
if _, found := index[name]; found {
newList = append(newList, item)
}
}
*list = newList
}
if mode == mergeModeAppend {
*list = append(*list, newList...)
} else {
*list = append(newList, *list...)
}
}
// Help returns the information relative to the elements within the list
func (list ImportFilesList) Help(listOnly bool, lookups ...string) (result string) {
list.sort()
add := func(item TerragruntExtensioner, name string) {
extra := item.extraInfo()
if extra != "" {
extra = " " + extra
}
result += fmt.Sprintf("\n%s%s%s\n%s", TitleID(item.id()), name, extra, item.help())
}
var table [][]string
width := []int{30, 0, 0}
if listOnly {
addLine := func(values ...string) {
table = append(table, values)
for i, value := range values {
if len(value) > width[i] {
width[i] = len(value)
}
}
}
add = func(item TerragruntExtensioner, name string) {
addLine(TitleID(item.id()), name, item.extraInfo())
}
}
for _, item := range list.Enabled() {
item := IImportFiles(&item)
match := len(lookups) == 0
for i := 0; !match && i < len(lookups); i++ {
match = strings.Contains(item.name(), lookups[i]) || strings.Contains(item.id(), lookups[i]) || strings.Contains(item.extraInfo(), lookups[i])
}
if !match {
continue
}
var name string
if item.id() != item.name() {
name = " " + item.name()
}
add(item, name)
}
if listOnly {
for i := range table {
result += fmt.Sprintln()
for j := range table[i] {
result += fmt.Sprintf("%-*s", width[j]+1, table[i][j])
}
}
}
return
}
// Enabled returns only the enabled items on the list
func (list ImportFilesList) Enabled() ImportFilesList {
result := make(ImportFilesList, 0, len(list))
for _, item := range list {
iItem := IImportFiles(&item)
if iItem.enabled() {
iItem.normalize()
result = append(result, item)
}
}
return result
}
// Run execute the content of the list
func (list ImportFilesList) Run(status error, args ...interface{}) (result []interface{}, err error) {
if len(list) == 0 {
return
}
list.sort()
var (
errs errorArray
errOccurred bool
)
for _, item := range list {
iItem := IImportFiles(&item)
if (status != nil || errOccurred) && !iItem.ignoreError() {
continue
}
iItem.logger().Infof("Running %s (%s): %s", iItem.itemType(), iItem.id(), iItem.name())
iItem.normalize()
temp, currentErr := iItem.run(args...)
currentErr = shell.FilterPlanError(currentErr, iItem.options().TerraformCliArgs[0])
if currentErr != nil {
if _, ok := currentErr.(errors.PlanWithChanges); ok {
errs = append(errs, currentErr)
} else {
errOccurred = true
errs = append(errs, fmt.Errorf("Error while executing %s(%s): %v", iItem.itemType(), iItem.id(), currentErr))
}
}
iItem.setState(currentErr)
result = append(result, temp)
}
switch len(errs) {
case 0:
break
case 1:
err = errs[0]
default:
err = errs
}
return
}