From 3be698eab959f6321911a6ee90ffd38750174837 Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Tue, 7 Jun 2022 09:15:00 -0600 Subject: [PATCH] return errors in cli handlers (#146) * return errors in cli handlers * return the wrapped errors --- src/cli/backup/backup.go | 5 +++-- src/cli/backup/exchange.go | 17 +++++++++-------- src/cli/repo/s3.go | 25 +++++++++++++------------ src/cli/utils/utils.go | 6 ------ 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/cli/backup/backup.go b/src/cli/backup/backup.go index 50773c610..0c55d06ec 100644 --- a/src/cli/backup/backup.go +++ b/src/cli/backup/backup.go @@ -22,7 +22,8 @@ func AddCommands(parent *cobra.Command) { // `corso backup [] [...]` var backupCmd = &cobra.Command{ Use: "backup", - Short: "Backup a M365 service", + Short: "Backup your service data", + Long: `Backup the data stored in one of your M365 services.`, Run: handleBackupCmd, Args: cobra.NoArgs, } @@ -38,7 +39,7 @@ func handleBackupCmd(cmd *cobra.Command, args []string) { var createCommand = "create" var createCmd = &cobra.Command{ Use: createCommand, - Short: "Backup M365 Exchange", + Short: "Backup an M365 Service", Run: handleCreateCmd, Args: cobra.NoArgs, } diff --git a/src/cli/backup/exchange.go b/src/cli/backup/exchange.go index 964128469..a73d4fb28 100644 --- a/src/cli/backup/exchange.go +++ b/src/cli/backup/exchange.go @@ -3,6 +3,7 @@ package backup import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/alcionai/corso/cli/utils" @@ -31,14 +32,13 @@ func addExchangeApp(parent *cobra.Command) *cobra.Command { // `corso backup create exchange [...]` var exchangeCreateCmd = &cobra.Command{ Use: "exchange", - Short: "Start backing up Exchange data", - Long: `Creates a new backup of your microsoft Exchange data.`, - Run: createExchangeCmd, + Short: "Backup M365 Exchange", + RunE: createExchangeCmd, Args: cobra.NoArgs, } // initializes a s3 repo. -func createExchangeCmd(cmd *cobra.Command, args []string) { +func createExchangeCmd(cmd *cobra.Command, args []string) error { mv := utils.GetM365Vars() fmt.Printf( "Called - %s\n\t365TenantID:\t%s\n\t356Client:\t%s\n\tfound 356Secret:\t%v\n", @@ -55,23 +55,24 @@ func createExchangeCmd(cmd *cobra.Command, args []string) { // todo (rkeepers) - retrieve storage details from corso config s, err := storage.NewStorage(storage.ProviderUnknown) if err != nil { - utils.Fatalf("Failed to configure storage provider: %v", err) + return errors.Wrap(err, "Failed to configure storage provider") } r, err := repository.Connect(cmd.Context(), a, s) if err != nil { - utils.Fatalf("Failed to connect to the %s repository: %v", s.Provider, err) + return errors.Wrapf(err, "Failed to connect to the %s repository", s.Provider) } defer utils.CloseRepo(cmd.Context(), r) bo, err := r.NewBackup(cmd.Context(), []string{user}) if err != nil { - utils.Fatalf("Failed to initialize Exchange backup: %v", err) + return errors.Wrap(err, "Failed to initialize Exchange backup") } if err := bo.Run(cmd.Context()); err != nil { - utils.Fatalf("Failed to run Exchange backup: %v", err) + return errors.Wrap(err, "Failed to run Exchange backup") } fmt.Printf("Backed up Exchange in %s for user %s.\n", s.Provider, user) + return nil } diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index 8f953c276..21460fe48 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/alcionai/corso/cli/utils" @@ -43,17 +44,16 @@ var s3InitCmd = &cobra.Command{ Use: "s3", Short: "Initialize a S3 repository", Long: `Bootstraps a new S3 repository and connects it to your m356 account.`, - Run: initS3Cmd, + RunE: initS3Cmd, Args: cobra.NoArgs, } // initializes a s3 repo. -func initS3Cmd(cmd *cobra.Command, args []string) { +func initS3Cmd(cmd *cobra.Command, args []string) error { mv := utils.GetM365Vars() s3Cfg, commonCfg, err := makeS3Config() if err != nil { - fmt.Println(err) - os.Exit(1) + return err } fmt.Printf( @@ -72,16 +72,17 @@ func initS3Cmd(cmd *cobra.Command, args []string) { } s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg) if err != nil { - utils.Fatalf("Failed to configure storage provider: %v", err) + return errors.Wrap(err, "Failed to configure storage provider") } r, err := repository.Initialize(cmd.Context(), a, s) if err != nil { - utils.Fatalf("Failed to initialize a new S3 repository: %v", err) + return errors.Wrap(err, "Failed to initialize a new S3 repository") } defer utils.CloseRepo(cmd.Context(), r) fmt.Printf("Initialized a S3 repository within bucket %s.\n", s3Cfg.Bucket) + return nil } // `corso repo connect s3 [...]` @@ -89,17 +90,16 @@ var s3ConnectCmd = &cobra.Command{ Use: "s3", Short: "Connect to a S3 repository", Long: `Ensures a connection to an existing S3 repository.`, - Run: connectS3Cmd, + RunE: connectS3Cmd, Args: cobra.NoArgs, } // connects to an existing s3 repo. -func connectS3Cmd(cmd *cobra.Command, args []string) { +func connectS3Cmd(cmd *cobra.Command, args []string) error { mv := utils.GetM365Vars() s3Cfg, commonCfg, err := makeS3Config() if err != nil { - fmt.Println(err) - os.Exit(1) + return err } fmt.Printf( @@ -118,16 +118,17 @@ func connectS3Cmd(cmd *cobra.Command, args []string) { } s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg) if err != nil { - utils.Fatalf("Failed to configure storage provider: %v", err) + return errors.Wrap(err, "Failed to configure storage provider") } r, err := repository.Connect(cmd.Context(), a, s) if err != nil { - utils.Fatalf("Failed to connect to the S3 repository: %v", err) + return errors.Wrap(err, "Failed to connect to the S3 repository") } defer utils.CloseRepo(cmd.Context(), r) fmt.Printf("Connected to S3 bucket %s.\n", s3Cfg.Bucket) + return nil } // helper for aggregating aws connection details. diff --git a/src/cli/utils/utils.go b/src/cli/utils/utils.go index 5e88261cf..3e53967da 100644 --- a/src/cli/utils/utils.go +++ b/src/cli/utils/utils.go @@ -44,9 +44,3 @@ func CloseRepo(ctx context.Context, r *repository.Repository) { fmt.Print("Error closing repository:", err) } } - -// Fatalf prints the message and then exits with os code 1. -func Fatalf(format string, a ...any) { - fmt.Printf(format, a...) - os.Exit(1) -}