forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generated_approval_config.go
145 lines (127 loc) · 3.87 KB
/
generated_approval_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
// 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"
)
// ApprovalConfigList represents an array of ApprovalConfig
type ApprovalConfigList []ApprovalConfig
// IApprovalConfig returns TerragruntExtensioner from the supplied type
func IApprovalConfig(item interface{}) TerragruntExtensioner {
return item.(TerragruntExtensioner)
}
func (list ApprovalConfigList) init(config *TerragruntConfigFile) {
for i := range list {
IApprovalConfig(&list[i]).init(config)
}
}
// Merge elements from an imported list to the current list priorising those already existing
func (list *ApprovalConfigList) merge(imported ApprovalConfigList, mode mergeMode, argName string) {
if len(imported) == 0 {
return
} else if len(*list) == 0 {
*list = imported
return
}
log := IApprovalConfig(&(*list)[0]).logger().Debugf
// Create a map with existing elements
index := make(map[string]int, len(*list))
for i, item := range *list {
index[IApprovalConfig(&item).id()] = i
}
// Create a list of the hooks that should be added to the list
newList := make(ApprovalConfigList, 0, len(imported))
for _, item := range imported {
name := IApprovalConfig(&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(ApprovalConfigList, 0, len(index))
for _, item := range *list {
name := IApprovalConfig(&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 ApprovalConfigList) 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 := IApprovalConfig(&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 ApprovalConfigList) Enabled() ApprovalConfigList {
result := make(ApprovalConfigList, 0, len(list))
for _, item := range list {
iItem := IApprovalConfig(&item)
if iItem.enabled() {
iItem.normalize()
result = append(result, item)
}
}
return result
}