diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index ea80a9367ede8..e2cb656e04a08 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -62,17 +62,30 @@ argocd context cd.argoproj.io --delete`, }, } - // List subcommand - listCommand := &cobra.Command{ + // Add subcommands to the main command + command.AddCommand(newListCommand(clientOpts)) + command.AddCommand(newUseCommand(clientOpts)) + command.AddCommand(newDeleteCommand(clientOpts)) + + command.Flags().BoolVar(&deleteFlag, "delete", false, "Delete the context instead of switching to it") + + return command +} + +// Function to create the list command +func newListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { + return &cobra.Command{ Use: "list", Short: "List Argo CD contexts", Run: func(c *cobra.Command, args []string) { printArgoCDContexts(clientOpts.ConfigPath) }, } +} - // Use subcommand to switch context - useCommand := &cobra.Command{ +// Function to create the use command +func newUseCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { + return &cobra.Command{ Use: "use [CONTEXT]", Short: "Switch to a specific Argo CD context", Args: cobra.ExactArgs(1), // context argument is required @@ -81,9 +94,11 @@ argocd context cd.argoproj.io --delete`, errors.CheckError(err) }, } +} - // Delete subcommand to remove context - deleteCommand := &cobra.Command{ +// Function to create the delete command +func newDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { + return &cobra.Command{ Use: "delete [CONTEXT]", Short: "Delete a specific Argo CD context", Args: cobra.ExactArgs(1), // context argument is required @@ -95,14 +110,6 @@ argocd context cd.argoproj.io --delete`, fmt.Printf("Deleted context '%s'\n", ctxName) }, } - - command.AddCommand(listCommand) - command.AddCommand(useCommand) - command.AddCommand(deleteCommand) - - command.Flags().BoolVar(&deleteFlag, "delete", false, "Delete the context instead of switching to it") - - return command } // Refactored logic for switching Argo CD context @@ -205,4 +212,4 @@ func printArgoCDContexts(configPath string) { _, err = fmt.Fprintf(w, "%s\t%s\t%s\n", prefix, context.Name, context.Server.Server) errors.CheckError(err) } -} \ No newline at end of file +} diff --git a/cmd/argocd/commands/context_test.go b/cmd/argocd/commands/context_test.go index e9f953a22cd0f..e2500f714d8bd 100644 --- a/cmd/argocd/commands/context_test.go +++ b/cmd/argocd/commands/context_test.go @@ -76,3 +76,50 @@ func TestContextDelete(t *testing.T) { assert.NotContains(t, localConfig.Users, localconfig.User{AuthToken: "vErrYS3c3tReFRe$hToken", Name: "localhost:8080"}) assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "argocd2.example.com:443", Server: "argocd2.example.com:443", User: "argocd2.example.com:443"}) } + +func Test_PrintArgocdContexts(t *testing.T) { + err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) + require.NoError(t, err) + defer os.Remove(testConfigFilePath) + err = os.Chmod(testConfigFilePath, 0o600) + require.NoError(t, err, "Could not change the file permission to 0600 %v", err) + + str, err := captureOutput(func() error { + printArgoCDContexts(testConfigFilePath) + return nil + }) + require.NoError(t, err) + + expectedOutput := `CURRENT NAME SERVER + argocd1.example.com:443 argocd1.example.com:443 + argocd2.example.com:443 argocd2.example.com:443 +* localhost:8080 localhost:8080 +` + assert.Equal(t, expectedOutput, str) +} + +// Test for useArgoCDContext +func TestUseArgoCDContext(t *testing.T) { + // Setup test configuration file + err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) + require.NoError(t, err) + defer os.Remove(testConfigFilePath) + err = os.Chmod(testConfigFilePath, 0o600) + require.NoError(t, err, "Could not change the file permission to 0600 %v", err) + + // Test switching to a different context + err = useArgoCDContext("argocd2.example.com:443", testConfigFilePath) + require.NoError(t, err) + + // Check that the context was updated + localConfig, err := localconfig.ReadLocalConfig(testConfigFilePath) + require.NoError(t, err) + assert.Equal(t, "argocd2.example.com:443", localConfig.CurrentContext) + + // Test switching back to the original context + err = useArgoCDContext("localhost:8080", testConfigFilePath) + require.NoError(t, err) + localConfig, err = localconfig.ReadLocalConfig(testConfigFilePath) + require.NoError(t, err) + assert.Equal(t, "localhost:8080", localConfig.CurrentContext) +}