Skip to content

Commit

Permalink
Merge pull request #137 from Nordix/move-functions
Browse files Browse the repository at this point in the history
Move functions
  • Loading branch information
efiacor authored Nov 14, 2024
2 parents a343bce + f511ea0 commit 6af2c49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
47 changes: 0 additions & 47 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions pkg/engine/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package engine

import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -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
}

0 comments on commit 6af2c49

Please sign in to comment.