return errors in cli handlers (#146)

* return errors in cli handlers

* return the wrapped errors
This commit is contained in:
Keepers 2022-06-07 09:15:00 -06:00 committed by GitHub
parent dbb778c569
commit 3be698eab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 28 deletions

View File

@ -22,7 +22,8 @@ func AddCommands(parent *cobra.Command) {
// `corso backup [<subcommand>] [<flag>...]` // `corso backup [<subcommand>] [<flag>...]`
var backupCmd = &cobra.Command{ var backupCmd = &cobra.Command{
Use: "backup", 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, Run: handleBackupCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
@ -38,7 +39,7 @@ func handleBackupCmd(cmd *cobra.Command, args []string) {
var createCommand = "create" var createCommand = "create"
var createCmd = &cobra.Command{ var createCmd = &cobra.Command{
Use: createCommand, Use: createCommand,
Short: "Backup M365 Exchange", Short: "Backup an M365 Service",
Run: handleCreateCmd, Run: handleCreateCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }

View File

@ -3,6 +3,7 @@ package backup
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/alcionai/corso/cli/utils" "github.com/alcionai/corso/cli/utils"
@ -31,14 +32,13 @@ func addExchangeApp(parent *cobra.Command) *cobra.Command {
// `corso backup create exchange [<flag>...]` // `corso backup create exchange [<flag>...]`
var exchangeCreateCmd = &cobra.Command{ var exchangeCreateCmd = &cobra.Command{
Use: "exchange", Use: "exchange",
Short: "Start backing up Exchange data", Short: "Backup M365 Exchange",
Long: `Creates a new backup of your microsoft Exchange data.`, RunE: createExchangeCmd,
Run: createExchangeCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
// initializes a s3 repo. // initializes a s3 repo.
func createExchangeCmd(cmd *cobra.Command, args []string) { func createExchangeCmd(cmd *cobra.Command, args []string) error {
mv := utils.GetM365Vars() mv := utils.GetM365Vars()
fmt.Printf( fmt.Printf(
"Called - %s\n\t365TenantID:\t%s\n\t356Client:\t%s\n\tfound 356Secret:\t%v\n", "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 // todo (rkeepers) - retrieve storage details from corso config
s, err := storage.NewStorage(storage.ProviderUnknown) s, err := storage.NewStorage(storage.ProviderUnknown)
if err != nil { 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) r, err := repository.Connect(cmd.Context(), a, s)
if err != nil { 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) defer utils.CloseRepo(cmd.Context(), r)
bo, err := r.NewBackup(cmd.Context(), []string{user}) bo, err := r.NewBackup(cmd.Context(), []string{user})
if err != nil { 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 { 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) fmt.Printf("Backed up Exchange in %s for user %s.\n", s.Provider, user)
return nil
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/alcionai/corso/cli/utils" "github.com/alcionai/corso/cli/utils"
@ -43,17 +44,16 @@ var s3InitCmd = &cobra.Command{
Use: "s3", Use: "s3",
Short: "Initialize a S3 repository", Short: "Initialize a S3 repository",
Long: `Bootstraps a new S3 repository and connects it to your m356 account.`, Long: `Bootstraps a new S3 repository and connects it to your m356 account.`,
Run: initS3Cmd, RunE: initS3Cmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
// initializes a s3 repo. // initializes a s3 repo.
func initS3Cmd(cmd *cobra.Command, args []string) { func initS3Cmd(cmd *cobra.Command, args []string) error {
mv := utils.GetM365Vars() mv := utils.GetM365Vars()
s3Cfg, commonCfg, err := makeS3Config() s3Cfg, commonCfg, err := makeS3Config()
if err != nil { if err != nil {
fmt.Println(err) return err
os.Exit(1)
} }
fmt.Printf( fmt.Printf(
@ -72,16 +72,17 @@ func initS3Cmd(cmd *cobra.Command, args []string) {
} }
s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg) s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg)
if err != nil { 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) r, err := repository.Initialize(cmd.Context(), a, s)
if err != nil { 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) defer utils.CloseRepo(cmd.Context(), r)
fmt.Printf("Initialized a S3 repository within bucket %s.\n", s3Cfg.Bucket) fmt.Printf("Initialized a S3 repository within bucket %s.\n", s3Cfg.Bucket)
return nil
} }
// `corso repo connect s3 [<flag>...]` // `corso repo connect s3 [<flag>...]`
@ -89,17 +90,16 @@ var s3ConnectCmd = &cobra.Command{
Use: "s3", Use: "s3",
Short: "Connect to a S3 repository", Short: "Connect to a S3 repository",
Long: `Ensures a connection to an existing S3 repository.`, Long: `Ensures a connection to an existing S3 repository.`,
Run: connectS3Cmd, RunE: connectS3Cmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
// connects to an existing s3 repo. // connects to an existing s3 repo.
func connectS3Cmd(cmd *cobra.Command, args []string) { func connectS3Cmd(cmd *cobra.Command, args []string) error {
mv := utils.GetM365Vars() mv := utils.GetM365Vars()
s3Cfg, commonCfg, err := makeS3Config() s3Cfg, commonCfg, err := makeS3Config()
if err != nil { if err != nil {
fmt.Println(err) return err
os.Exit(1)
} }
fmt.Printf( fmt.Printf(
@ -118,16 +118,17 @@ func connectS3Cmd(cmd *cobra.Command, args []string) {
} }
s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg) s, err := storage.NewStorage(storage.ProviderS3, s3Cfg, commonCfg)
if err != nil { 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) r, err := repository.Connect(cmd.Context(), a, s)
if err != nil { 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) defer utils.CloseRepo(cmd.Context(), r)
fmt.Printf("Connected to S3 bucket %s.\n", s3Cfg.Bucket) fmt.Printf("Connected to S3 bucket %s.\n", s3Cfg.Bucket)
return nil
} }
// helper for aggregating aws connection details. // helper for aggregating aws connection details.

View File

@ -44,9 +44,3 @@ func CloseRepo(ctx context.Context, r *repository.Repository) {
fmt.Print("Error closing repository:", err) 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)
}