diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index b18b423..ac43047 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -19,9 +19,6 @@ import ( "context" "errors" "fmt" - "io/fs" - "os" - "path/filepath" "reflect" "strings" @@ -1149,50 +1146,6 @@ func findCloneTask(pr *api.PackageRevision) *api.Task { return nil } -func writeResourcesToDirectory(dir string, resources repository.PackageResources) error { - for k, v := range resources.Contents { - p := filepath.Join(dir, k) - dir := filepath.Dir(p) - if err := os.MkdirAll(dir, 0755); err != nil { - return fmt.Errorf("failed to create directory %q: %w", dir, err) - } - if err := os.WriteFile(p, []byte(v), 0644); err != nil { - return fmt.Errorf("failed to write file %q: %w", dir, err) - } - } - return nil -} - -func loadResourcesFromDirectory(dir string) (repository.PackageResources, error) { - // TODO: return abstraction instead of loading everything - result := repository.PackageResources{ - Contents: map[string]string{}, - } - if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() { - return nil - } - rel, err := filepath.Rel(dir, path) - if err != nil { - return fmt.Errorf("cannot compute relative path %q, %q, %w", dir, path, err) - } - - contents, err := os.ReadFile(path) - if err != nil { - return fmt.Errorf("cannot read file %q: %w", dir, err) - } - result.Contents[rel] = string(contents) - return nil - }); err != nil { - return repository.PackageResources{}, err - } - - return result, nil -} - type mutationReplaceResources struct { newResources *api.PackageRevisionResources oldResources *api.PackageRevisionResources diff --git a/pkg/engine/update.go b/pkg/engine/update.go index d7405d4..633f02b 100644 --- a/pkg/engine/update.go +++ b/pkg/engine/update.go @@ -16,6 +16,8 @@ package engine import ( "context" + "fmt" + "io/fs" "os" "path/filepath" @@ -91,3 +93,47 @@ func (m *defaultPackageUpdater) do(_ context.Context, localPkgDir, originalPkgDi return nil } + +func writeResourcesToDirectory(dir string, resources repository.PackageResources) error { + for k, v := range resources.Contents { + p := filepath.Join(dir, k) + dir := filepath.Dir(p) + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("failed to create directory %q: %w", dir, err) + } + if err := os.WriteFile(p, []byte(v), 0644); err != nil { + return fmt.Errorf("failed to write file %q: %w", dir, err) + } + } + return nil +} + +func loadResourcesFromDirectory(dir string) (repository.PackageResources, error) { + // TODO: return abstraction instead of loading everything + result := repository.PackageResources{ + Contents: map[string]string{}, + } + if err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + return nil + } + rel, err := filepath.Rel(dir, path) + if err != nil { + return fmt.Errorf("cannot compute relative path %q, %q, %w", dir, path, err) + } + + contents, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("cannot read file %q: %w", dir, err) + } + result.Contents[rel] = string(contents) + return nil + }); err != nil { + return repository.PackageResources{}, err + } + + return result, nil +}