-
Notifications
You must be signed in to change notification settings - Fork 785
/
schema_template.go
87 lines (78 loc) · 2.87 KB
/
schema_template.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
package surveyutils
import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
"text/template"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/util"
"k8s.io/helm/pkg/chartutil"
"github.com/pkg/errors"
)
// TemplateSchemaFile if there is a template for the schema file then evaluate it and write the schema file
func TemplateSchemaFile(schemaFileName string, requirements *config.RequirementsConfig) error {
templateFile := strings.TrimSuffix(schemaFileName, ".schema.json") + ".tmpl.schema.json"
exists, err := util.FileExists(templateFile)
if err != nil {
return err
}
if !exists {
return nil
}
data, err := ReadSchemaTemplate(templateFile, requirements)
if err != nil {
return errors.Wrapf(err, "failed to render schema template %s", templateFile)
}
err = ioutil.WriteFile(schemaFileName, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save schema file %s generated from template %s", schemaFileName, templateFile)
}
log.Logger().Infof("generated schema file %s from template %s\n", util.ColorInfo(schemaFileName), util.ColorInfo(templateFile))
return nil
}
func validateRequirements(requirements *config.RequirementsConfig) {
if requirements.Cluster.GitKind == "" {
requirements.Cluster.GitKind = "github"
}
if requirements.Cluster.GitServer == "" {
switch requirements.Cluster.GitKind {
case "bitbucketcloud":
requirements.Cluster.GitServer = "https://bitbucket.org"
case "github":
requirements.Cluster.GitServer = "https://github.com"
case "gitlab":
requirements.Cluster.GitServer = "https://gitlab.com"
}
}
}
// readSchemaTemplate evaluates the given go template file and returns the output data
func ReadSchemaTemplate(templateFile string, requirements *config.RequirementsConfig) ([]byte, error) {
_, name := filepath.Split(templateFile)
funcMap := helm.NewFunctionMap()
tmpl, err := template.New(name).Option("missingkey=error").Funcs(funcMap).ParseFiles(templateFile)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse schema template: %s", templateFile)
}
validateRequirements(requirements)
requirementsMap, err := requirements.ToMap()
if err != nil {
return nil, errors.Wrapf(err, "failed turn requirements into a map: %v", requirements)
}
templateData := map[string]interface{}{
"GitKind": requirements.Cluster.GitKind,
"GitServer": requirements.Cluster.GitServer,
"GithubApp": requirements.GithubApp != nil && requirements.GithubApp.Enabled,
"Requirements": chartutil.Values(requirementsMap),
"Environments": chartutil.Values(requirements.EnvironmentMap()),
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, templateData)
if err != nil {
return nil, errors.Wrapf(err, "failed to execute schema template: %s", templateFile)
}
data := buf.Bytes()
return data, nil
}