diff --git a/cmd/delete/deleteNode.go b/cmd/delete/deleteNode.go index f4333c36..783080c2 100644 --- a/cmd/delete/deleteNode.go +++ b/cmd/delete/deleteNode.go @@ -22,6 +22,7 @@ THE SOFTWARE. package delete import ( + "github.com/rancher/k3d/cmd/util" "github.com/rancher/k3d/pkg/cluster" "github.com/rancher/k3d/pkg/runtimes" k3d "github.com/rancher/k3d/pkg/types" @@ -34,10 +35,11 @@ func NewCmdDeleteNode() *cobra.Command { // create new cobra command cmd := &cobra.Command{ - Use: "node (NAME | --all)", - Short: "Delete a node.", - Long: `Delete a node.`, - Args: cobra.MinimumNArgs(1), // at least one node has to be specified + Use: "node (NAME | --all)", + Short: "Delete a node.", + Long: `Delete a node.`, + Args: cobra.MinimumNArgs(1), // at least one node has to be specified + ValidArgsFunction: util.ValidArgsAvailableNodes, Run: func(cmd *cobra.Command, args []string) { nodes := parseDeleteNodeCmd(cmd, args) diff --git a/cmd/get/getNode.go b/cmd/get/getNode.go index 0b13b060..c254ac42 100644 --- a/cmd/get/getNode.go +++ b/cmd/get/getNode.go @@ -28,6 +28,7 @@ import ( "strings" "github.com/liggitt/tabwriter" + "github.com/rancher/k3d/cmd/util" "github.com/rancher/k3d/pkg/cluster" "github.com/rancher/k3d/pkg/runtimes" k3d "github.com/rancher/k3d/pkg/types" @@ -41,11 +42,12 @@ func NewCmdGetNode() *cobra.Command { // create new command cmd := &cobra.Command{ - Use: "node [NAME [NAME...]]", - Aliases: []string{"nodes"}, - Short: "Get node(s)", - Long: `Get node(s).`, - Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all + Use: "node [NAME [NAME...]]", + Aliases: []string{"nodes"}, + Short: "Get node(s)", + Long: `Get node(s).`, + Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all + ValidArgsFunction: util.ValidArgsAvailableNodes, Run: func(cmd *cobra.Command, args []string) { nodes, headersOff := parseGetNodeCmd(cmd, args) var existingNodes []*k3d.Node diff --git a/cmd/start/startNode.go b/cmd/start/startNode.go index 0e563b4b..365898ca 100644 --- a/cmd/start/startNode.go +++ b/cmd/start/startNode.go @@ -22,6 +22,7 @@ THE SOFTWARE. package start import ( + "github.com/rancher/k3d/cmd/util" "github.com/rancher/k3d/pkg/runtimes" k3d "github.com/rancher/k3d/pkg/types" "github.com/spf13/cobra" @@ -34,9 +35,10 @@ func NewCmdStartNode() *cobra.Command { // create new command cmd := &cobra.Command{ - Use: "node NAME", // TODO: startNode: allow one or more names or --all - Short: "Start an existing k3d node", - Long: `Start an existing k3d node.`, + Use: "node NAME", // TODO: startNode: allow one or more names or --all + Short: "Start an existing k3d node", + Long: `Start an existing k3d node.`, + ValidArgsFunction: util.ValidArgsAvailableNodes, Run: func(cmd *cobra.Command, args []string) { node := parseStartNodeCmd(cmd, args) if err := runtimes.SelectedRuntime.StartNode(cmd.Context(), node); err != nil { diff --git a/cmd/stop/stopNode.go b/cmd/stop/stopNode.go index 80dbda3d..91581e3a 100644 --- a/cmd/stop/stopNode.go +++ b/cmd/stop/stopNode.go @@ -22,6 +22,7 @@ THE SOFTWARE. package stop import ( + "github.com/rancher/k3d/cmd/util" "github.com/rancher/k3d/pkg/runtimes" "github.com/spf13/cobra" @@ -35,9 +36,10 @@ func NewCmdStopNode() *cobra.Command { // create new command cmd := &cobra.Command{ - Use: "node NAME", // TODO: stopNode: allow one or more names or --all", - Short: "Stop an existing k3d node", - Long: `Stop an existing k3d node.`, + Use: "node NAME", // TODO: stopNode: allow one or more names or --all", + Short: "Stop an existing k3d node", + Long: `Stop an existing k3d node.`, + ValidArgsFunction: util.ValidArgsAvailableNodes, Run: func(cmd *cobra.Command, args []string) { node := parseStopNodeCmd(cmd, args) if err := runtimes.SelectedRuntime.StopNode(cmd.Context(), node); err != nil { diff --git a/cmd/util/completion.go b/cmd/util/completion.go index aa0b7e67..dcf3ecab 100644 --- a/cmd/util/completion.go +++ b/cmd/util/completion.go @@ -56,3 +56,28 @@ clusterLoop: } return completions, cobra.ShellCompDirectiveDefault } + +// ValidArgsAvailableNodes is used for shell completion: proposes the list of existing nodes +func ValidArgsAvailableNodes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + + var completions []string + var nodes []*k3d.Node + nodes, err := k3dcluster.GetNodes(context.Background(), runtimes.SelectedRuntime) + if err != nil { + log.Errorln("Failed to get list of nodes for shell completion") + return nil, cobra.ShellCompDirectiveError + } + +nodeLoop: + for _, node := range nodes { + for _, arg := range args { + if arg == node.Name { // only clusters, that are not in the args yet + continue nodeLoop + } + } + if strings.HasPrefix(node.Name, toComplete) { + completions = append(completions, node.Name) + } + } + return completions, cobra.ShellCompDirectiveDefault +}