Skip to content

Commit

Permalink
refactor: add error to packager set paths
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Jul 23, 2024
1 parent 05ef439 commit 85eafeb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,22 @@ func (pp *PackagePaths) AddSBOMs() *PackagePaths {
}

// SetFromLayers maps layers to package paths.
func (pp *PackagePaths) SetFromLayers(layers []ocispec.Descriptor) {
func (pp *PackagePaths) SetFromLayers(layers []ocispec.Descriptor) error {
paths := []string{}
for _, layer := range layers {
if layer.Annotations[ocispec.AnnotationTitle] != "" {
paths = append(paths, layer.Annotations[ocispec.AnnotationTitle])
}
}
pp.SetFromPaths(paths)
err := pp.SetFromPaths(paths)
if err != nil {
return err
}
return nil
}

// SetFromPaths maps paths to package paths.
func (pp *PackagePaths) SetFromPaths(paths []string) {
func (pp *PackagePaths) SetFromPaths(paths []string) error {
for _, rel := range paths {
// Convert from the standard '/' to the OS path separator for Windows support
switch path := filepath.FromSlash(rel); {
Expand Down Expand Up @@ -310,9 +314,10 @@ func (pp *PackagePaths) SetFromPaths(paths []string) {
}
pp.Components.Tarballs[componentName] = filepath.Join(pp.Base, path)
default:
message.Debug("ignoring path", path)
return fmt.Errorf("unknown path %s", path)
}
}
return nil
}

// Files returns a map of all the files in the package.
Expand Down
6 changes: 4 additions & 2 deletions src/pkg/layout/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func TestPackageFiles(t *testing.T) {
normalizePath("images/oci-layout"),
normalizePath("images/blobs/sha256/" + strings.Repeat("1", 64)),
}
pp.SetFromPaths(paths)
err := pp.SetFromPaths(paths)
require.NoError(t, err)

files := pp.Files()
expected := map[string]string{
Expand Down Expand Up @@ -150,7 +151,8 @@ func TestPackageFiles(t *testing.T) {
},
}
pp.AddImages()
pp.SetFromLayers(descs)
err := pp.SetFromLayers(descs)
require.NoError(t, err)

files := pp.Files()
expected := map[string]string{
Expand Down
15 changes: 12 additions & 3 deletions src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (s *OCISource) LoadPackage(ctx context.Context, dst *layout.PackagePaths, f
if err != nil {
return pkg, nil, fmt.Errorf("unable to pull the package: %w", err)
}
dst.SetFromLayers(layersFetched)
err = dst.SetFromLayers(layersFetched)
if err != nil {
return types.ZarfPackage{}, nil, err
}

if err := dst.MigrateLegacy(); err != nil {
return pkg, nil, err
Expand Down Expand Up @@ -119,7 +122,10 @@ func (s *OCISource) LoadPackageMetadata(ctx context.Context, dst *layout.Package
if err != nil {
return pkg, nil, err
}
dst.SetFromLayers(layersFetched)
err = dst.SetFromLayers(layersFetched)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down Expand Up @@ -174,7 +180,10 @@ func (s *OCISource) Collect(ctx context.Context, dir string) (string, error) {
}

loaded := layout.New(tmp)
loaded.SetFromLayers(fetched)
err = loaded.SetFromLayers(fetched)
if err != nil {
return "", err
}

var pkg types.ZarfPackage

Expand Down
10 changes: 8 additions & 2 deletions src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths,
return pkg, nil, err
}

dst.SetFromPaths(pathsExtracted)
err = dst.SetFromPaths(pathsExtracted)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down Expand Up @@ -161,7 +164,10 @@ func (s *TarballSource) LoadPackageMetadata(_ context.Context, dst *layout.Packa
}
}

dst.SetFromPaths(pathsExtracted)
err = dst.SetFromPaths(pathsExtracted)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down

0 comments on commit 85eafeb

Please sign in to comment.