-
Notifications
You must be signed in to change notification settings - Fork 10
/
standard_templates.go
45 lines (37 loc) · 1.16 KB
/
standard_templates.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
package pushtofile
import "fmt"
type standardTemplate struct {
template string
fileFormat string
validateAlias func(alias string) error
}
func (s standardTemplate) ValidateAlias(alias string) error {
if s.validateAlias == nil {
return nil
}
return s.validateAlias(alias)
}
var standardTemplates = map[string]standardTemplate{
"yaml": {template: yamlTemplate, validateAlias: validateYAMLKey},
"json": {template: jsonTemplate, validateAlias: validateJSONKey},
"dotenv": {template: dotenvTemplate, validateAlias: validateBashVarName},
"bash": {template: bashTemplate, validateAlias: validateBashVarName},
}
// FileTemplateForFormat returns the template for a file format, after ensuring the
// standard template exists and validating secret spec aliases against it
func FileTemplateForFormat(
fileFormat string,
secretSpecs []SecretSpec,
) (string, error) {
stdTemplate, ok := standardTemplates[fileFormat]
if !ok {
return "", fmt.Errorf(`unrecognized standard file format, "%s"`, fileFormat)
}
for _, s := range secretSpecs {
err := stdTemplate.ValidateAlias(s.Alias)
if err != nil {
return "", err
}
}
return stdTemplate.template, nil
}