diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index ea80a9367ede8..55275f06626cb 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -33,6 +33,9 @@ argocd context use cd.argoproj.io # Delete Argo CD context argocd context delete cd.argoproj.io +# Get Argocd CD context details +argocd context get cd.argoproj.io + # Switch Argo CD context (legacy) argocd context cd.argoproj.io @@ -99,12 +102,25 @@ argocd context cd.argoproj.io --delete`, command.AddCommand(listCommand) command.AddCommand(useCommand) command.AddCommand(deleteCommand) + command.AddCommand(NewContextGetCommand(clientOpts)) command.Flags().BoolVar(&deleteFlag, "delete", false, "Delete the context instead of switching to it") return command } +func NewContextGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { + return &cobra.Command{ + Use: "get [CONTEXT]", + Short: "Get a specific Argo CD context details", + Args: cobra.ExactArgs(1), + Run: func(c *cobra.Command, args []string) { + ctxName := args[0] + getContextDetails(ctxName, clientOpts.ConfigPath) + }, + } +} + // Refactored logic for switching Argo CD context func useArgoCDContext(ctxName string, configPath string) error { localCfg, err := localconfig.ReadLocalConfig(configPath) @@ -205,4 +221,35 @@ 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 +} + +func getContextDetails(context, configPath string) { + localCfg, err := localconfig.ReadLocalConfig(configPath) + if err != nil { + errors.CheckError(err) + } + + if localCfg == nil { + log.Fatalf("No contexts defined in %s", configPath) + } + + ctx, err := localCfg.ResolveContext(context) + errors.CheckError(err) + + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + defer func() { _ = w.Flush() }() + + writeAndCheck(w, "- SERVERS\n") + writeAndCheck(w, "grpc-web-root-path:\t%s\n", ctx.Server.GRPCWebRootPath) + writeAndCheck(w, "plain-text:\t%v\n", ctx.Server.PlainText) + writeAndCheck(w, "server:\t%s\n\n", ctx.Server.Server) + + writeAndCheck(w, "- USERS\n") + writeAndCheck(w, "name:\t%s\n", ctx.User.Name) + writeAndCheck(w, "auth-token:\t%s\n", ctx.User.AuthToken) +} + +func writeAndCheck(w *tabwriter.Writer, format string, args ...interface{}) { + _, err := fmt.Fprintf(w, format, args...) + errors.CheckError(err) +} diff --git a/cmd/argocd/commands/context_test.go b/cmd/argocd/commands/context_test.go index e9f953a22cd0f..47381cccf90c4 100644 --- a/cmd/argocd/commands/context_test.go +++ b/cmd/argocd/commands/context_test.go @@ -76,3 +76,30 @@ 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 TestGetContextDetails(t *testing.T) { + // Write the test config 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) + localConfig, err := localconfig.ReadLocalConfig(testConfigFilePath) + require.NoError(t, err) + assert.Equal(t, "localhost:8080", localConfig.CurrentContext) + assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "localhost:8080", Server: "localhost:8080", User: "localhost:8080"}) + + // Get a specific ArgoCD Context details + output, err := captureOutput(func() error { + getContextDetails("argocd1.example.com:443", testConfigFilePath) + return nil + }) + + assert.Contains(t, output, "- SERVERS\n") + assert.Contains(t, output, "grpc-web-root-path:") + assert.Contains(t, output, "plain-text:") + assert.Contains(t, output, "server:") + assert.Contains(t, output, "- USERS\n") + assert.Contains(t, output, "name:") + assert.Contains(t, output, "auth-token:") +}