From ce52cc5f41927b02804663a7404a60db3780a636 Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Thu, 30 Jun 2022 09:30:37 -0600 Subject: [PATCH] Add the backup list command to the cli (#254) * Add the backup list command to the cli Adds `corso backup list ` to the cli commands. Currently does nothing, while we're waiting on downstream implementation. e2e hookup will arrive later. --- src/cli/backup/backup.go | 18 ++++++++++++++ src/cli/backup/exchange.go | 49 +++++++++++++++++++++++++++++++++----- src/cli/repo/s3.go | 18 ++++++++------ src/cli/utils/utils.go | 8 +++++++ src/go.mod | 2 +- 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/cli/backup/backup.go b/src/cli/backup/backup.go index 0c55d06ec..64022e295 100644 --- a/src/cli/backup/backup.go +++ b/src/cli/backup/backup.go @@ -12,9 +12,11 @@ var backupApplications = []func(parent *cobra.Command) *cobra.Command{ func AddCommands(parent *cobra.Command) { parent.AddCommand(backupCmd) backupCmd.AddCommand(createCmd) + backupCmd.AddCommand(listCmd) for _, addBackupTo := range backupApplications { addBackupTo(createCmd) + addBackupTo(listCmd) } } @@ -49,3 +51,19 @@ var createCmd = &cobra.Command{ func handleCreateCmd(cmd *cobra.Command, args []string) { cmd.Help() } + +// The backup list subcommand. +// `corso backup list [...]` +var listCommand = "list" +var listCmd = &cobra.Command{ + Use: listCommand, + Short: "List the history of restore points for a service", + Run: handleListCmd, + Args: cobra.NoArgs, +} + +// Handler for calls to `corso backup list`. +// Produces the same output as `corso backup list --help`. +func handleListCmd(cmd *cobra.Command, args []string) { + cmd.Help() +} diff --git a/src/cli/backup/exchange.go b/src/cli/backup/exchange.go index fc8fcd1b2..897aa54e1 100644 --- a/src/cli/backup/exchange.go +++ b/src/cli/backup/exchange.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/alcionai/corso/cli/config" "github.com/alcionai/corso/cli/utils" @@ -19,20 +20,25 @@ var ( // called by backup.go to map parent subcommands to provider-specific handling. func addExchangeApp(parent *cobra.Command) *cobra.Command { - var c *cobra.Command + var ( + c *cobra.Command + fs *pflag.FlagSet + ) switch parent.Use { case createCommand: - c = exchangeCreateCmd + c, fs = utils.AddCommand(parent, exchangeCreateCmd) + fs.StringVar(&user, "user", "", "ID of the user whose Exchange data is to be backed up.") + case listCommand: + c, _ = utils.AddCommand(parent, exchangeListCmd) } - parent.AddCommand(c) - fs := c.Flags() - fs.StringVar(&user, "user", "", "ID of the user whose Exchange data is to be backed up.") return c } +const exchangeServiceCommand = "exchange" + // `corso backup create exchange [...]` var exchangeCreateCmd = &cobra.Command{ - Use: "exchange", + Use: exchangeServiceCommand, Short: "Backup M365 Exchange service data", RunE: createExchangeCmd, Args: cobra.NoArgs, @@ -81,3 +87,34 @@ func createExchangeCmd(cmd *cobra.Command, args []string) error { fmt.Printf("Backed up restore point %s in %s for Exchange user %s.\n", result.SnapshotID, s.Provider, user) return nil } + +// `corso backup list exchange [...]` +var exchangeListCmd = &cobra.Command{ + Use: exchangeServiceCommand, + Short: "List the history of M365 Exchange service backups", + RunE: listExchangeCmd, + Args: cobra.NoArgs, +} + +// lists the history of backup operations +func listExchangeCmd(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + _, acct, err := config.GetStorageAndAccount(true, nil) + if err != nil { + return err + } + + m365, err := acct.M365Config() + if err != nil { + return errors.Wrap(err, "Failed to parse m365 account config") + } + + logger.Ctx(ctx).Debugw( + "Called - "+cmd.CommandPath(), + "tenantID", m365.TenantID) + + // todo (keepers issue #251): e2e hookup + + return nil +} diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index ebf2c250c..18b9f647f 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/alcionai/corso/cli/config" "github.com/alcionai/corso/cli/utils" @@ -24,15 +25,16 @@ var ( // called by repo.go to map parent subcommands to provider-specific handling. func addS3Commands(parent *cobra.Command) *cobra.Command { - var c *cobra.Command + var ( + c *cobra.Command + fs *pflag.FlagSet + ) switch parent.Use { case initCommand: - c = s3InitCmd + c, fs = utils.AddCommand(parent, s3InitCmd) case connectCommand: - c = s3ConnectCmd + c, fs = utils.AddCommand(parent, s3ConnectCmd) } - parent.AddCommand(c) - fs := c.Flags() fs.StringVar(&accessKey, "access-key", "", "Access key ID (replaces the AWS_ACCESS_KEY_ID env variable).") fs.StringVar(&bucket, "bucket", "", "Name of the S3 bucket (required).") c.MarkFlagRequired("bucket") @@ -41,9 +43,11 @@ func addS3Commands(parent *cobra.Command) *cobra.Command { return c } +const s3ProviderCommand = "s3" + // `corso repo init s3 [...]` var s3InitCmd = &cobra.Command{ - Use: "s3", + Use: s3ProviderCommand, Short: "Initialize a S3 repository", Long: `Bootstraps a new S3 repository and connects it to your m356 account.`, RunE: initS3Cmd, @@ -103,7 +107,7 @@ func initS3Cmd(cmd *cobra.Command, args []string) error { // `corso repo connect s3 [...]` var s3ConnectCmd = &cobra.Command{ - Use: "s3", + Use: s3ProviderCommand, Short: "Connect to a S3 repository", Long: `Ensures a connection to an existing S3 repository.`, RunE: connectS3Cmd, diff --git a/src/cli/utils/utils.go b/src/cli/utils/utils.go index 51f3b3bb8..9e7c51acb 100644 --- a/src/cli/utils/utils.go +++ b/src/cli/utils/utils.go @@ -7,6 +7,7 @@ import ( "github.com/alcionai/corso/pkg/repository" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) // RequireProps validates the existence of the properties @@ -39,3 +40,10 @@ func HasNoFlagsAndShownHelp(cmd *cobra.Command) bool { } return false } + +// AddCommand adds the subCommand to the parent, and returns +// both the subCommand and its pflags. +func AddCommand(parent, c *cobra.Command) (*cobra.Command, *pflag.FlagSet) { + parent.AddCommand(c) + return c, c.Flags() +} diff --git a/src/go.mod b/src/go.mod index 53993d52b..41042b1bb 100644 --- a/src/go.mod +++ b/src/go.mod @@ -16,6 +16,7 @@ require ( github.com/microsoftgraph/msgraph-sdk-go-core v0.26.1 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.4.0 + github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.12.0 github.com/stretchr/testify v1.7.2 go.uber.org/zap v1.21.0 @@ -77,7 +78,6 @@ require ( github.com/prometheus/procfs v0.7.3 // indirect github.com/rs/xid v1.4.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect github.com/zeebo/blake3 v0.2.3 // indirect go.opentelemetry.io/otel v1.7.0 // indirect