Skip to content
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

feat: Context get cmd #18

Open
wants to merge 4 commits into
base: context
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion cmd/argocd/commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}

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)
}
27 changes: 27 additions & 0 deletions cmd/argocd/commands/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
}