diff --git a/site/src/content/docs/commands/zarf_package_inspect.md b/site/src/content/docs/commands/zarf_package_inspect.md index def3845bb1..7a27daff9f 100644 --- a/site/src/content/docs/commands/zarf_package_inspect.md +++ b/site/src/content/docs/commands/zarf_package_inspect.md @@ -22,6 +22,7 @@ zarf package inspect [ PACKAGE_SOURCE ] [flags] ``` -h, --help help for inspect + --list-images List images in the package (prints to stdout) -s, --sbom View SBOM contents while inspecting the package --sbom-out string Specify an output directory for the SBOMs from the inspected Zarf package ``` diff --git a/src/cmd/package.go b/src/cmd/package.go index e3fb00b48a..a0f124a864 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -422,6 +422,7 @@ func bindInspectFlags(_ *viper.Viper) { inspectFlags := packageInspectCmd.Flags() inspectFlags.BoolVarP(&pkgConfig.InspectOpts.ViewSBOM, "sbom", "s", false, lang.CmdPackageInspectFlagSbom) inspectFlags.StringVar(&pkgConfig.InspectOpts.SBOMOutputDir, "sbom-out", "", lang.CmdPackageInspectFlagSbomOut) + inspectFlags.BoolVar(&pkgConfig.InspectOpts.ListImages, "list-images", false, lang.CmdPackageInspectFlagListImages) } func bindRemoveFlags(v *viper.Viper) { diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 1d1847d207..224e269166 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -308,9 +308,10 @@ $ zarf package mirror-resources \ CmdPackageMirrorFlagComponents = "Comma-separated list of components to mirror. This list will be respected regardless of a component's 'required' or 'default' status. Globbing component names with '*' and deselecting components with a leading '-' are also supported." CmdPackageMirrorFlagNoChecksum = "Turns off the addition of a checksum to image tags (as would be used by the Zarf Agent) while mirroring images." - CmdPackageInspectFlagSbom = "View SBOM contents while inspecting the package" - CmdPackageInspectFlagSbomOut = "Specify an output directory for the SBOMs from the inspected Zarf package" - CmdPackageInspectErr = "Failed to inspect package: %s" + CmdPackageInspectFlagSbom = "View SBOM contents while inspecting the package" + CmdPackageInspectFlagSbomOut = "Specify an output directory for the SBOMs from the inspected Zarf package" + CmdPackageInspectFlagListImages = "List images in the package (prints to stdout)" + CmdPackageInspectErr = "Failed to inspect package: %s" CmdPackageRemoveShort = "Removes a Zarf package that has been deployed already (runs offline)" CmdPackageRemoveFlagConfirm = "REQUIRED. Confirm the removal action to prevent accidental deletions" diff --git a/src/pkg/packager/inspect.go b/src/pkg/packager/inspect.go index 6a6cc7fac9..56645e2d54 100644 --- a/src/pkg/packager/inspect.go +++ b/src/pkg/packager/inspect.go @@ -5,6 +5,10 @@ package packager import ( + "fmt" + "os" + + "github.com/defenseunicorns/pkg/helpers" "github.com/defenseunicorns/zarf/src/internal/packager/sbom" "github.com/defenseunicorns/zarf/src/pkg/utils" ) @@ -18,7 +22,18 @@ func (p *Packager) Inspect() (err error) { return err } - utils.ColorPrintYAML(p.cfg.Pkg, nil, false) + if p.cfg.InspectOpts.ListImages { + imageList := []string{} + for _, component := range p.cfg.Pkg.Components { + imageList = append(imageList, component.Images...) + } + imageList = helpers.Unique(imageList) + for _, image := range imageList { + fmt.Fprintln(os.Stdout, "-", image) + } + } else { + utils.ColorPrintYAML(p.cfg.Pkg, nil, false) + } sbomDir := p.layout.SBOMs.Path diff --git a/src/test/e2e/06_create_sbom_test.go b/src/test/e2e/06_create_sbom_test.go index 9a6027bb14..d040ba2e05 100644 --- a/src/test/e2e/06_create_sbom_test.go +++ b/src/test/e2e/06_create_sbom_test.go @@ -41,6 +41,10 @@ func TestCreateSBOM(t *testing.T) { _, err = os.ReadFile(filepath.Join(sbomPath, "dos-games", "docker.io_defenseunicorns_zarf-game_multi-tile-dark.json")) require.NoError(t, err) + stdOut, _, err = e2e.Zarf("package", "inspect", pkgName, "--list-images") + require.NoError(t, err) + require.Equal(t, "- defenseunicorns/zarf-game:multi-tile-dark\n", stdOut) + // Pull the current zarf binary version to find the corresponding init package version, stdErr, err := e2e.Zarf("version") require.NoError(t, err, version, stdErr) diff --git a/src/types/runtime.go b/src/types/runtime.go index 3d9ea5d09a..24a466926e 100644 --- a/src/types/runtime.go +++ b/src/types/runtime.go @@ -38,8 +38,12 @@ type ZarfPackageOptions struct { // ZarfInspectOptions tracks the user-defined preferences during a package inspection. type ZarfInspectOptions struct { - ViewSBOM bool `json:"sbom" jsonschema:"description=View SBOM contents while inspecting the package"` - SBOMOutputDir string `json:"sbomOutput" jsonschema:"description=Location to output an SBOM into after package inspection"` + // View SBOM contents while inspecting the package + ViewSBOM bool + // Location to output an SBOM into after package inspection + SBOMOutputDir string + // ListImages will list the images in the package + ListImages bool } // ZarfFindImagesOptions tracks the user-defined preferences during a prepare find-images search.