Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions artifactory/commands/utils/templateutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ type TemplateUserCommand interface {
Vars() string
}

func ConvertTemplateToMap(tuc TemplateUserCommand) (map[string]interface{}, error) {
func ConvertTemplateToMap(templateUserCommand TemplateUserCommand) (map[string]interface{}, error) {
// Read the template file
content, err := fileutils.ReadFile(tuc.TemplatePath())
content, err := fileutils.ReadFile(templateUserCommand.TemplatePath())
if errorutils.CheckError(err) != nil {
return nil, err
}
// Replace vars string-by-string if needed
if len(tuc.Vars()) > 0 {
templateVars := coreutils.SpecVarsStringToMap(tuc.Vars())
if len(templateUserCommand.Vars()) > 0 {
templateVars := coreutils.SpecVarsStringToMap(templateUserCommand.Vars())
content = coreutils.ReplaceVars(content, templateVars)
}
// Unmarshal template to a map
Expand All @@ -38,6 +38,41 @@ func ConvertTemplateToMap(tuc TemplateUserCommand) (map[string]interface{}, erro
return configMap, errorutils.CheckError(err)
}

func ConvertTemplateToMaps(templateUserCommand TemplateUserCommand) ([]map[string]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)
}

isArray := checkTemplateType(content)

var multiRepoCreateEntities []map[string]interface{}
err = json.Unmarshal(content, &multiRepoCreateEntities)
if err != nil && isArray {
return nil, errorutils.CheckError(err)
} else if err == nil {
return multiRepoCreateEntities, nil
}

var repoCreateEntity map[string]interface{}
err = json.Unmarshal(content, &repoCreateEntity)
if err == nil {
return []map[string]interface{}{repoCreateEntity}, nil
}

return nil, err
}

func checkTemplateType(content []byte) bool {
trimmedContent := strings.TrimSpace(string(content))
return len(trimmedContent) > 1 && trimmedContent[0] == '[' && trimmedContent[len(trimmedContent)-1] == ']'
}

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: \"" + key + "\".")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20250707095624-7062538a0961
replace github.com/jfrog/jfrog-client-go => github.com/reshmifrog/jfrog-client-go v1.28.1-0.20250710101657-612b8a1389d1

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20250611113558-c1a092f216fd

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ github.com/jfrog/build-info-go v1.10.14 h1:PWnw+rBwiQTHZ5q+84+E8MHFjtAQkB3+Oc2sK
github.com/jfrog/build-info-go v1.10.14/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
github.com/jfrog/jfrog-client-go v1.54.2 h1:z7GjCyIbV5Wx35USVDCF/0vf/fCWki++pZ7lcKt0gj0=
github.com/jfrog/jfrog-client-go v1.54.2/go.mod h1:1v0eih4thdPA4clBo9TuvAMT25sGDr1IQJ81DXQ/lBY=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down Expand Up @@ -186,6 +184,8 @@ github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/reshmifrog/jfrog-client-go v1.28.1-0.20250710101657-612b8a1389d1 h1:/wK/zL8yXxZCqQ7YFlBEfUeRoQ7vkYRP4edaioxhggw=
github.com/reshmifrog/jfrog-client-go v1.28.1-0.20250710101657-612b8a1389d1/go.mod h1:1v0eih4thdPA4clBo9TuvAMT25sGDr1IQJ81DXQ/lBY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
Loading