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

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [x] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

* closes #1306 

unblocks:
* #1192

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-10-24 12:40:24 -07:00 committed by GitHub
parent 740b193415
commit f58db6c3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 38 deletions

View File

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)
var subCommands = []*cobra.Command{
var subCommandFuncs = []func() *cobra.Command{
createCmd,
listCmd,
detailsCmd,
@ -18,28 +18,30 @@ 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)
addBackupTo(subCommand)
}
}
}
// The backup category of commands.
// `corso backup [<subcommand>] [<flag>...]`
var backupCmd = &cobra.Command{
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`.
// Produces the same output as `corso backup --help`.
@ -49,15 +51,16 @@ func handleBackupCmd(cmd *cobra.Command, args []string) error {
// The backup create subcommand.
// `corso backup create <service> [<flag>...]`
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 <service> [<flag>...]`
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 <service> [<flag>...]`
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 <service> [<flag>...]`
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`.

View File

@ -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,13 +23,15 @@ const restoreCommand = "restore"
// The restore category of commands.
// `corso restore [<subcommand>] [<flag>...]`
var restoreCmd = &cobra.Command{
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`.
// Produces the same output as `corso restore --help`.