From 587b318d1f752a9a2f877e0247d5fc5073940554 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 14 Mar 2024 13:03:22 -0600 Subject: [PATCH] PrintTableToStdOut returns err when rendering fails Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/inspect/inspect.go | 15 ++++++++++----- pkg/cmd/attestation/logging/logger.go | 8 ++++---- pkg/cmd/attestation/verify/verify.go | 4 +++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/attestation/inspect/inspect.go b/pkg/cmd/attestation/inspect/inspect.go index 8c034eff1..0b028de7b 100644 --- a/pkg/cmd/attestation/inspect/inspect.go +++ b/pkg/cmd/attestation/inspect/inspect.go @@ -18,9 +18,10 @@ import ( func NewInspectCmd(f *cmdutil.Factory, runF func(*Options) error) *cobra.Command { opts := &Options{} inspectCmd := &cobra.Command{ - Use: "inspect [ | oci://] --bundle ", - Args: cobra.ExactArgs(1), - Short: "Inspect a sigstore bundle", + Use: "inspect [ | oci://] --bundle ", + Args: cobra.ExactArgs(1), + Hidden: true, + Short: "Inspect a sigstore bundle", Long: heredoc.Docf(` Inspect a downloaded Sigstore bundle for a given artifact. @@ -144,7 +145,9 @@ func runInspect(opts *Options) error { rows := make([][]string, 1) rows[0] = jsonResults - opts.Logger.PrintTableToStdOut(nil, rows) + if err = opts.Logger.PrintTableToStdOut(nil, rows); err != nil { + return fmt.Errorf("failed to print output as JSON: %w", err) + } return nil } @@ -155,7 +158,9 @@ func runInspect(opts *Options) error { } headerRow := []string{"Repo Name", "Repo ID", "Org Name", "Org ID", "Workflow ID"} - opts.Logger.PrintTableToStdOut(headerRow, details) + if err = opts.Logger.PrintTableToStdOut(headerRow, details); err != nil { + return fmt.Errorf("failed to print output as table: %w", err) + } return nil } diff --git a/pkg/cmd/attestation/logging/logger.go b/pkg/cmd/attestation/logging/logger.go index aaae86215..f97742a29 100644 --- a/pkg/cmd/attestation/logging/logger.go +++ b/pkg/cmd/attestation/logging/logger.go @@ -2,7 +2,6 @@ package logging import ( "fmt" - "log" "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/go-gh/v2/pkg/tableprinter" @@ -69,9 +68,9 @@ func (l *Logger) VerbosePrintf(f string, v ...interface{}) (int, error) { return fmt.Fprintf(l.IO.ErrOut, f, v...) } -func (l *Logger) PrintTableToStdOut(headers []string, rows [][]string) { +func (l *Logger) PrintTableToStdOut(headers []string, rows [][]string) error { if rows == nil { - return + return nil } t := tableprinter.New(l.IO.Out, l.IO.IsStdoutTTY(), l.IO.TerminalWidth()) @@ -88,6 +87,7 @@ func (l *Logger) PrintTableToStdOut(headers []string, rows [][]string) { } if err := t.Render(); err != nil { - log.Fatal(err) + return err } + return nil } diff --git a/pkg/cmd/attestation/verify/verify.go b/pkg/cmd/attestation/verify/verify.go index baa42373d..1b0c81bb8 100644 --- a/pkg/cmd/attestation/verify/verify.go +++ b/pkg/cmd/attestation/verify/verify.go @@ -210,7 +210,9 @@ func runVerify(opts *Options) error { rows := make([][]string, 1) rows[0] = jsonResults - opts.Logger.PrintTableToStdOut(nil, rows) + if err := opts.Logger.PrintTableToStdOut(nil, rows); err != nil { + return fmt.Errorf("failed to print JSON output") + } } // All attestations passed verification and policy evaluation