Skip to content

Commit

Permalink
chore: add logger to packager2 inspect (#3153)
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp authored Oct 29, 2024
1 parent d61d099 commit 4b7e6e4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/internal/packager/sbom/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package sbom

import (
"context"
"fmt"
"github.com/zarf-dev/zarf/src/pkg/logger"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -14,20 +16,23 @@ 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
}

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
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager2/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4b7e6e4

Please sign in to comment.