Skip to content

Commit

Permalink
Support directory targets for pr automations (#506)
Browse files Browse the repository at this point in the history
Need to expand the directory recursively and template from there
  • Loading branch information
michaeljguarino authored Apr 22, 2024
1 parent 836c4ae commit 913fe25
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pkg/pr/creates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package pr

import (
"path/filepath"

"github.com/pluralsh/plural-cli/pkg/utils"
)

type replacement struct {
source string
dest string
}

func applyCreates(creates *CreateSpec, ctx map[string]interface{}) error {
if creates == nil {
return nil
Expand All @@ -21,10 +28,30 @@ func applyCreates(creates *CreateSpec, ctx map[string]interface{}) error {
dest = destPath
}

if err := replaceTo(source, string(dest), func(data []byte) ([]byte, error) {
return templateReplacement(data, ctx)
}); err != nil {
return err
replacements := []replacement{{source, string(dest)}}
if utils.IsDir(source) {
files, err := utils.ListDirectory(source)
if err != nil {
return err
}

replacements = []replacement{}
for _, file := range files {
destFile, err := filepath.Rel(source, file)
if err != nil {
return err
}
destFile = filepath.Join(string(dest), destFile)
replacements = append(replacements, replacement{source: file, dest: destFile})
}
}

for _, replacement := range replacements {
if err := replaceTo(replacement.source, replacement.dest, func(data []byte) ([]byte, error) {
return templateReplacement(data, ctx)
}); err != nil {
return err
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ import (
"sigs.k8s.io/yaml"
)

func ListDirectory(dir string) ([]string, error) {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})

return files, err
}

func IsDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}

return fileInfo.IsDir()
}

func CopyFile(src, dest string) error {
bytesRead, err := os.ReadFile(src)
if err != nil {
Expand Down

0 comments on commit 913fe25

Please sign in to comment.