From 4b7e6e4481ae1615edd648a423f029d56a0c604d Mon Sep 17 00:00:00 2001 From: Kit Patella Date: Tue, 29 Oct 2024 08:56:36 -0700 Subject: [PATCH] chore: add logger to packager2 inspect (#3153) Signed-off-by: Kit Patella --- src/cmd/package.go | 4 ++++ src/internal/packager/sbom/tools.go | 7 ++++++- src/internal/packager2/inspect.go | 4 ++-- src/pkg/packager/creator/normal.go | 2 +- src/pkg/packager/inspect.go | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/cmd/package.go b/src/cmd/package.go index 22e866cdbf..c0d441a0f0 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -201,6 +201,7 @@ var packageInspectCmd = &cobra.Command{ } }, RunE: func(cmd *cobra.Command, args []string) error { + // NOTE(mkcp): Gets user input with message src, err := choosePackage(args) if err != nil { return err @@ -234,6 +235,9 @@ var packageInspectCmd = &cobra.Command{ if err != nil { return fmt.Errorf("failed to inspect package: %w", err) } + // HACK(mkcp): This init call ensures we still can still print Yaml when message is disabled. Remove when we + // release structured logged and don't have to disable message globally in pre-run. + message.InitializePTerm(logger.DestinationDefault) err = utils.ColorPrintYAML(output, nil, false) if err != nil { return err diff --git a/src/internal/packager/sbom/tools.go b/src/internal/packager/sbom/tools.go index 972b709906..ae95ac7486 100644 --- a/src/internal/packager/sbom/tools.go +++ b/src/internal/packager/sbom/tools.go @@ -5,7 +5,9 @@ package sbom import ( + "context" "fmt" + "github.com/zarf-dev/zarf/src/pkg/logger" "path/filepath" "github.com/AlecAivazis/survey/v2" @@ -14,7 +16,8 @@ import ( ) // ViewSBOMFiles opens a browser to view the SBOM files and pauses for user input. -func ViewSBOMFiles(directory string) error { +func ViewSBOMFiles(ctx context.Context, directory string) error { + l := logger.From(ctx) sbomViewFiles, err := filepath.Glob(filepath.Join(directory, "sbom-viewer-*")) if err != nil { return err @@ -22,12 +25,14 @@ func ViewSBOMFiles(directory string) error { if len(sbomViewFiles) == 0 { message.Note("There were no images with software bill-of-materials (SBOM) included.") + l.Info("there were no images with software bill-of-materials (SBOM) included.") return nil } link := sbomViewFiles[0] msg := fmt.Sprintf("This package has %d images with software bill-of-materials (SBOM) included. If your browser did not open automatically you can copy and paste this file location into your browser address bar to view them: %s\n\n", len(sbomViewFiles), link) message.Note(msg) + l.Info("this package has images with software bill-of-materials (SBOM) included. If your browser did not open automatically you can copy and paste this file location into your browser address bar to view them", "SBOMCount", len(sbomViewFiles), "link", link) if err := exec.LaunchURL(link); err != nil { return err } diff --git a/src/internal/packager2/inspect.go b/src/internal/packager2/inspect.go index 0e4c1e0321..ca03d51f4c 100644 --- a/src/internal/packager2/inspect.go +++ b/src/internal/packager2/inspect.go @@ -54,7 +54,7 @@ func InspectList(ctx context.Context, opt ZarfInspectOptions) ([]string, error) if err != nil { return nil, err } - // Only list images if we have have components + // Only list images if we have components if len(pkg.Components) > 0 { for _, component := range pkg.Components { imageList = append(imageList, component.Images...) @@ -104,7 +104,7 @@ func handleSBOMOptions(ctx context.Context, opt ZarfInspectOptions) error { return err } if opt.ViewSBOM { - err := sbom.ViewSBOMFiles(sbomPath) + err := sbom.ViewSBOMFiles(ctx, sbomPath) if err != nil { return err } diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index 89bbf64537..d13d05e595 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -349,7 +349,7 @@ func (pc *PackageCreator) Output(ctx context.Context, dst *layout.PackagePaths, } if pc.createOpts.ViewSBOM { - err := sbom.ViewSBOMFiles(sbomDir) + err := sbom.ViewSBOMFiles(ctx, sbomDir) if err != nil { return err } diff --git a/src/pkg/packager/inspect.go b/src/pkg/packager/inspect.go index 03dee54883..01c6ca4135 100644 --- a/src/pkg/packager/inspect.go +++ b/src/pkg/packager/inspect.go @@ -51,7 +51,7 @@ func (p *Packager) Inspect(ctx context.Context) error { } if p.cfg.InspectOpts.ViewSBOM { - err := sbom.ViewSBOMFiles(sbomDir) + err := sbom.ViewSBOMFiles(ctx, sbomDir) if err != nil { return err }