switch over to using RunE

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-12 17:52:17 -06:00
parent 0018903264
commit 9ad3b220a3
4 changed files with 18 additions and 26 deletions

View file

@ -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
},
}

View file

@ -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
},
}

View file

@ -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
},
}

View file

@ -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
},
}