Skip to content
119 changes: 100 additions & 19 deletions cmd/argocd/commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,67 @@ import (

// NewContextCommand returns a new instance of an `argocd ctx` command
func NewContextCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var deletion bool
command := &cobra.Command{
Use: "context [CONTEXT]",
Aliases: []string{"ctx"},
Short: "Switch between contexts",
Example: `# List Argo CD Contexts
argocd context
# List Argo CD Contexts
argocd context list

# Switch Argo CD context
argocd context cd.argoproj.io
argocd context switch cd.argoproj.io
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about argocd context use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not wrong, the switch command does exactly the same as use command, do you mean to change the name of the command from switch to be use?
If this is not what you mean, can you tell me the difference between them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onasser1 I believe @nitishfy is suggesting to use the term use instead of switch just like kubectl command

Copy link
Contributor Author

@onasser1 onasser1 Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ppapapetrou76 I added use as an alias for switch command in the latest commit.
Do you think I should replace switch completely with use? or keep them together with alias?

argocd context use cd.argoproj.io

# Delete Argo CD context
argocd context cd.argoproj.io --delete`,
argocd context delete cd.argoproj.io`,
Run: func(c *cobra.Command, args []string) {
localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath)
errors.CheckError(err)
c.HelpFunc()(c, args)
os.Exit(1)
},
}
command.AddCommand(NewContextListCommand(clientOpts))
command.AddCommand(NewContextSwitchCommand(clientOpts))
command.AddCommand(NewContextDeleteCommand(clientOpts))
command.AddCommand(NewContextLoginCommand(clientOpts))
return command
}

if deletion {
if len(args) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
err := deleteContext(args[0], clientOpts.ConfigPath)
errors.CheckError(err)
return
// NewContextListCommand returns a new instance of `argocd context list` command
func NewContextListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func NewContextListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
// NewContextListCommand lists the contexts
func NewContextListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should to stick to the function comments format in app and login (i.e. https://github.com/argoproj/argo-cd/blob/master/cmd/argocd/commands/app.go#L124)
or apply your change and make the comment more meaningful (Anyway I see that the usage of that commands make it clear anyway) but I'd wait for your answer

command := &cobra.Command{
Use: "list",
Short: "List Argo CD Contexts",
Example: ` # List Argo CD Contexts
argocd context list`,
Run: func(c *cobra.Command, args []string) {
if len(args) != 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
printArgoCDContexts(clientOpts.ConfigPath)
},
}

return command
}

// NewContextSwitchCommand returns a new instance of `argocd context switch` command
func NewContextSwitchCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
command := &cobra.Command{
Use: "switch",
Aliases: []string{"use"},
Short: "Switch Argo CD Context",
Example: ` # Switch Argo CD Context
argocd context switch cd.argoproj.io`,
Run: func(c *cobra.Command, args []string) {
localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath)
errors.CheckError(err)

if len(args) == 0 {
printArgoCDContexts(clientOpts.ConfigPath)
return
c.HelpFunc()(c, args)
os.Exit(1)
}

ctxName := args[0]

argoCDDir, err := localconfig.DefaultConfigDir()
Expand Down Expand Up @@ -78,7 +107,60 @@ argocd context cd.argoproj.io --delete`,
fmt.Printf("Switched to context '%s'\n", localCfg.CurrentContext)
},
}
command.Flags().BoolVar(&deletion, "delete", false, "Delete the context instead of switching to it")
return command
}

// NewContextLoginCommand returns a new instance of `argocd context login` command
func NewContextLoginCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
command := &cobra.Command{
Use: "login",
Short: "Login using Argo CD Context",
Example: ` # Login using Argo CD Context
argocd context login cd.argoproj.io`,
RunE: func(c *cobra.Command, args []string) error {
if len(args) != 1 {
c.HelpFunc()(c, args)
return stderrors.New("invalid arguments")
}
localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath)
errors.CheckError(err)
if localCfg == nil {
return stderrors.New("couldn't find local config")
}
ctx, err := localCfg.ResolveContext(args[0])
if err != nil {
return fmt.Errorf("context %s does not exist", args[0])
}
server, err := localCfg.GetServer(ctx.Server.Server)
if err != nil {
return fmt.Errorf("server %s does not exist", ctx.Server.Server)
}
loginCmd := NewLoginCommand(clientOpts)
loginCmd.SetArgs([]string{server.Server})
err = loginCmd.Execute()
errors.CheckError(err)
return nil
},
}
return command
}

// NewContextDeleteCommand returns a new instance of `argocd context delete` command
func NewContextDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
command := &cobra.Command{
Use: "delete",
Short: "Delete Argo CD Context",
Example: ` # Delete Argo CD Context
argocd context delete cd.argoproj.io`,
Run: func(c *cobra.Command, args []string) {
if len(args) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
err := deleteContext(args[0], clientOpts.ConfigPath)
errors.CheckError(err)
},
}
return command
}

Expand All @@ -88,7 +170,6 @@ func deleteContext(context, configPath string) error {
if localCfg == nil {
return stderrors.New("nothing to logout from")
}

serverName, ok := localCfg.RemoveContext(context)
if !ok {
return fmt.Errorf("context %s does not exist", context)
Expand Down
15 changes: 10 additions & 5 deletions docs/user-guide/commands/argocd_context.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions docs/user-guide/commands/argocd_context_delete.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions docs/user-guide/commands/argocd_context_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions docs/user-guide/commands/argocd_context_login.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading