diff --git a/src/cli/backup/backup.go b/src/cli/backup/backup.go index 0cb72c792..83d0870a2 100644 --- a/src/cli/backup/backup.go +++ b/src/cli/backup/backup.go @@ -3,6 +3,8 @@ package backup import ( "context" + "github.com/hashicorp/go-multierror" + "github.com/alcionai/corso/src/cli/config" "github.com/alcionai/corso/src/cli/options" . "github.com/alcionai/corso/src/cli/print" @@ -13,6 +15,7 @@ import ( "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/repository" + "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/store" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -154,6 +157,62 @@ func handleDeleteCmd(cmd *cobra.Command, args []string) error { return cmd.Help() } +func runBackups( + ctx context.Context, + r repository.Repository, + serviceName, resourceOwnerType string, + selectorSet []selectors.Selector, +) error { + var ( + merrs *multierror.Error + bIDs []model.StableID + ) + + for _, discSel := range selectorSet { + bo, err := r.NewBackup(ctx, discSel) + if err != nil { + merrs = multierror.Append(merrs, errors.Wrapf( + err, + "Failed to initialize %s backup for %s %s", + serviceName, + resourceOwnerType, + discSel.DiscreteOwner, + )) + + continue + } + + err = bo.Run(ctx) + if err != nil { + merrs = multierror.Append(merrs, errors.Wrapf( + err, + "Failed to run %s backup for %s %s", + serviceName, + resourceOwnerType, + discSel.DiscreteOwner, + )) + + continue + } + + bIDs = append(bIDs, bo.Results.BackupID) + } + + bups, ferrs := r.Backups(ctx, bIDs) + // TODO: print/log recoverable errors + if ferrs.Failure() != nil { + return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) + } + + backup.PrintAll(ctx, bups) + + if e := merrs.ErrorOrNil(); e != nil { + return Only(ctx, e) + } + + return nil +} + // genericDeleteCommand is a helper function that all services can use // for the removal of an entry from the repository func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []string) error { diff --git a/src/cli/backup/exchange.go b/src/cli/backup/exchange.go index 04d9f5b6c..ec6a543d8 100644 --- a/src/cli/backup/exchange.go +++ b/src/cli/backup/exchange.go @@ -3,7 +3,6 @@ package backup import ( "context" - "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -12,8 +11,6 @@ import ( . "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/cli/utils" "github.com/alcionai/corso/src/internal/data" - "github.com/alcionai/corso/src/internal/model" - "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/path" @@ -275,50 +272,18 @@ func createExchangeCmd(cmd *cobra.Command, args []string) error { return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 user(s)")) } - var ( - merrs *multierror.Error - bIDs []model.StableID - ) + selectorSet := []selectors.Selector{} for _, discSel := range sel.SplitByResourceOwner(users) { - bo, err := r.NewBackup(ctx, discSel.Selector) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to initialize Exchange backup for user %s", - discSel.DiscreteOwner, - )) - - continue - } - - err = bo.Run(ctx) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to run Exchange backup for user %s", - discSel.DiscreteOwner, - )) - - continue - } - - bIDs = append(bIDs, bo.Results.BackupID) + selectorSet = append(selectorSet, discSel.Selector) } - bups, ferrs := r.Backups(ctx, bIDs) - // TODO: print/log recoverable errors - if ferrs.Failure() != nil { - return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) - } - - backup.PrintAll(ctx, bups) - - if e := merrs.ErrorOrNil(); e != nil { - return Only(ctx, e) - } - - return nil + return runBackups( + ctx, + r, + "Exchange", "user", + selectorSet, + ) } func exchangeBackupCreateSelectors(userIDs, cats []string) *selectors.ExchangeBackup { diff --git a/src/cli/backup/onedrive.go b/src/cli/backup/onedrive.go index fb9c0c57f..a7ca08f85 100644 --- a/src/cli/backup/onedrive.go +++ b/src/cli/backup/onedrive.go @@ -3,7 +3,6 @@ package backup import ( "context" - "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -12,8 +11,6 @@ import ( . "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/cli/utils" "github.com/alcionai/corso/src/internal/data" - "github.com/alcionai/corso/src/internal/model" - "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/path" @@ -194,50 +191,18 @@ func createOneDriveCmd(cmd *cobra.Command, args []string) error { return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 users")) } - var ( - merrs *multierror.Error - bIDs []model.StableID - ) + selectorSet := []selectors.Selector{} for _, discSel := range sel.SplitByResourceOwner(users) { - bo, err := r.NewBackup(ctx, discSel.Selector) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to initialize OneDrive backup for user %s", - discSel.DiscreteOwner, - )) - - continue - } - - err = bo.Run(ctx) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to run OneDrive backup for user %s", - discSel.DiscreteOwner, - )) - - continue - } - - bIDs = append(bIDs, bo.Results.BackupID) + selectorSet = append(selectorSet, discSel.Selector) } - bups, ferrs := r.Backups(ctx, bIDs) - // TODO: print/log recoverable errors - if ferrs.Failure() != nil { - return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) - } - - backup.PrintAll(ctx, bups) - - if e := merrs.ErrorOrNil(); e != nil { - return Only(ctx, e) - } - - return nil + return runBackups( + ctx, + r, + "OneDrive", "user", + selectorSet, + ) } func validateOneDriveBackupCreateFlags(users []string) error { diff --git a/src/cli/backup/sharepoint.go b/src/cli/backup/sharepoint.go index 22b57846e..e3d12fe33 100644 --- a/src/cli/backup/sharepoint.go +++ b/src/cli/backup/sharepoint.go @@ -3,7 +3,6 @@ package backup import ( "context" - "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -14,8 +13,6 @@ import ( "github.com/alcionai/corso/src/internal/connector" "github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/data" - "github.com/alcionai/corso/src/internal/model" - "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/path" @@ -241,50 +238,18 @@ func createSharePointCmd(cmd *cobra.Command, args []string) error { return Only(ctx, errors.Wrap(err, "Retrieving up sharepoint sites by ID and WebURL")) } - var ( - merrs *multierror.Error - bIDs []model.StableID - ) + selectorSet := []selectors.Selector{} for _, discSel := range sel.SplitByResourceOwner(gc.GetSiteIDs()) { - bo, err := r.NewBackup(ctx, discSel.Selector) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to initialize SharePoint backup for site %s", - discSel.DiscreteOwner, - )) - - continue - } - - err = bo.Run(ctx) - if err != nil { - merrs = multierror.Append(merrs, errors.Wrapf( - err, - "Failed to run SharePoint backup for site %s", - discSel.DiscreteOwner, - )) - - continue - } - - bIDs = append(bIDs, bo.Results.BackupID) + selectorSet = append(selectorSet, discSel.Selector) } - bups, ferrs := r.Backups(ctx, bIDs) - // TODO: print/log recoverable errors - if ferrs.Failure() != nil { - return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) - } - - backup.PrintAll(ctx, bups) - - if e := merrs.ErrorOrNil(); e != nil { - return Only(ctx, e) - } - - return nil + return runBackups( + ctx, + r, + "SharePoint", "site", + selectorSet, + ) } func validateSharePointBackupCreateFlags(sites, weburls, cats []string) error {