-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_writer.go
184 lines (143 loc) · 3.94 KB
/
terraform_writer.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
package core
import (
"fmt"
"os"
"path"
"path/filepath"
"sync"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/rs/zerolog/log"
"github.com/yoanm/go-gh2tf"
"github.com/yoanm/go-tfsig"
)
/** Public **/
func GenerateHclRepoFiles(configList []*GhRepoConfig) (map[string]*hclwrite.File, error) {
valueGenerator := gh2tf.NewValueGenerator()
waitGroup := &sync.WaitGroup{}
collector := make(fileCollector, len(configList))
errCollector := make(errorCollector, len(configList))
var errList []error
for k, repoConfig := range configList {
waitGroup.Add(1)
if repoConfig.Name == nil {
errList = append(errList, RepositoryNameIsMandatoryForConfigIndexError(k))
} else {
repoTfId := tfsig.ToTerraformIdentifier(*repoConfig.Name)
go generateHclRepoFileAsync(repoConfig, valueGenerator, repoTfId, collector, waitGroup)
}
}
waitGroup.Wait()
close(collector)
close(errCollector)
if len(errCollector) > 0 || len(errList) > 0 {
return nil, FileGenerationError(createFileGenerationErrorMessages(errCollector, errList))
}
list := map[string]*hclwrite.File{}
for fs := range collector {
list[fs.name] = fs.file
}
return list, nil
}
func WriteTerraformFiles(rootPath string, files map[string]*hclwrite.File) error {
if len(files) == 0 {
return nil
}
waitGroup := &sync.WaitGroup{}
errCollector := make(errorCollector, len(files))
fs, statError := os.Stat(rootPath)
exists := !os.IsNotExist(statError)
isDir := exists && statError == nil && fs.IsDir()
if exists && isDir {
for fName, hclFile := range files {
waitGroup.Add(1)
go writeTerraformFileAsync(path.Join(rootPath, fName), hclFile, errCollector, waitGroup)
}
waitGroup.Wait()
}
close(errCollector)
if errList := generateWritingFileErrors(rootPath, exists, statError, isDir, errCollector); len(errList) > 0 {
return TerraformFileWritingErrors(errList)
}
return nil
}
/** Private **/
type fileCollectorItem struct {
name string
file *hclwrite.File
}
type errorCollectorItem struct {
File string
Err error
}
type (
fileCollector chan fileCollectorItem
errorCollector chan errorCollectorItem
)
func generateHclRepoFileAsync(
repoConfig *GhRepoConfig,
valGen tfsig.ValueGenerator,
repoTfId string,
collector fileCollector,
wg *sync.WaitGroup,
) {
defer wg.Done()
fname := fmt.Sprintf("repo.%s.tf", repoTfId)
collector <- fileCollectorItem{name: fname, file: NewHclRepository(repoTfId, repoConfig, valGen)}
}
func writeTerraformFileAsync(path string, hclFile *hclwrite.File, errCollector errorCollector, wg *sync.WaitGroup) {
defer wg.Done()
formatted := hclwrite.Format(hclFile.Bytes())
var (
file *os.File
err error
)
fName := filepath.Base(path)
if file, err = os.Create(path); err != nil {
errCollector <- errorCollectorItem{fName, err}
} else {
log.Debug().Msgf("Writing terraform file '%s'", file.Name())
if _, err = file.Write(formatted); err != nil {
errCollector <- errorCollectorItem{fName, err}
}
}
}
func createFileGenerationErrorMessages(errCollector errorCollector, errList []error) []string {
msgList := []string{}
if len(errCollector) > 0 {
for _, err := range sortErrCollectorByFile(errCollector) {
msgList = append(msgList, err.Error())
}
}
if len(errList) > 0 {
for _, err := range errList {
msgList = append(msgList, err.Error())
}
}
return msgList
}
func generateWritingFileErrors(
rootPath string,
exists bool,
statError error,
isDir bool,
errCollector errorCollector,
) []error {
switch {
case !exists:
return []error{WorkspacePathDoesntExistError(rootPath)}
case statError != nil:
return []error{statError}
case !isDir:
return []error{WorkspacePathIsExpectedToBeADirectoryError(rootPath)}
case len(errCollector) > 0:
return sortErrCollectorByFile(errCollector)
}
return nil
}
func sortErrCollectorByFile(errCollector errorCollector) []error {
errList := map[string]error{}
for errItem := range errCollector {
errList[errItem.File] = errItem.Err
}
return MapToSortedList(errList)
}