forked from apptio/kr8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectories.go
196 lines (153 loc) · 4.92 KB
/
directories.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
192
193
194
195
196
package cmd
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type componentDef struct {
Path string `json:"path"`
}
func (c *Clusters) addItem(item Cluster) Clusters {
c.Cluster = append(c.Cluster, item)
return *c
}
func getClusters(searchDir string) (Clusters, error) {
fileList := make([]string, 0)
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
fileList = append(fileList, path)
return err
})
if e != nil {
log.Fatal().Err(e).Msg("Error building cluster list: ")
}
ClusterData := []Cluster{}
c := Clusters{ClusterData}
for _, file := range fileList {
splitFile := strings.Split(file, "/")
// get the filename
fileName := splitFile[len(splitFile)-1]
if fileName == "cluster.jsonnet" {
entry := Cluster{Name: splitFile[len(splitFile)-2], Path: strings.Join(splitFile[:len(splitFile)-1], "/")}
c.addItem(entry)
}
}
return c, nil
}
func getCluster(searchDir string, clusterName string) string {
var clusterPath string
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
dir, file := filepath.Split(path)
if filepath.Base(dir) == clusterName && file == "cluster.jsonnet" {
clusterPath = path
return nil
} else {
return err
}
})
if e != nil {
log.Fatal().Err(e).Msg("Error building cluster list: ")
}
if clusterPath == "" {
log.Fatal().Msg("Could not find cluster: " + clusterName)
}
return clusterPath
}
func getClusterParams(basePath string, targetPath string) []string {
// a slice to store results
var results []string
results = append(results, targetPath)
// remove the cluster.jsonnet
splitFile := strings.Split(targetPath, "/")
// gets the targetdir without the cluster.jsonnet
targetDir := strings.Join(splitFile[:len(splitFile)-1], "/")
// walk through the directory hierachy
for {
rel, _ := filepath.Rel(basePath, targetDir)
// check if there's a params.json in the folder
if _, err := os.Stat(targetDir + "/params.jsonnet"); err == nil {
results = append(results, targetDir+"/params.jsonnet")
}
// stop if we're in the basePath
if rel == "." {
break
}
// next!
targetDir += "/.."
}
// jsonnet's import order matters, so we need to reverse the slice
last := len(results) - 1
for i := 0; i < len(results)/2; i++ {
results[i], results[last-i] = results[last-i], results[i]
}
return results
}
// only render cluster params (_cluster), without components
func renderClusterParamsOnly(cmd *cobra.Command, clusterName string, clusterParams string, prune bool) string {
var params []string
if clusterName != "" {
clusterPath := getCluster(clusterDir, clusterName)
params = getClusterParams(clusterDir, clusterPath)
}
if clusterParams != "" {
params = append(params, clusterParams)
}
renderedParams := renderJsonnet(cmd, params, "._cluster", prune, "", "clusterparams")
return renderedParams
}
// render cluster params, merged with one or more component's parameters. Empty componentName list renders all component parameters
func renderClusterParams(cmd *cobra.Command, clusterName string, componentNames []string, clusterParams string, prune bool) string {
if clusterName == "" && clusterParams == "" {
log.Fatal().Msg("Please specify a --cluster name and/or --clusterparams")
}
var params []string
var componentMap map[string]componentDef
if clusterName != "" {
clusterPath := getCluster(clusterDir, clusterName)
params = getClusterParams(clusterDir, clusterPath)
}
if clusterParams != "" {
params = append(params, clusterParams)
}
compParams := renderJsonnet(cmd, params, "", true, "", "clusterparams")
compString := gjson.Get(compParams, "_components")
err := json.Unmarshal([]byte(compString.String()), &componentMap)
if err != nil {
log.Fatal().Err(err).Msg("failed to parse component map")
}
componentDefaultsMerged := "{"
if len(componentNames) > 0 {
// we are passed a list of components
for _, key := range componentNames {
if value, ok := componentMap[key]; ok {
path := baseDir + "/" + value.Path + "/params.jsonnet"
filec, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal().Err(err).Msg("Error reading " + path)
}
componentDefaultsMerged = componentDefaultsMerged + fmt.Sprintf("'%s': %s,", key, string(filec))
}
}
} else {
// all components
for key, value := range componentMap {
if componentName != "" && key != componentName {
continue
}
path := baseDir + "/" + value.Path + "/params.jsonnet"
filec, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal().Err(err).Msg("Error reading " + path)
}
componentDefaultsMerged = componentDefaultsMerged + fmt.Sprintf("'%s': %s,", key, string(filec))
}
}
componentDefaultsMerged = componentDefaultsMerged + "}"
compParams = renderJsonnet(cmd, params, "", prune, componentDefaultsMerged, "componentparams")
return compParams
}