Skip to content

Commit

Permalink
showoff potential ReadGeneratedZarfYaml
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Aug 28, 2024
1 parent 7826046 commit 59ac803
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package layout

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
Expand All @@ -17,6 +19,7 @@ import (
"github.com/mholt/archiver/v3"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/api/v1beta1"
"github.com/zarf-dev/zarf/src/pkg/interactive"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/deprecated"
Expand Down Expand Up @@ -50,6 +53,31 @@ func New(baseDir string) *PackagePaths {
}
}

// ReadGeneratedZarfYaml reads a zarf package from the a generated zarfv1beta1.yaml file or from a zarf.yaml file and translates it
// This should only be used on generated Zarf packages
func (pp *PackagePaths) ReadGeneratedZarfYaml() (v1beta1.ZarfPackage, error) {
path := filepath.Join(pp.Base, "zarfv1beta1.yaml")
_, err := os.Stat(path)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return v1beta1.ZarfPackage{}, err
}
if errors.Is(err, fs.ErrNotExist) {
alphaPkg, _, err := pp.ReadZarfYAML()
if err != nil {
return v1beta1.ZarfPackage{}, err
}
betaPkg, err := v1beta1.TranslateAlphaPackage(alphaPkg)
if err != nil {
return v1beta1.ZarfPackage{}, err
}
return betaPkg, nil
}

var pkg v1beta1.ZarfPackage
err = utils.ReadYaml(path, &pkg)
return pkg, err
}

// ReadZarfYAML reads a zarf.yaml file into memory,
// checks if it's using the legacy layout, and migrates deprecated component configs.
func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *OCISource) LoadPackageMetadata(ctx context.Context, dst *layout.Package
}
dst.SetFromLayers(layersFetched)

pkg, warnings, err = dst.ReadZarfYAML()
pkg, warnings, err = dst.ReadGeneratedZarfYaml()
if err != nil {
return pkg, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *TarballSource) LoadPackage(ctx context.Context, dst *layout.PackagePath

dst.SetFromPaths(pathsExtracted)

pkg, warnings, err = dst.ReadZarfYAML()
pkg, warnings, err = dst.ReadGeneratedZarfYaml()
if err != nil {
return pkg, nil, err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (s *TarballSource) LoadPackageMetadata(ctx context.Context, dst *layout.Pac

dst.SetFromPaths(pathsExtracted)

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

0 comments on commit 59ac803

Please sign in to comment.