diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index ec2d30e28..142d6cd9f 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -76,24 +76,22 @@ func NewDownloadCmd(f *cmdutil.Factory) *cobra.Command { // Use Run instead of RunE because if an error is returned by RunVerify // when RunE is used, the command usage will be printed // We only want to print the error, not usage - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { hc, err := f.HttpClient() if err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } opts.APIClient = api.NewLiveClient(hc, opts.Logger) opts.OCIClient = oci.NewLiveClient() if err := auth.IsHostSupported(); err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } if err := RunDownload(opts); err != nil { - opts.Logger.ColorScheme.Redf("Failed to download the artifact's bundle(s): %s", err.Error()) - os.Exit(1) + return fmt.Errorf("Failed to download the artifact's bundle(s): %w", err) } + return nil }, } diff --git a/pkg/cmd/attestation/inspect/inspect.go b/pkg/cmd/attestation/inspect/inspect.go index 630966217..2041309ba 100644 --- a/pkg/cmd/attestation/inspect/inspect.go +++ b/pkg/cmd/attestation/inspect/inspect.go @@ -3,7 +3,6 @@ package inspect import ( "encoding/json" "fmt" - "os" "github.com/cli/cli/v2/pkg/cmd/attestation/artifact" "github.com/cli/cli/v2/pkg/cmd/attestation/artifact/oci" @@ -70,17 +69,16 @@ func NewInspectCmd(f *cmdutil.Factory) *cobra.Command { // Use Run instead of RunE because if an error is returned by RunInspect // when RunE is used, the command usage will be printed // We only want to print the error, not usage - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { opts.OCIClient = oci.NewLiveClient() if err := auth.IsHostSupported(); err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } if err := RunInspect(opts); err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Redf("Failed to inspect the artifact and bundle: %s", err.Error())) - os.Exit(1) + return fmt.Errorf("Failed to inspect the artifact and bundle: %w", err) } + return nil }, } diff --git a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go b/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go index 22074c709..ed5400260 100644 --- a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go +++ b/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go @@ -37,19 +37,18 @@ func NewTUFRootVerifyCmd(f *cmdutil.Factory) *cobra.Command { # Verify the TUF repository from a provided TUF root gh attestation tuf-root-verify --mirror https://tuf-repo.github.com --root /path/to/1.root.json `), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { logger := logging.NewDefaultLogger(f.IOStreams) if err := auth.IsHostSupported(); err != nil { - fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } if err := verifyTUFRoot(mirror, root); err != nil { - fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Redf("Failed to verify the TUF repository: %s", err)) - os.Exit(1) + return fmt.Errorf("Failed to verify the TUF repository: %w", err) } fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Green("Successfully verified the TUF repository")) + return nil }, } diff --git a/pkg/cmd/attestation/verify/verify.go b/pkg/cmd/attestation/verify/verify.go index 0a0031e00..ec3529ee1 100644 --- a/pkg/cmd/attestation/verify/verify.go +++ b/pkg/cmd/attestation/verify/verify.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "os" "github.com/cli/cli/v2/pkg/cmd/attestation/api" "github.com/cli/cli/v2/pkg/cmd/attestation/artifact" @@ -92,24 +91,22 @@ func NewVerifyCmd(f *cmdutil.Factory) *cobra.Command { // Use Run instead of RunE because if an error is returned by RunVerify // when RunE is used, the command usage will be printed // We only want to print the error, not usage - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { hc, err := f.HttpClient() if err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } opts.APIClient = api.NewLiveClient(hc, opts.Logger) opts.OCIClient = oci.NewLiveClient() if err := auth.IsHostSupported(); err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error())) - os.Exit(1) + return err } if err := RunVerify(opts); err != nil { - opts.Logger.Println(opts.Logger.ColorScheme.Redf("Failed to verify the artifact: %s", err.Error())) - os.Exit(1) + return fmt.Errorf("Failed to verify the artifact: %w", err) } + return nil }, }