Skip to content

Commit

Permalink
utilizeLoad
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff <[email protected]>
  • Loading branch information
schristoff committed Sep 19, 2024
1 parent a55e52f commit fc4fe01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
42 changes: 19 additions & 23 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ import (
"runtime"
"strings"

"github.com/zarf-dev/zarf/src/cmd/common"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"

"oras.land/oras-go/v2/registry"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/spf13/cobra"
Expand All @@ -39,9 +29,13 @@ import (
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager"
<<<<<<< HEAD
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
)

var packageCmd = &cobra.Command{
Use: "package",
Aliases: []string{"p"},
Short: lang.CmdPackageShort,
Expand Down Expand Up @@ -195,31 +189,33 @@ var packageInspectCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) error {
packageSource, err := choosePackage(args)
src, err := choosePackage(args)
if err != nil {
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource

pkgClient, err := packager.New(&pkgConfig)
loadOpt := packager2.LoadOptions{
Source: src,
Shasum: pkgConfig.PkgOpts.Shasum,
PublicKeyPath: pkgConfig.PkgOpts.PublicKeyPath,
SkipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
}

pkgPaths, err := packager2.LoadPackage(cmd.Context(), loadOpt)
if err != nil {
return err
}
defer pkgClient.ClearTempPaths()
defer os.RemoveAll(pkgPaths.Base)

options := packager2.ZarfInspectOptions{
inspectOpt := packager2.ZarfInspectOptions{
PackagePaths: pkgPaths,
ListImages: pkgConfig.InspectOpts.ListImages,
ViewSBOM: pkgConfig.InspectOpts.ViewSBOM,
SBOMOutputDir: pkgConfig.InspectOpts.SBOMOutputDir,
}

src, err := sources.New(&pkgConfig.PkgOpts)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}

if pkgConfig.InspectOpts.ListImages {
output, err := packager2.InspectList(cmd.Context(), src, pkgClient.Layout, options)
output, err := packager2.InspectList(cmd.Context(), inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
Expand All @@ -228,7 +224,7 @@ var packageInspectCmd = &cobra.Command{
}
}

output, err := packager2.Inspect(cmd.Context(), src, pkgClient.Layout, options)
output, err := packager2.Inspect(cmd.Context(), inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
Expand Down
20 changes: 9 additions & 11 deletions src/pkg/packager2/inspect.go → src/internal/packager2/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/internal/packager/sbom"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
)

// ZarfInspectOptions tracks the user-defined preferences during a package inspection.
type ZarfInspectOptions struct {

// PackagePaths
PackagePaths *layout.PackagePaths
// View SBOM contents while inspecting the package
ViewSBOM bool
// Location to output an SBOM into after package inspection
Expand All @@ -27,15 +27,15 @@ type ZarfInspectOptions struct {
}

// Inspect list the contents of a package.
func Inspect(ctx context.Context, src sources.PackageSource, layout *layout.PackagePaths, options ZarfInspectOptions) (v1alpha1.ZarfPackage, error) {
func Inspect(ctx context.Context, options ZarfInspectOptions) (v1alpha1.ZarfPackage, error) {
var err error
pkg, err := getPackageMetadata(ctx, src, layout, options.ViewSBOM, options.SBOMOutputDir)
pkg, err := getPackageMetadata(ctx, options.PackagePaths)
if err != nil {
return pkg, err
}

if getSBOM(options.ViewSBOM, options.SBOMOutputDir) {
err = handleSBOMOptions(layout, pkg, options.ViewSBOM, options.SBOMOutputDir)
err = handleSBOMOptions(options.PackagePaths, pkg, options.ViewSBOM, options.SBOMOutputDir)
if err != nil {
return pkg, err
}
Expand All @@ -45,9 +45,9 @@ func Inspect(ctx context.Context, src sources.PackageSource, layout *layout.Pack
}

// InspectList lists the images in a component action
func InspectList(ctx context.Context, src sources.PackageSource, layout *layout.PackagePaths, options ZarfInspectOptions) ([]string, error) {
func InspectList(ctx context.Context, options ZarfInspectOptions) ([]string, error) {
var imageList []string
pkg, err := getPackageMetadata(ctx, src, layout, options.ViewSBOM, options.SBOMOutputDir)
pkg, err := getPackageMetadata(ctx, options.PackagePaths)
if err != nil {
return nil, err
}
Expand All @@ -66,10 +66,8 @@ func InspectList(ctx context.Context, src sources.PackageSource, layout *layout.
return imageList, err
}

func getPackageMetadata(ctx context.Context, src sources.PackageSource, layout *layout.PackagePaths, viewSBOM bool, SBOMOutputDir string) (v1alpha1.ZarfPackage, error) {
SBOM := getSBOM(viewSBOM, SBOMOutputDir)

pkg, _, err := src.LoadPackageMetadata(ctx, layout, SBOM, true)
func getPackageMetadata(_ context.Context, layout *layout.PackagePaths) (v1alpha1.ZarfPackage, error) {
pkg, _, err := layout.ReadZarfYAML()
if err != nil {
return pkg, err
}
Expand Down

0 comments on commit fc4fe01

Please sign in to comment.