completion: new ValidArgsAvailableNodes
- completion proposes list of existing nodes - available for get/delete/start/stop node
This commit is contained in:
parent
d3f7621ed8
commit
45a7b2dd7d
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user