-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
154 lines (113 loc) · 5.34 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/zclconf/go-cty/cty"
"os"
"github.com/stevenaldinger/decker/internal/pkg/dependencies"
"github.com/stevenaldinger/decker/internal/pkg/hcl"
"github.com/stevenaldinger/decker/internal/pkg/paths"
"github.com/stevenaldinger/decker/internal/pkg/reports"
"github.com/stevenaldinger/decker/pkg/gocty"
"github.com/stevenaldinger/decker/pkg/plugins"
)
// loop over list of strings and return true if list contains a given string
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func main() {
// get the path to a given config file passed in via CLI
hclConfigFile := paths.GetConfigFilePath()
blocks := hcl.GetBlocksFromConfig(hclConfigFile)
dependencies.ValidateConfig(blocks)
varBlockNames := dependencies.GetVariableNames(blocks)
resBlocksSorted := dependencies.Sort(blocks)
// get environment variable values for each declared variable block
var envVarCtx = hcl.GetHCLEvalContextVarsFromEnv(varBlockNames)
// map to keep track of all the values returned from plugins
resultsMapCty := map[string]*map[string]cty.Value{}
resultsMapCtyNested := map[string]*map[string]*map[string]cty.Value{}
decoder := gocty.Decoder{}
encoder := gocty.Encoder{}
for index, block := range resBlocksSorted {
resourcePlugin, resourceName := block.Labels[0], block.Labels[1]
pluginHCLPath := paths.GetPluginHCLFilePath(resourcePlugin)
pluginAttrs := hcl.GetPluginAttributes(block)
containsForEach := contains(pluginAttrs, "for_each")
hclConfig, pluginContent := hcl.GetPluginContent(containsForEach, block, pluginHCLPath)
if containsForEach {
// returns JSON, not sure why
forEachDecoded := hcl.DecodeHCLListAttribute(pluginContent.Attributes["for_each"], envVarCtx, &resultsMapCty, &resultsMapCtyNested)
var forEachList []string
jsonUnmarshalErr := json.Unmarshal([]byte(forEachDecoded), &forEachList)
if jsonUnmarshalErr != nil {
fmt.Println("Error unmarshaling json", jsonUnmarshalErr)
os.Exit(1)
}
for _, eachKey := range forEachList {
var forEachMap = &map[string]cty.Value{
"key": encoder.StringVal(string(eachKey)),
}
resultsMapCty["each"] = forEachMap
inputsMap := hcl.CreateInputsMapCty(hclConfig.Inputs, pluginContent.Attributes, envVarCtx, &resultsMapCty, &resultsMapCtyNested)
// declare a new empty map to be passed into the plugin
var resultsMap = map[string]cty.Value{}
var resultsListMap = map[string][]cty.Value{}
pluginEnabled := plugins.RunIfEnabled(resourcePlugin, &inputsMap, &resultsMap, &resultsListMap)
if pluginEnabled {
fmt.Println(fmt.Sprintf("DECKER: Ran plugin %d[%s] of %d: %s (%s)", index+1, string(eachKey), len(resBlocksSorted), resourcePlugin, resourceName))
} else {
fmt.Println(fmt.Sprintf("DECKER: [Disabled] Did not run plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))
resultsMap["raw_output"] = encoder.StringVal("")
}
// initialize if map doesn't exist yet
if _, ok := resultsMapCtyNested[resourceName]; !ok {
var initMap = &map[string]*map[string]cty.Value{}
// var initMap = &map[string]cty.Value {}
resultsMapCtyNested[resourceName] = initMap
}
// build eval context from plugin results and add it to the ongoing map
(*resultsMapCtyNested[resourceName])[string(eachKey)] = &resultsMap
if pluginEnabled {
reports.WriteStringToFile(paths.GetReportFilePath(resourceName+"["+string(eachKey)+"]", "txt"), decoder.GetString(resultsMap["raw_output"]))
}
}
} else {
inputsMap := hcl.CreateInputsMapCty(hclConfig.Inputs, pluginContent.Attributes, envVarCtx, &resultsMapCty, &resultsMapCtyNested)
// declare a new empty map to be passed into the plugin
var resultsMap = map[string]cty.Value{}
var resultsListMap = map[string][]cty.Value{}
pluginEnabled := plugins.RunIfEnabled(resourcePlugin, &inputsMap, &resultsMap, &resultsListMap)
if pluginEnabled {
fmt.Println(fmt.Sprintf("DECKER: Ran plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))
// build eval context from plugin results and add it to the ongoing map
resultsMapCty[resourceName] = &resultsMap
reports.WriteStringToFile(paths.GetReportFilePath(resourceName, "txt"), decoder.GetString(resultsMap["raw_output"]))
} else {
fmt.Println(fmt.Sprintf("DECKER: [Disabled] Did not run plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))
resultsMap["raw_output"] = encoder.StringVal("")
// build eval context from plugin results and add it to the ongoing map
resultsMapCty[resourceName] = &resultsMap
}
}
}
// run outputs plugin
outputsResults := plugins.RunOutputsPlugin(&resultsMapCty)
if os.Getenv("DECKER_OUTPUTS_XML") == "true" {
reports.WriteStringToFile(paths.GetReportFilePath("All", "xml"), outputsResults.AllXML)
for uniqueName, outputVal := range outputsResults.Results {
reports.WriteStringToFile(paths.GetReportFilePath(uniqueName, "xml"), outputVal.XML)
}
}
if os.Getenv("DECKER_OUTPUTS_JSON") == "true" {
reports.WriteStringToFile(paths.GetReportFilePath("All", "json"), outputsResults.AllJSON)
for uniqueName, outputVal := range outputsResults.Results {
reports.WriteStringToFile(paths.GetReportFilePath(uniqueName, "json"), outputVal.JSON)
}
}
}