From f58db6c3a72f8d7a4b2267a3b9d5a3b98212d935 Mon Sep 17 00:00:00 2001 From: ashmrtn Date: Mon, 24 Oct 2022 12:40:24 -0700 Subject: [PATCH] Always return new instances of cobra commands (#1310) ## Description CI associates config information with individual command instances. This causes problems when multiple test suites are running at the same time. Work around this by always using a new instance of a command when the command tree is built. ## Type of change - [ ] :sunflower: Feature - [x] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [ ] :hamster: Trivial/Minor ## Issue(s) * closes #1306 unblocks: * #1192 ## Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --- src/cli/backup/backup.go | 66 +++++++++++++++++++++----------------- src/cli/restore/restore.go | 19 ++++++----- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/cli/backup/backup.go b/src/cli/backup/backup.go index b9ec6f5cc..008d8eb44 100644 --- a/src/cli/backup/backup.go +++ b/src/cli/backup/backup.go @@ -4,7 +4,7 @@ import ( "github.com/spf13/cobra" ) -var subCommands = []*cobra.Command{ +var subCommandFuncs = []func() *cobra.Command{ createCmd, listCmd, detailsCmd, @@ -18,27 +18,29 @@ var serviceCommands = []func(parent *cobra.Command) *cobra.Command{ // AddCommands attaches all `corso backup * *` commands to the parent. func AddCommands(parent *cobra.Command) { - parent.AddCommand(backupCmd) + backupC := backupCmd() + parent.AddCommand(backupC) - for _, sc := range subCommands { - backupCmd.AddCommand(sc) - } + for _, sc := range subCommandFuncs { + subCommand := sc() + backupC.AddCommand(subCommand) - for _, addBackupTo := range serviceCommands { - for _, sc := range subCommands { - addBackupTo(sc) + for _, addBackupTo := range serviceCommands { + addBackupTo(subCommand) } } } // The backup category of commands. // `corso backup [] [...]` -var backupCmd = &cobra.Command{ - Use: "backup", - Short: "Backup your service data", - Long: `Backup the data stored in one of your M365 services.`, - RunE: handleBackupCmd, - Args: cobra.NoArgs, +func backupCmd() *cobra.Command { + return &cobra.Command{ + Use: "backup", + Short: "Backup your service data", + Long: `Backup the data stored in one of your M365 services.`, + RunE: handleBackupCmd, + Args: cobra.NoArgs, + } } // Handler for flat calls to `corso backup`. @@ -49,15 +51,16 @@ func handleBackupCmd(cmd *cobra.Command, args []string) error { // The backup create subcommand. // `corso backup create [...]` -var ( - createCommand = "create" - createCmd = &cobra.Command{ +var createCommand = "create" + +func createCmd() *cobra.Command { + return &cobra.Command{ Use: createCommand, Short: "Backup an M365 Service", RunE: handleCreateCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso backup create`. // Produces the same output as `corso backup create --help`. @@ -67,15 +70,16 @@ func handleCreateCmd(cmd *cobra.Command, args []string) error { // The backup list subcommand. // `corso backup list [...]` -var ( - listCommand = "list" - listCmd = &cobra.Command{ +var listCommand = "list" + +func listCmd() *cobra.Command { + return &cobra.Command{ Use: listCommand, Short: "List the history of backups for a service", RunE: handleListCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso backup list`. // Produces the same output as `corso backup list --help`. @@ -85,15 +89,16 @@ func handleListCmd(cmd *cobra.Command, args []string) error { // The backup details subcommand. // `corso backup details [...]` -var ( - detailsCommand = "details" - detailsCmd = &cobra.Command{ +var detailsCommand = "details" + +func detailsCmd() *cobra.Command { + return &cobra.Command{ Use: detailsCommand, Short: "Shows the details of a backup for a service", RunE: handleDetailsCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso backup details`. // Produces the same output as `corso backup details --help`. @@ -103,15 +108,16 @@ func handleDetailsCmd(cmd *cobra.Command, args []string) error { // The backup delete subcommand. // `corso backup delete [...]` -var ( - deleteCommand = "delete" - deleteCmd = &cobra.Command{ +var deleteCommand = "delete" + +func deleteCmd() *cobra.Command { + return &cobra.Command{ Use: deleteCommand, Short: "Deletes a backup for a service", RunE: handleDeleteCmd, Args: cobra.NoArgs, } -) +} // Handler for calls to `corso backup delete`. // Produces the same output as `corso backup delete --help`. diff --git a/src/cli/restore/restore.go b/src/cli/restore/restore.go index faed134f2..541c9d37a 100644 --- a/src/cli/restore/restore.go +++ b/src/cli/restore/restore.go @@ -11,10 +11,11 @@ var restoreCommands = []func(parent *cobra.Command) *cobra.Command{ // AddCommands attaches all `corso restore * *` commands to the parent. func AddCommands(parent *cobra.Command) { - parent.AddCommand(restoreCmd) + restoreC := restoreCmd() + parent.AddCommand(restoreC) for _, addRestoreTo := range restoreCommands { - addRestoreTo(restoreCmd) + addRestoreTo(restoreC) } } @@ -22,12 +23,14 @@ const restoreCommand = "restore" // The restore category of commands. // `corso restore [] [...]` -var restoreCmd = &cobra.Command{ - Use: restoreCommand, - Short: "Restore your service data", - Long: `Restore the data stored in one of your M365 services.`, - RunE: handleRestoreCmd, - Args: cobra.NoArgs, +func restoreCmd() *cobra.Command { + return &cobra.Command{ + Use: restoreCommand, + Short: "Restore your service data", + Long: `Restore the data stored in one of your M365 services.`, + RunE: handleRestoreCmd, + Args: cobra.NoArgs, + } } // Handler for flat calls to `corso restore`.