Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logger to packager2 inspect #3153

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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