From ed1fcf22b23951fdeab1451a14d909ef3030b19b Mon Sep 17 00:00:00 2001 From: joe miller Date: Wed, 31 Jan 2024 00:53:37 +0000 Subject: [PATCH] fix: --path flag in update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recent changes have resulted in the `wolfictl update --path` flag breaking. This flag allows for working with a repo where the melange yaml's are in a subdir such as `./packages` of the repo. > ℹ️ | 2024/01/31 00:40:50 wolfictl update: attempt 1: failed to update packages in git repository: failed to parse open docker-compose.yaml: no such file or directory --- pkg/update/update.go | 14 +++++++++----- pkg/yam/yam.go | 8 +++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/update/update.go b/pkg/update/update.go index 7ce7b0b1..7a3a0566 100644 --- a/pkg/update/update.go +++ b/pkg/update/update.go @@ -369,7 +369,7 @@ func (o *Options) updateGitPackage(ctx context.Context, repo *git.Repository, pa root := worktree.Filesystem.Root() log.Printf("working directory: %s", root) - configFile := filepath.Join(root, pc.Filename) + configFile := filepath.Join(pc.Dir, pc.Filename) if configFile == "" { return "", fmt.Errorf("no config filename found for package %s", packageName) } @@ -416,7 +416,7 @@ func (o *Options) updateGitPackage(ctx context.Context, repo *git.Repository, pa } // now make sure update config is configured - updated, err := config.ParseConfiguration(ctx, filepath.Join(root, pc.Filename)) + updated, err := config.ParseConfiguration(ctx, filepath.Join(pc.Dir, pc.Filename)) if err != nil { return "", fmt.Errorf("failed to parse %v", err) } @@ -435,7 +435,7 @@ func (o *Options) updateGitPackage(ctx context.Context, repo *git.Repository, pa // Skip any processing for definitions with a single pipeline if len(updated.Pipeline) > 1 && deps.ContainsGoBumpPipeline(updated) { - if err := o.updateGoBumpDeps(updated, root, pc.Filename, mutations); err != nil { + if err := o.updateGoBumpDeps(updated, pc.Dir, pc.Filename, mutations); err != nil { return fmt.Sprintf("error cleaning up go/bump deps: %v", err), nil } } @@ -447,12 +447,16 @@ func (o *Options) updateGitPackage(ctx context.Context, repo *git.Repository, pa o.Logger.Printf("after clean go bumps: %s git status: %s", packageName, string(rs)) // Run yam formatter - err = yam.FormatConfigurationFile(root, pc.Filename) + err = yam.FormatConfigurationFile(root, filepath.Join(pc.Dir, pc.Filename)) if err != nil { return fmt.Sprintf("failed to format configuration file: %v", err), nil } - _, err = worktree.Add(pc.Filename) + relativeFilename, err := filepath.Rel(root, filepath.Join(pc.Dir, pc.Filename)) + if err != nil { + return "", fmt.Errorf("failed to get relative path from dir %s and file %s: %v", pc.Dir, pc.Filename, err) + } + _, err = worktree.Add(relativeFilename) if err != nil { return "", fmt.Errorf("failed to git add %s: %w", configFile, err) } diff --git a/pkg/yam/yam.go b/pkg/yam/yam.go index fa3d7d39..3ed51538 100644 --- a/pkg/yam/yam.go +++ b/pkg/yam/yam.go @@ -22,9 +22,15 @@ func FormatConfigurationFile(dir, filename string) error { return fmt.Errorf("failed to read yam config file: %v", err) } + // remove root dir from filename + relativeFilename, err := filepath.Rel(dir, filename) + if err != nil { + return fmt.Errorf("failed to get relative path from dir %s and file %s: %v", dir, filename, err) + } + fsys := osAdapter.DirFS(dir) // Format file following the repo level format - err = yam.Format(fsys, []string{filename}, yam.FormatOptions{EncodeOptions: *encodeOptions}) + err = yam.Format(fsys, []string{relativeFilename}, yam.FormatOptions{EncodeOptions: *encodeOptions}) if err != nil { return fmt.Errorf("error formatting the file %s: %v", filename, err) }