From dd680790059982b27da513749f2b3f38073c95b4 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 4 May 2026 05:42:23 +0200 Subject: [PATCH] feat: add shell completion for ucind CLI (#344) For feature parity with uc. Only _really_ usefull for ucind cluster rm, but still useful, and not many lines of code. Signed-off-by: Miek Gieben --- cmd/ucind/cluster/remove.go | 7 +++++++ internal/ucind/completion/cluster.go | 29 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 internal/ucind/completion/cluster.go diff --git a/cmd/ucind/cluster/remove.go b/cmd/ucind/cluster/remove.go index 70ad44e7..476e7ff8 100644 --- a/cmd/ucind/cluster/remove.go +++ b/cmd/ucind/cluster/remove.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/psviderski/uncloud/internal/ucind" + "github.com/psviderski/uncloud/internal/ucind/completion" "github.com/spf13/cobra" ) @@ -26,6 +27,12 @@ func NewRemoveCommand() *cobra.Command { fmt.Printf("Cluster '%s' removed.\n", name) return nil }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { + if len(args) > 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + return completion.Clusters(cmd, args, toComplete) + }, } return cmd } diff --git a/internal/ucind/completion/cluster.go b/internal/ucind/completion/cluster.go new file mode 100644 index 00000000..ff0e7b2a --- /dev/null +++ b/internal/ucind/completion/cluster.go @@ -0,0 +1,29 @@ +// Package completion implements completion functions for the ucind cli. +package completion + +import ( + "slices" + "strings" + + "github.com/psviderski/uncloud/internal/ucind" + "github.com/spf13/cobra" +) + +func Clusters(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { + p := cmd.Context().Value("provisioner").(*ucind.Provisioner) + clusters, err := p.ListClusters(cmd.Context()) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + names := []cobra.Completion{} + for _, cluster := range clusters { + if slices.Contains(args, cluster.Name) { + continue + } + if strings.HasPrefix(cluster.Name, toComplete) { + names = append(names, cluster.Name) + } + } + return names, cobra.ShellCompDirectiveNoFileComp +}