Skip to content

Commit

Permalink
refactor: add error handling to view SBOM files
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba committed Jul 23, 2024
1 parent 05ef439 commit ac96d29
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
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

0 comments on commit ac96d29

Please sign in to comment.