From 3163fef41da4e0b1961545e821888e3cbffe781d Mon Sep 17 00:00:00 2001 From: LeeSeolHui Date: Fri, 4 Oct 2024 23:40:46 +0900 Subject: [PATCH 1/4] feature: create cmd that get context details Signed-off-by: LeeSeolHui --- cmd/argocd/commands/context.go | 50 +++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index ea80a9367ede8..08c89834d00a1 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 @@ -96,9 +99,22 @@ argocd context cd.argoproj.io --delete`, }, } + // Get context details + getDetailsCommand := &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) + }, + } + command.AddCommand(listCommand) command.AddCommand(useCommand) command.AddCommand(deleteCommand) + command.AddCommand(getDetailsCommand) command.Flags().BoolVar(&deleteFlag, "delete", false, "Delete the context instead of switching to it") @@ -205,4 +221,36 @@ 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) +} From 008adc31b74c6baa3221aad5e0c03dde526e976a Mon Sep 17 00:00:00 2001 From: LeeSeolHui Date: Sat, 5 Oct 2024 09:46:37 +0900 Subject: [PATCH 2/4] chore: remove unnecessary comments. Signed-off-by: LeeSeolHui --- cmd/argocd/commands/context.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index 08c89834d00a1..0899aafe0506d 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -249,7 +249,6 @@ func getContextDetails(context, configPath string) { 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) From 27175ee58c6ac68433c95aa08f370d9bf06d3977 Mon Sep 17 00:00:00 2001 From: LeeSeolHui Date: Sun, 20 Oct 2024 15:09:00 +0900 Subject: [PATCH 3/4] refactor: reflect code review Signed-off-by: LeeSeolHui --- cmd/argocd/commands/context.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index 0899aafe0506d..55275f06626cb 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -99,26 +99,26 @@ argocd context cd.argoproj.io --delete`, }, } - // Get context details - getDetailsCommand := &cobra.Command{ + 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) }, } - - command.AddCommand(listCommand) - command.AddCommand(useCommand) - command.AddCommand(deleteCommand) - command.AddCommand(getDetailsCommand) - - command.Flags().BoolVar(&deleteFlag, "delete", false, "Delete the context instead of switching to it") - - return command } // Refactored logic for switching Argo CD context From ad3f7171a43b357c97dffdf1b4d3311a761d854c Mon Sep 17 00:00:00 2001 From: LeeSeolHui Date: Sun, 20 Oct 2024 15:40:37 +0900 Subject: [PATCH 4/4] test: test get context Signed-off-by: LeeSeolHui --- cmd/argocd/commands/context_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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:") +}