-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtemplateutils.go
More file actions
97 lines (84 loc) · 2.95 KB
/
templateutils.go
File metadata and controls
97 lines (84 loc) · 2.95 KB
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
package utils
import (
"encoding/json"
"os"
"strings"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)
const pathErrorSuffixMsg = " please enter a path, in which the new template file will be created"
type TemplateUserCommand interface {
// Returns the file path.
TemplatePath() string
// Returns vars to replace in the template content.
Vars() string
}
func ConvertTemplateToMap(templateUserCommand TemplateUserCommand) (map[string]interface{}, error) {
// Read the template file
content, err := fileutils.ReadFile(templateUserCommand.TemplatePath())
if errorutils.CheckError(err) != nil {
return nil, err
}
// Replace vars string-by-string if needed
if len(templateUserCommand.Vars()) > 0 {
templateVars := coreutils.SpecVarsStringToMap(templateUserCommand.Vars())
content = coreutils.ReplaceVars(content, templateVars)
}
// Unmarshal template to a map
var configMap map[string]interface{}
err = json.Unmarshal(content, &configMap)
return configMap, errorutils.CheckError(err)
}
func ConvertTemplateToMaps(templateUserCommand TemplateUserCommand) (interface{}, error) {
content, err := fileutils.ReadFile(templateUserCommand.TemplatePath())
if err != nil {
return nil, errorutils.CheckError(err)
}
// Replace vars string-by-string if needed
if len(templateUserCommand.Vars()) > 0 {
templateVars := coreutils.SpecVarsStringToMap(templateUserCommand.Vars())
content = coreutils.ReplaceVars(content, templateVars)
}
var multiRepoCreateEntities []map[string]interface{}
err = json.Unmarshal(content, &multiRepoCreateEntities)
if err == nil {
return multiRepoCreateEntities, nil
}
if _, ok := err.(*json.SyntaxError); ok {
return nil, errorutils.CheckError(err)
}
var repoCreateEntity map[string]interface{}
err = json.Unmarshal(content, &repoCreateEntity)
if err == nil {
return repoCreateEntity, nil
}
return nil, errorutils.CheckError(err)
}
func ValidateMapEntry(key string, value interface{}, writersMap map[string]ioutils.AnswerWriter) error {
if _, ok := writersMap[key]; !ok {
return errorutils.CheckErrorf("template syntax error: unknown key: \"%s\".", key)
}
if _, ok := value.(string); !ok {
return errorutils.CheckErrorf("template syntax error: the value for the key: \"%s\" is not a string type.", key)
}
return nil
}
func ValidateTemplatePath(templatePath string) error {
exists, err := fileutils.IsDirExists(templatePath, false)
if err != nil {
return errorutils.CheckError(err)
}
if exists || strings.HasSuffix(templatePath, string(os.PathSeparator)) {
return errorutils.CheckErrorf("path cannot be a directory," + pathErrorSuffixMsg)
}
exists, err = fileutils.IsFileExists(templatePath, false)
if err != nil {
return errorutils.CheckError(err)
}
if exists {
return errorutils.CheckErrorf("file already exists," + pathErrorSuffixMsg)
}
return nil
}