Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions cmd/argocd/commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
}
47 changes: 47 additions & 0 deletions cmd/argocd/commands/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}