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

refactor: add error handling to view SBOM files #2752

Merged
merged 1 commit into from
Jul 25, 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
43 changes: 24 additions & 19 deletions src/internal/packager/sbom/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@ import (
)

// ViewSBOMFiles opens a browser to view the SBOM files and pauses for user input.
func ViewSBOMFiles(directory string) {
sbomViewFiles, _ := filepath.Glob(filepath.Join(directory, "sbom-viewer-*"))

if len(sbomViewFiles) > 0 {
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)

if err := exec.LaunchURL(link); err != nil {
message.Debug(err)
}
func ViewSBOMFiles(directory string) error {
sbomViewFiles, err := filepath.Glob(filepath.Join(directory, "sbom-viewer-*"))
if err != nil {
return err
}

// Use survey.Input to hang until user input
var value string
prompt := &survey.Input{
Message: "Hit the 'enter' key when you are done viewing the SBOM files",
Default: "",
}
_ = survey.AskOne(prompt, &value)
} else {
if len(sbomViewFiles) == 0 {
message.Note("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)
if err := exec.LaunchURL(link); err != nil {
return err
}
var value string
prompt := &survey.Input{
Message: "Hit the 'enter' key when you are done viewing the SBOM files",
Default: "",
}
err = survey.AskOne(prompt, &value)
if err != nil {
return err
}
return nil
}
5 changes: 4 additions & 1 deletion src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ func (pc *PackageCreator) Output(ctx context.Context, dst *layout.PackagePaths,
}

if pc.createOpts.ViewSBOM {
sbom.ViewSBOMFiles(sbomDir)
err := sbom.ViewSBOMFiles(sbomDir)
if err != nil {
return err
}
}
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (p *Packager) Inspect(ctx context.Context) (err error) {
}

if p.cfg.InspectOpts.ViewSBOM {
sbom.ViewSBOMFiles(sbomDir)
err := sbom.ViewSBOMFiles(sbomDir)
if err != nil {
return err
}
}

return nil
Expand Down
Loading