diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index db3172cd25fd6..c87c3e074538f 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -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 +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 { + 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) diff --git a/docs/user-guide/commands/argocd_context.md b/docs/user-guide/commands/argocd_context.md index e75620e33766a..34dd4b6dcf39d 100644 --- a/docs/user-guide/commands/argocd_context.md +++ b/docs/user-guide/commands/argocd_context.md @@ -12,20 +12,21 @@ argocd context [CONTEXT] [flags] ``` # 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 +argocd context use cd.argoproj.io # Delete Argo CD context -argocd context cd.argoproj.io --delete +argocd context delete cd.argoproj.io ``` ### Options ``` - --delete Delete the context instead of switching to it - -h, --help help for context + -h, --help help for context ``` ### Options inherited from parent commands @@ -62,4 +63,8 @@ argocd context cd.argoproj.io --delete ### SEE ALSO * [argocd](argocd.md) - argocd controls a Argo CD server +* [argocd context delete](argocd_context_delete.md) - Delete Argo CD Context +* [argocd context list](argocd_context_list.md) - List Argo CD Contexts +* [argocd context login](argocd_context_login.md) - Login using Argo CD Context +* [argocd context switch](argocd_context_switch.md) - Switch Argo CD Context diff --git a/docs/user-guide/commands/argocd_context_delete.md b/docs/user-guide/commands/argocd_context_delete.md new file mode 100644 index 0000000000000..bee2593fd6681 --- /dev/null +++ b/docs/user-guide/commands/argocd_context_delete.md @@ -0,0 +1,58 @@ +# `argocd context delete` Command Reference + +## argocd context delete + +Delete Argo CD Context + +``` +argocd context delete [flags] +``` + +### Examples + +``` + # Delete Argo CD Context + argocd context delete cd.argoproj.io +``` + +### Options + +``` + -h, --help help for delete +``` + +### Options inherited from parent commands + +``` + --argocd-context string The name of the Argo-CD server context to use + --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable + --client-crt string Client certificate file + --client-crt-key string Client certificate key file + --config string Path to Argo CD config (default "/home/user/.config/argocd/config") + --controller-name string Name of the Argo CD Application controller; set this or the ARGOCD_APPLICATION_CONTROLLER_NAME environment variable when the controller's name label differs from the default, for example when installing via the Helm chart (default "argocd-application-controller") + --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server + --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. + --grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root. + -H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers) + --http-retry-max int Maximum number of retries to establish http connection to Argo CD server + --insecure Skip server certificate and domain verification + --kube-context string Directs the command to the given kube-context + --logformat string Set the logging format. One of: json|text (default "json") + --loglevel string Set the logging level. One of: debug|info|warn|error (default "info") + --plaintext Disable TLS + --port-forward Connect to a random argocd-server port using port forwarding + --port-forward-namespace string Namespace name which should be used for port forwarding + --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default. + --redis-compress string Enable this if the application controller is configured with redis compression enabled. (possible values: gzip, none) (default "gzip") + --redis-haproxy-name string Name of the Redis HA Proxy; set this or the ARGOCD_REDIS_HAPROXY_NAME environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis-ha-haproxy") + --redis-name string Name of the Redis deployment; set this or the ARGOCD_REDIS_NAME environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis") + --repo-server-name string Name of the Argo CD Repo server; set this or the ARGOCD_REPO_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-repo-server") + --server string Argo CD server address + --server-crt string Server certificate file + --server-name string Name of the Argo CD API server; set this or the ARGOCD_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-server") +``` + +### SEE ALSO + +* [argocd context](argocd_context.md) - Switch between contexts + diff --git a/docs/user-guide/commands/argocd_context_list.md b/docs/user-guide/commands/argocd_context_list.md new file mode 100644 index 0000000000000..ede53b153dea7 --- /dev/null +++ b/docs/user-guide/commands/argocd_context_list.md @@ -0,0 +1,58 @@ +# `argocd context list` Command Reference + +## argocd context list + +List Argo CD Contexts + +``` +argocd context list [flags] +``` + +### Examples + +``` + # List Argo CD Contexts + argocd context list +``` + +### Options + +``` + -h, --help help for list +``` + +### Options inherited from parent commands + +``` + --argocd-context string The name of the Argo-CD server context to use + --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable + --client-crt string Client certificate file + --client-crt-key string Client certificate key file + --config string Path to Argo CD config (default "/home/user/.config/argocd/config") + --controller-name string Name of the Argo CD Application controller; set this or the ARGOCD_APPLICATION_CONTROLLER_NAME environment variable when the controller's name label differs from the default, for example when installing via the Helm chart (default "argocd-application-controller") + --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server + --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. + --grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root. + -H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers) + --http-retry-max int Maximum number of retries to establish http connection to Argo CD server + --insecure Skip server certificate and domain verification + --kube-context string Directs the command to the given kube-context + --logformat string Set the logging format. One of: json|text (default "json") + --loglevel string Set the logging level. One of: debug|info|warn|error (default "info") + --plaintext Disable TLS + --port-forward Connect to a random argocd-server port using port forwarding + --port-forward-namespace string Namespace name which should be used for port forwarding + --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default. + --redis-compress string Enable this if the application controller is configured with redis compression enabled. (possible values: gzip, none) (default "gzip") + --redis-haproxy-name string Name of the Redis HA Proxy; set this or the ARGOCD_REDIS_HAPROXY_NAME environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis-ha-haproxy") + --redis-name string Name of the Redis deployment; set this or the ARGOCD_REDIS_NAME environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis") + --repo-server-name string Name of the Argo CD Repo server; set this or the ARGOCD_REPO_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-repo-server") + --server string Argo CD server address + --server-crt string Server certificate file + --server-name string Name of the Argo CD API server; set this or the ARGOCD_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-server") +``` + +### SEE ALSO + +* [argocd context](argocd_context.md) - Switch between contexts + diff --git a/docs/user-guide/commands/argocd_context_login.md b/docs/user-guide/commands/argocd_context_login.md new file mode 100644 index 0000000000000..5793aa96d1951 --- /dev/null +++ b/docs/user-guide/commands/argocd_context_login.md @@ -0,0 +1,58 @@ +# `argocd context login` Command Reference + +## argocd context login + +Login using Argo CD Context + +``` +argocd context login [flags] +``` + +### Examples + +``` + # Login using Argo CD Context + argocd context login cd.argoproj.io +``` + +### Options + +``` + -h, --help help for login +``` + +### Options inherited from parent commands + +``` + --argocd-context string The name of the Argo-CD server context to use + --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable + --client-crt string Client certificate file + --client-crt-key string Client certificate key file + --config string Path to Argo CD config (default "/home/user/.config/argocd/config") + --controller-name string Name of the Argo CD Application controller; set this or the ARGOCD_APPLICATION_CONTROLLER_NAME environment variable when the controller's name label differs from the default, for example when installing via the Helm chart (default "argocd-application-controller") + --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server + --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. + --grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root. + -H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers) + --http-retry-max int Maximum number of retries to establish http connection to Argo CD server + --insecure Skip server certificate and domain verification + --kube-context string Directs the command to the given kube-context + --logformat string Set the logging format. One of: json|text (default "json") + --loglevel string Set the logging level. One of: debug|info|warn|error (default "info") + --plaintext Disable TLS + --port-forward Connect to a random argocd-server port using port forwarding + --port-forward-namespace string Namespace name which should be used for port forwarding + --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default. + --redis-compress string Enable this if the application controller is configured with redis compression enabled. (possible values: gzip, none) (default "gzip") + --redis-haproxy-name string Name of the Redis HA Proxy; set this or the ARGOCD_REDIS_HAPROXY_NAME environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis-ha-haproxy") + --redis-name string Name of the Redis deployment; set this or the ARGOCD_REDIS_NAME environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis") + --repo-server-name string Name of the Argo CD Repo server; set this or the ARGOCD_REPO_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-repo-server") + --server string Argo CD server address + --server-crt string Server certificate file + --server-name string Name of the Argo CD API server; set this or the ARGOCD_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-server") +``` + +### SEE ALSO + +* [argocd context](argocd_context.md) - Switch between contexts + diff --git a/docs/user-guide/commands/argocd_context_switch.md b/docs/user-guide/commands/argocd_context_switch.md new file mode 100644 index 0000000000000..0db385eef030d --- /dev/null +++ b/docs/user-guide/commands/argocd_context_switch.md @@ -0,0 +1,58 @@ +# `argocd context switch` Command Reference + +## argocd context switch + +Switch Argo CD Context + +``` +argocd context switch [flags] +``` + +### Examples + +``` + # Switch Argo CD Context + argocd context switch cd.argoproj.io +``` + +### Options + +``` + -h, --help help for switch +``` + +### Options inherited from parent commands + +``` + --argocd-context string The name of the Argo-CD server context to use + --auth-token string Authentication token; set this or the ARGOCD_AUTH_TOKEN environment variable + --client-crt string Client certificate file + --client-crt-key string Client certificate key file + --config string Path to Argo CD config (default "/home/user/.config/argocd/config") + --controller-name string Name of the Argo CD Application controller; set this or the ARGOCD_APPLICATION_CONTROLLER_NAME environment variable when the controller's name label differs from the default, for example when installing via the Helm chart (default "argocd-application-controller") + --core If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server + --grpc-web Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. + --grpc-web-root-path string Enables gRPC-web protocol. Useful if Argo CD server is behind proxy which does not support HTTP2. Set web root. + -H, --header strings Sets additional header to all requests made by Argo CD CLI. (Can be repeated multiple times to add multiple headers, also supports comma separated headers) + --http-retry-max int Maximum number of retries to establish http connection to Argo CD server + --insecure Skip server certificate and domain verification + --kube-context string Directs the command to the given kube-context + --logformat string Set the logging format. One of: json|text (default "json") + --loglevel string Set the logging level. One of: debug|info|warn|error (default "info") + --plaintext Disable TLS + --port-forward Connect to a random argocd-server port using port forwarding + --port-forward-namespace string Namespace name which should be used for port forwarding + --prompts-enabled Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default. + --redis-compress string Enable this if the application controller is configured with redis compression enabled. (possible values: gzip, none) (default "gzip") + --redis-haproxy-name string Name of the Redis HA Proxy; set this or the ARGOCD_REDIS_HAPROXY_NAME environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis-ha-haproxy") + --redis-name string Name of the Redis deployment; set this or the ARGOCD_REDIS_NAME environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart (default "argocd-redis") + --repo-server-name string Name of the Argo CD Repo server; set this or the ARGOCD_REPO_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-repo-server") + --server string Argo CD server address + --server-crt string Server certificate file + --server-name string Name of the Argo CD API server; set this or the ARGOCD_SERVER_NAME environment variable when the server's name label differs from the default, for example when installing via the Helm chart (default "argocd-server") +``` + +### SEE ALSO + +* [argocd context](argocd_context.md) - Switch between contexts + diff --git a/util/localconfig/localconfig.go b/util/localconfig/localconfig.go index b44ccc6959ef4..8120d031cf8ab 100644 --- a/util/localconfig/localconfig.go +++ b/util/localconfig/localconfig.go @@ -153,7 +153,7 @@ func (l *LocalConfig) ResolveContext(name string) (*Context, error) { User: *user, }, nil } - return nil, fmt.Errorf("Context '%s' undefined", name) + return nil, fmt.Errorf("context '%s' undefined", name) } func (l *LocalConfig) GetServer(name string) (*Server, error) { @@ -162,7 +162,7 @@ func (l *LocalConfig) GetServer(name string) (*Server, error) { return &s, nil } } - return nil, fmt.Errorf("Server '%s' undefined", name) + return nil, fmt.Errorf("server '%s' undefined", name) } func (l *LocalConfig) UpsertServer(server Server) { @@ -192,7 +192,7 @@ func (l *LocalConfig) GetUser(name string) (*User, error) { return &u, nil } } - return nil, fmt.Errorf("User '%s' undefined", name) + return nil, fmt.Errorf("user '%s' undefined", name) } func (l *LocalConfig) UpsertUser(user User) { @@ -205,7 +205,7 @@ func (l *LocalConfig) UpsertUser(user User) { l.Users = append(l.Users, user) } -// Returns true if user was removed successfully +// RemoveUser Returns true if user was removed successfully func (l *LocalConfig) RemoveUser(serverName string) bool { for i, u := range l.Users { if u.Name == serverName { @@ -216,7 +216,7 @@ func (l *LocalConfig) RemoveUser(serverName string) bool { return false } -// Returns true if user was removed successfully +// RemoveToken Returns true if user was removed successfully func (l *LocalConfig) RemoveToken(serverName string) bool { for i, u := range l.Users { if u.Name == serverName { @@ -238,7 +238,7 @@ func (l *LocalConfig) UpsertContext(context ContextRef) { l.Contexts = append(l.Contexts, context) } -// Returns true if context was removed successfully +// RemoveContext Returns true if context was removed successfully func (l *LocalConfig) RemoveContext(serverName string) (string, bool) { for i, c := range l.Contexts { if c.Name == serverName {