From 6409a47c718b002363f7290e54f871c9e98e055e Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Tue, 21 Apr 2026 10:22:02 +0200 Subject: [PATCH] feat: add uc ctx show command to print the current cluster context (#317) * feat: add uc ctx show This only shows the current context and nothing more. On error nothing is printed (also no error). This is useful to include in your prompt. See: #314 Signed-off-by: Miek Gieben * docs Signed-off-by: Miek Gieben --------- Signed-off-by: Miek Gieben --- cmd/uncloud/context/root.go | 1 + cmd/uncloud/context/show.go | 35 ++++++++++++++++++++++++++ website/docs/9-cli-reference/uc_ctx.md | 1 + 3 files changed, 37 insertions(+) create mode 100644 cmd/uncloud/context/show.go diff --git a/cmd/uncloud/context/root.go b/cmd/uncloud/context/root.go index ea5f9573..1a353084 100644 --- a/cmd/uncloud/context/root.go +++ b/cmd/uncloud/context/root.go @@ -20,6 +20,7 @@ func NewRootCommand() *cobra.Command { NewListCommand(), NewUseCommand(), NewConnectionCommand(), + NewShowCommand(), ) return cmd diff --git a/cmd/uncloud/context/show.go b/cmd/uncloud/context/show.go new file mode 100644 index 00000000..04c4c50e --- /dev/null +++ b/cmd/uncloud/context/show.go @@ -0,0 +1,35 @@ +package context + +import ( + "fmt" + + "github.com/psviderski/uncloud/internal/cli" + "github.com/spf13/cobra" +) + +func NewShowCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "show", + Short: "Show current cluster context.", + RunE: func(cmd *cobra.Command, args []string) error { + uncli := cmd.Context().Value("cli").(*cli.CLI) + return show(uncli) + }, + } + + return cmd +} + +func show(uncli *cli.CLI) error { + // discard errors, only show the current context, otherwise nothing + if uncli.Config == nil { + return nil + } + + if len(uncli.Config.Contexts) == 0 { + return nil + } + fmt.Println(uncli.Config.CurrentContext) + + return nil +} diff --git a/website/docs/9-cli-reference/uc_ctx.md b/website/docs/9-cli-reference/uc_ctx.md index 41dadac0..f421543f 100644 --- a/website/docs/9-cli-reference/uc_ctx.md +++ b/website/docs/9-cli-reference/uc_ctx.md @@ -26,5 +26,6 @@ uc ctx [flags] * [uc](uc.md) - A CLI tool for managing Uncloud resources such as machines, services, and volumes. * [uc ctx connection](uc_ctx_connection.md) - Choose a new default connection for the current context. * [uc ctx ls](uc_ctx_ls.md) - List available cluster contexts. +* [uc ctx show](uc_ctx_show.md) - Show current cluster context. * [uc ctx use](uc_ctx_use.md) - Switch to a different cluster context.