-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_writer.go
64 lines (45 loc) · 1.48 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
package main
import (
"path"
"github.com/rs/zerolog/log"
"github.com/yoanm/go-github-tf/core"
)
const (
noErrorExitCode = 0
readWorkspaceErrorExitCode = 1
computeConfigErrorExitCode = 2
generateTerraformFilesErrorExitCode = 3
writeTerraformFilesErrorExitCode = 4
)
func loadYamlAndWriteTerraform(workspacePath, configDir, templateDir, terraformDir, yamlAnchorDir string) int {
var err error
var rawConfig *core.Config
if rawConfig, err = readWorkspace(workspacePath, configDir, templateDir, yamlAnchorDir); err != nil {
log.Error().Msgf("%s", err)
return readWorkspaceErrorExitCode
}
core.ConfigTrace("Decoded config", rawConfig)
log.Info().Msgf(
"Found: %d repos / %d repo templates / %d branch templates / %d branch protection templates",
len(rawConfig.Repos),
len(rawConfig.Templates.Repos),
len(rawConfig.Templates.Branches),
len(rawConfig.Templates.BranchProtections),
)
var config *core.Config
if config, err = core.ComputeConfig(rawConfig); err != nil {
log.Error().Msgf("%s", err)
return computeConfigErrorExitCode
}
core.ConfigTrace("Computed config", config)
files, err := core.GenerateHclRepoFiles(config.Repos)
if err != nil {
log.Error().Msgf("%s", err)
return generateTerraformFilesErrorExitCode
}
if err = core.WriteTerraformFiles(path.Join(workspacePath, terraformDir), files); err != nil {
log.Error().Msgf("%s", err)
return writeTerraformFilesErrorExitCode
}
return noErrorExitCode
}