From 1d413734d8e8f9f9058ab82a4d4c49ef46b4e8a1 Mon Sep 17 00:00:00 2001 From: Justin Bradford Date: Tue, 18 Nov 2025 19:53:29 -0800 Subject: [PATCH] feat: Interactive 'uc ctx connection` command to select preferred cluster connection (#170) * feat: Interactive uc command to select preferred cluster connection https://github.com/psviderski/uncloud/issues/125 * Simplify connection selection and reordering by using the index instead of a pointer * Move connection reordering logic to a new SetDefaultConnection on context --- cmd/uncloud/context/connection.go | 78 +++++++++++++++++++++++++++++++ cmd/uncloud/context/root.go | 1 + internal/cli/config/context.go | 18 +++++++ 3 files changed, 97 insertions(+) create mode 100644 cmd/uncloud/context/connection.go diff --git a/cmd/uncloud/context/connection.go b/cmd/uncloud/context/connection.go new file mode 100644 index 00000000..df0db67c --- /dev/null +++ b/cmd/uncloud/context/connection.go @@ -0,0 +1,78 @@ +package context + +import ( + "fmt" + + "github.com/charmbracelet/huh" + "github.com/psviderski/uncloud/internal/cli" + "github.com/psviderski/uncloud/internal/cli/config" + "github.com/spf13/cobra" +) + +func NewConnectionCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "connection", + Short: "Choose a new default connection for the current context.", + RunE: func(cmd *cobra.Command, args []string) error { + uncli := cmd.Context().Value("cli").(*cli.CLI) + return selectConnection(uncli) + }, + } + return cmd +} + +func selectConnection(uncli *cli.CLI) error { + if uncli.Config == nil { + return fmt.Errorf("connection management is not available: Uncloud configuration file is not being used") + } + if len(uncli.Config.Contexts) == 0 { + return fmt.Errorf("no contexts found in Uncloud config (%s)", uncli.Config.Path()) + } + + currentCtxName := uncli.Config.CurrentContext + currentCtx, ok := uncli.Config.Contexts[currentCtxName] + if !ok { + return fmt.Errorf("current context '%s' not found", currentCtxName) + } + + if len(currentCtx.Connections) == 0 { + return fmt.Errorf("no connections found in context '%s'", currentCtxName) + } + + var selectedConnection int + form := huh.NewForm( + huh.NewGroup( + huh.NewSelect[int](). + Title("Select a default connection"). + Options(buildConnectionOptions(currentCtx.Connections)...). + Value(&selectedConnection), + ), + ) + if err := form.Run(); err != nil { + return fmt.Errorf("select connection: %w", err) + } + + currentCtx.SetDefaultConnection(selectedConnection) + selected := currentCtx.Connections[0] + + if err := uncli.Config.Save(); err != nil { + return fmt.Errorf("save config: %w", err) + } + + fmt.Printf("Default connection for context '%s' is now '%s'.\n", currentCtxName, selected) + return nil +} + +func buildConnectionOptions(connections []config.MachineConnection) []huh.Option[int] { + options := make([]huh.Option[int], len(connections)) + for i, conn := range connections { + key := conn.String() + opt := huh.NewOption(key, i) + if i == 0 { + opt.Key += " (default)" + opt = opt.Selected(true) + } + options[i] = opt + } + return options +} diff --git a/cmd/uncloud/context/root.go b/cmd/uncloud/context/root.go index dd333de8..ea5f9573 100644 --- a/cmd/uncloud/context/root.go +++ b/cmd/uncloud/context/root.go @@ -19,6 +19,7 @@ func NewRootCommand() *cobra.Command { cmd.AddCommand( NewListCommand(), NewUseCommand(), + NewConnectionCommand(), ) return cmd diff --git a/internal/cli/config/context.go b/internal/cli/config/context.go index a86b53da..60ca8043 100644 --- a/internal/cli/config/context.go +++ b/internal/cli/config/context.go @@ -4,3 +4,21 @@ type Context struct { Name string `yaml:"-"` Connections []MachineConnection `yaml:"connections"` } + +func (c *Context) SetDefaultConnection(index int) { + // Do nothing if the index is out of range. + if index < 0 || index >= len(c.Connections) { + return + } + + selected := c.Connections[index] + newConnections := make([]MachineConnection, 0, len(c.Connections)) + newConnections = append(newConnections, selected) + + for i, conn := range c.Connections { + if i != index { + newConnections = append(newConnections, conn) + } + } + c.Connections = newConnections +}