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>...]`
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,
}

View File

@ -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 [<flag>...]`
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
}

View File

@ -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 [<flag>...]`
@ -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.

View File

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