This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
generate.go
66 lines (56 loc) · 1.92 KB
/
generate.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
package tasks
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/KablamoOSS/kombustion/internal/cloudformation"
"github.com/KablamoOSS/kombustion/internal/core"
yaml "github.com/KablamoOSS/yaml"
)
// GenerateTemplate and save it to disk, without upserting it
func GenerateTemplate(params cloudformation.GenerateParams) {
output, _ := GenerateYamlTemplate(params)
// prepareOutputDir(params.Directory)
if params.WriteParams {
writeParamMap(params.ObjectStore, params.Filename, params.Directory, params.ParamMap)
}
writeOutput(params.ObjectStore, params.Filename, params.Directory, output)
}
// GenerateYamlTemplate and return both the raw data as []byte, but also the cloudformation yaml object
func GenerateYamlTemplate(params cloudformation.GenerateParams) ([]byte, cloudformation.YamlCloudformation) {
cf, err := cloudformation.GenerateYamlTemplate(params)
checkError(err)
output, err := yaml.Marshal(cf)
checkError(err)
return output, cf
}
func prepareOutputDir(directory string) {
err := os.Mkdir(directory, 0744)
if !os.IsExist(err) {
checkError(err)
}
}
func writeOutput(store core.ObjectStore, file, directory string, output []byte) {
filename := filepath.Base(file)
basename := strings.TrimSuffix(filename, filepath.Ext(filename))
err := store.Put(output, directory, fmt.Sprint(basename, ".yaml"))
checkError(err)
}
func writeParamMap(store core.ObjectStore, file, directory string, params map[string]string) {
outParams := make([]cloudformation.Parameter, 0)
for key, value := range params {
cfParam := cloudformation.Parameter{
ParameterKey: key,
ParameterValue: value,
}
outParams = append(outParams, cfParam)
}
out, err := json.MarshalIndent(outParams, "", " ")
checkError(err)
filename := filepath.Base(file)
basename := strings.TrimSuffix(filename, filepath.Ext(filename))
err = store.Put(out, directory, fmt.Sprint(basename, "-params.json"))
checkError(err)
}