Skip to content

Commit

Permalink
NewOpts functions
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Jan 22, 2025
1 parent ffa438a commit 836dc61
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewPackageCommand() *cobra.Command {
cmd.AddCommand(NewPackageCreateCommand(v))
cmd.AddCommand(NewPackageDeployCommand(v))
cmd.AddCommand(NewPackageMirrorResourcesCommand(v))
cmd.AddCommand(NewPackageInspectCommand(v))
cmd.AddCommand(NewPackageInspectCommand())
cmd.AddCommand(NewPackageRemoveCommand(v))
cmd.AddCommand(NewPackageListCommand())
cmd.AddCommand(NewPackagePublishCommand(v))
Expand Down Expand Up @@ -345,7 +345,7 @@ func (o *PackageMirrorResourcesOptions) Run(cmd *cobra.Command, args []string) (
type PackageInspectOptions struct{}

// NewPackageInspectCommand creates the `package inspect` sub-command.
func NewPackageInspectCommand(v *viper.Viper) *cobra.Command {
func NewPackageInspectCommand() *cobra.Command {
o := &PackageInspectOptions{}
cmd := &cobra.Command{
Use: "inspect [ PACKAGE_SOURCE ]",
Expand All @@ -357,7 +357,7 @@ func NewPackageInspectCommand(v *viper.Viper) *cobra.Command {
RunE: o.Run,
}

cmd.AddCommand(NewPackageInspectSBOMCommand(v))
cmd.AddCommand(NewPackageInspectSBOMCommand())
cmd.AddCommand(NewPackageInspectImagesCommand())
cmd.AddCommand(NewPackageInspectDefinitionCommand())

Expand All @@ -381,14 +381,14 @@ func (o *PackageInspectOptions) Run(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
logger.From(ctx).Warn("Direct usage of inspect is deprecated and will be removed in a future release. Inspect is now a parent command. Use 'zarf package inspect definition|sbom|images' instead.")

if pkgConfig.InspectOpts.ListImages && (pkgConfig.InspectOpts.SBOMOutputDir != "") {
return fmt.Errorf("cannot use --sbom or --sbom-out and --list-images at the same time")
if pkgConfig.InspectOpts.ListImages && pkgConfig.InspectOpts.SBOMOutputDir != "" {
return fmt.Errorf("cannot use --sbom-out and --list-images at the same time")
}

if pkgConfig.InspectOpts.SBOMOutputDir != "" {
sbomOpts := PackageInspectSBOMOptions{
SkipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
SBOMOutputDir: pkgConfig.InspectOpts.SBOMOutputDir,
skipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
outputDir: pkgConfig.InspectOpts.SBOMOutputDir,
}
return sbomOpts.Run(cmd, args)
}
Expand All @@ -408,22 +408,29 @@ func (o *PackageInspectOptions) Run(cmd *cobra.Command, args []string) error {

// PackageInspectSBOMOptions holds the command-line options for 'package inspect sbom' sub-command.
type PackageInspectSBOMOptions struct {
SkipSignatureValidation bool
SBOMOutputDir string
skipSignatureValidation bool
outputDir string
}

func newPackageInspectSBOMOptions() *PackageInspectSBOMOptions {
return &PackageInspectSBOMOptions{
outputDir: "",
skipSignatureValidation: false,
}
}

// NewPackageInspectSBOMCommand creates the `package inspect sbom` sub-command.
func NewPackageInspectSBOMCommand(v *viper.Viper) *cobra.Command {
o := &PackageInspectSBOMOptions{}
func NewPackageInspectSBOMCommand() *cobra.Command {
o := newPackageInspectSBOMOptions()
cmd := &cobra.Command{
Use: "sbom [ PACKAGE ]",
Short: "Output the package SBOM (Software Bill Of Materials) to the specified directory",
Args: cobra.MaximumNArgs(1),
RunE: o.Run,
}

cmd.Flags().BoolVar(&o.SkipSignatureValidation, "skip-signature-validation", false, lang.CmdPackageFlagSkipSignatureValidation)
cmd.Flags().StringVar(&o.SBOMOutputDir, "output", v.GetString(common.VPkgCreateSbomOutput), lang.CmdPackageCreateFlagSbomOut)
cmd.Flags().BoolVar(&o.skipSignatureValidation, "skip-signature-validation", o.skipSignatureValidation, lang.CmdPackageFlagSkipSignatureValidation)
cmd.Flags().StringVar(&o.outputDir, "output", o.outputDir, lang.CmdPackageCreateFlagSbomOut)

return cmd
}
Expand All @@ -437,21 +444,22 @@ func (o *PackageInspectSBOMOptions) Run(cmd *cobra.Command, args []string) error
}
loadOpt := packager2.LoadOptions{
Source: src,
SkipSignatureValidation: o.SkipSignatureValidation,
SkipSignatureValidation: o.skipSignatureValidation,
Filter: filters.Empty(),
PublicKeyPath: pkgConfig.PkgOpts.PublicKeyPath,
}
layout, err := packager2.LoadPackage(ctx, loadOpt)
if err != nil {
return err
}
outputPath, err := layout.GetSBOM(o.SBOMOutputDir)
outputPath, err := layout.GetSBOM(o.outputDir)
if err != nil {
return err
}
outputPath, err = filepath.Abs(outputPath)
if err != nil {
logger.From(ctx).Warn("SBOM successfully extracted, couldn't get output path", "error", err)
return nil
}
logger.From(ctx).Info("SBOM successfully extracted", "path", outputPath)
return nil
Expand All @@ -462,17 +470,23 @@ type PackageInspectImagesOptions struct {
SkipSignatureValidation bool
}

func newPackageInspectImagesOptions() *PackageInspectImagesOptions {
return &PackageInspectImagesOptions{
SkipSignatureValidation: false,
}
}

// NewPackageInspectImagesCommand creates the `inspect images` sub-command.
func NewPackageInspectImagesCommand() *cobra.Command {
o := PackageInspectImagesOptions{}
o := newPackageInspectImagesOptions()
cmd := &cobra.Command{
Use: "images [ PACKAGE_SOURCE ]",
Short: "List all container images contained in the package",
Args: cobra.MaximumNArgs(1),
RunE: o.Run,
}

cmd.Flags().BoolVar(&o.SkipSignatureValidation, "skip-signature-validation", false, lang.CmdPackageFlagSkipSignatureValidation)
cmd.Flags().BoolVar(&o.SkipSignatureValidation, "skip-signature-validation", o.SkipSignatureValidation, lang.CmdPackageFlagSkipSignatureValidation)

return cmd
}
Expand All @@ -486,6 +500,8 @@ func (o *PackageInspectImagesOptions) Run(cmd *cobra.Command, args []string) err
return err
}

// The user may be pulling the package from the cluster or using a built package
// since we don't know we don't check this error
cluster, _ := cluster.NewCluster() //nolint:errcheck

pkg, err := packager2.GetPackageFromSourceOrCluster(ctx, cluster, src, o.SkipSignatureValidation, pkgConfig.PkgOpts.PublicKeyPath)
Expand All @@ -501,10 +517,7 @@ func (o *PackageInspectImagesOptions) Run(cmd *cobra.Command, args []string) err
}
imageList = helpers.Unique(imageList)
for _, image := range imageList {
_, err := fmt.Fprintln(os.Stdout, "-", image)
if err != nil {
return err
}
fmt.Println("-", image)
}
return nil
}
Expand All @@ -514,17 +527,23 @@ type PackageInspectDefinitionOptions struct {
SkipSignatureValidation bool
}

func newPackageInspectDefinitionOptions() *PackageInspectDefinitionOptions {
return &PackageInspectDefinitionOptions{
SkipSignatureValidation: false,
}
}

// NewPackageInspectDefinitionCommand creates the `package inspect definition` sub-command.
func NewPackageInspectDefinitionCommand() *cobra.Command {
o := PackageInspectDefinitionOptions{}
o := newPackageInspectDefinitionOptions()
cmd := &cobra.Command{
Use: "definition [ PACKAGE_SOURCE ]",
Short: "Displays the 'zarf.yaml' definition for the specified package",
Args: cobra.MaximumNArgs(1),
RunE: o.Run,
}

cmd.Flags().BoolVar(&o.SkipSignatureValidation, "skip-signature-validation", false, lang.CmdPackageFlagSkipSignatureValidation)
cmd.Flags().BoolVar(&o.SkipSignatureValidation, "skip-signature-validation", o.SkipSignatureValidation, lang.CmdPackageFlagSkipSignatureValidation)

return cmd
}
Expand All @@ -538,6 +557,8 @@ func (o *PackageInspectDefinitionOptions) Run(cmd *cobra.Command, args []string)
return err
}

// The user may be pulling the package from the cluster or using a built package
// since we don't know we don't check this error
cluster, _ := cluster.NewCluster() //nolint:errcheck

pkg, err := packager2.GetPackageFromSourceOrCluster(ctx, cluster, src, o.SkipSignatureValidation, pkgConfig.PkgOpts.PublicKeyPath)
Expand Down

0 comments on commit 836dc61

Please sign in to comment.