-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(cli): add argocd context subcommands
#25077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e371c4a
30aaf10
a7c151a
6506a4a
56946f4
ac142e9
fcd1c27
d28d54d
9c1e965
e3b7c18
22ec297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ppapapetrou76 I added |
||||||||
| 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 { | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||||||||
| 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() | ||||||||
|
|
@@ -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 | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -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) | ||||||||
|
|
||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.