Skip to content

Commit

Permalink
Fix format for filespecs with deleteParent
Browse files Browse the repository at this point in the history
  • Loading branch information
praqma-thi committed Sep 29, 2023
1 parent f8ffe84 commit 458097b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
118 changes: 66 additions & 52 deletions commands/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
"golang.org/x/exp/maps"
)

type ExpandConfiguration struct {
Expand All @@ -28,19 +27,15 @@ type Policy struct {
Entries []map[string]interface{} `json:"entries"`
}

type RepoPath struct {
Repo string
Path string
}

const deleteParentTemplateText = `
{
"files": [
{
"aql": {
"items.find": {
"repo": "{{ .Repo }}",
"$or": [
{{ formatRepoPaths .RepoPaths }}
{{ formatPaths .Paths }}
]
}
}
Expand All @@ -52,13 +47,13 @@ const deleteParentTemplateText = `
var deleteParentTemplate *template.Template

func InitTemplates() {
log.Info("Initializing built-in templates") //Debug?
log.Info("Initializing built-in templates")
deleteParentTemplate = template.Must(template.New("deleteParent").Funcs(template.FuncMap{
"formatRepoPaths": func(repoPaths []RepoPath) string {
"formatPaths": func(paths []string) string {
result := ""
for i, repoPath := range repoPaths {
result += fmt.Sprintf(` { "repo": "%s", "path": "%s" }`, repoPath.Repo, repoPath.Path)
if i < len(repoPaths)-1 {
for i, path := range paths {
result += fmt.Sprintf(` { "path": "%s" }`, path)
if i < len(paths)-1 {
result += ",\n"
}
}
Expand Down Expand Up @@ -152,25 +147,22 @@ func ExpandCmd(context *components.Context) error {
}

// Create output dir if necessary
if dirErr := os.MkdirAll(path.Join(expandConfig.outputPath, policyName), 0755); dirErr != nil {
policyPath := path.Join(expandConfig.outputPath, policyName)
if dirErr := os.MkdirAll(policyPath, 0755); dirErr != nil {
return dirErr
}

// Iterate over policy entries
for index, entry := range policy.Entries {
// Figure out the file name for the result file
fileName := fmt.Sprint(policyName, "-", index, ".json")

// If the policy has a NameProperty, use that for the filename
if policy.NameProperty != "" {
if entry[policy.NameProperty] != "" {
fileName = fmt.Sprint(entry[policy.NameProperty], "-", index, ".json")
if !policy.DeleteParent {
// Figure out the file name for the result file
fileName := fmt.Sprintf("%s-%d.json", policyName, index)
if policy.NameProperty != "" && entry[policy.NameProperty] != "" {
fileName = fmt.Sprintf("%s-%d.json", entry[policy.NameProperty], index)
}
}

if !policy.DeleteParent {
// Create the result file
resultFile, fileErr := os.Create(path.Join(expandConfig.outputPath, policyName, fileName))
resultFile, fileErr := os.Create(path.Join(policyPath, fileName))
if fileErr != nil {
return fileErr
}
Expand All @@ -194,15 +186,15 @@ func ExpandCmd(context *components.Context) error {
defer os.RemoveAll(tmpDir)

// Create and expand the template into a temp file
tempFile, fileErr := os.Create(path.Join(tmpDir, fileName))
tempFile, fileErr := os.Create(path.Join(tmpDir, policyName))
if fileErr != nil {
return fileErr
}
if templatingErr := template.Execute(tempFile, entry); templatingErr != nil {
return templatingErr
}

searchParams, parseErr := ParseSearchParamsFromPath(path.Join(tmpDir, fileName))
searchParams, parseErr := ParseSearchParamsFromPath(path.Join(tmpDir, policyName))
if parseErr != nil {
return parseErr
}
Expand All @@ -213,7 +205,7 @@ func ExpandCmd(context *components.Context) error {
}

// Search for and collect matches' parent paths
var repoPaths []RepoPath
repoPaths := make(map[string][]string)
for _, sp := range searchParams {
reader, searchErr := artifactoryManager.SearchFiles(sp)
if searchErr != nil {
Expand All @@ -226,45 +218,67 @@ func ExpandCmd(context *components.Context) error {
}
}()

pathMap := make(map[RepoPath]struct{}) // Map to avoid duplicates in the parent paths
for currentResult := new(utils.ResultItem); reader.NextRecord(currentResult) == nil; currentResult = new(utils.ResultItem) {
repoPath := RepoPath{Repo: currentResult.Repo, Path: currentResult.Path}
pathMap[repoPath] = struct{}{}
for result := new(utils.ResultItem); reader.NextRecord(result) == nil; result = new(utils.ResultItem) {
if _, exists := repoPaths[result.Repo]; !exists {
repoPaths[result.Repo] = []string{}
}
repoPaths[result.Repo] = append(repoPaths[result.Repo], result.Path)
}
repoPaths = maps.Keys(pathMap)

log.Debug(" Parent paths for [", policyName, "] :")
for _, repoPath := range repoPaths {
log.Debug(" -", repoPath.Repo, "-", repoPath.Path)
for repo, paths := range repoPaths {
log.Debug(" -", repo)
for _, path := range paths {
log.Debug(" -", path)
}
}

if readErr := reader.GetError(); readErr != nil {
return readErr
}
}

if len(repoPaths) <= 0 {
log.Debug("No parent paths found, skipping generating")
continue
}
// Write File Specs for each matched repository
for repo, paths := range repoPaths {
log.Debug("Generating for", repo)
if len(paths) <= 0 {
log.Debug("No parent paths found, skipping generating")
continue
}

// Create the result file
resultFile, fileErr := os.Create(path.Join(expandConfig.outputPath, policyName, fileName))
if fileErr != nil {
return fileErr
}
// Figure out the file name for the result file
fileName := fmt.Sprintf("%s-%d.json", policyName, index)
if policy.NameProperty != "" && entry[policy.NameProperty] != "" {
fileName = fmt.Sprintf("%s-%d.json", entry[policy.NameProperty], index)
}

// Expand the template, dumping it into the result file
data := struct {
RepoPaths []RepoPath
}{
RepoPaths: repoPaths,
}
if templatingErr := deleteParentTemplate.Execute(resultFile, data); templatingErr != nil {
return templatingErr
}
// Create output dir if necessary
log.Debug("DEBUG", path.Join(policyPath, repo))
if dirErr := os.MkdirAll(path.Join(policyPath, repo), 0755); dirErr != nil {
return dirErr
}

log.Debug(" Expanded: ", resultFile.Name())
// Create the result file
resultFile, fileErr := os.Create(path.Join(policyPath, repo, fileName))
if fileErr != nil {
return fileErr
}

// Expand the template, dumping it into the result file
data := struct {
Repo string
Paths []string
}{
Repo: repo,
Paths: paths,
}
if templatingErr := deleteParentTemplate.Execute(resultFile, data); templatingErr != nil {
return templatingErr
}

log.Debug(" Expanded: ", resultFile.Name())

}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/jfrog/jfrog-cli-core/v2 v2.43.1
github.com/jfrog/jfrog-client-go v1.32.3
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)

require (
Expand Down Expand Up @@ -70,6 +69,7 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand Down

0 comments on commit 458097b

Please sign in to comment.