From ace6f3805376230dc12a592f874add9796100053 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 28 Sep 2023 14:43:13 +0200 Subject: [PATCH] tmp --- completers/k3d_completer/cmd/cluster_edit.go | 21 +++++++++++++ completers/k3d_completer/cmd/node_delete.go | 2 +- pkg/actions/tools/k3d/node.go | 31 ++++++++++++++++++-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/completers/k3d_completer/cmd/cluster_edit.go b/completers/k3d_completer/cmd/cluster_edit.go index 994c5daa0e..cb1cac0f9d 100644 --- a/completers/k3d_completer/cmd/cluster_edit.go +++ b/completers/k3d_completer/cmd/cluster_edit.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/actions/tools/k3d" "github.com/spf13/cobra" ) @@ -17,4 +18,24 @@ func init() { cluster_editCmd.Flags().StringSlice("port-add", []string{}, "[EXPERIMENTAL] Map ports from the node containers (via the serverlb) to the host (Format: `[HOST:][HOSTPORT:]CONTAINERPORT[/PROTOCOL][@NODEFILTER]`)") clusterCmd.AddCommand(cluster_editCmd) + + carapace.Gen(cluster_editCmd).FlagCompletion(carapace.ActionMap{ + "port-add": carapace.ActionMultiPartsN("@", 2, func(c carapace.Context) carapace.Action { + cluster := "" + if len(c.Args) > 0 { + cluster = c.Args[0] + } + + switch len(c.Parts) { + case 0: + return carapace.ActionValues() + default: + return k3d.ActionNodes(k3d.NodeOpts{Cluster: cluster, Running: true, Stopped: true}).UniqueList(",") // TODO nodefilter? + } + }), + }) + + carapace.Gen(cluster_editCmd).PositionalCompletion( + k3d.ActionClusters(), + ) } diff --git a/completers/k3d_completer/cmd/node_delete.go b/completers/k3d_completer/cmd/node_delete.go index 3b413e8f43..ba7739eebe 100644 --- a/completers/k3d_completer/cmd/node_delete.go +++ b/completers/k3d_completer/cmd/node_delete.go @@ -20,6 +20,6 @@ func init() { nodeCmd.AddCommand(node_deleteCmd) carapace.Gen(node_deleteCmd).PositionalAnyCompletion( - k3d.ActionNodes().FilterArgs(), + k3d.ActionNodes(k3d.NodeOpts{}.Default()).FilterArgs(), ) } diff --git a/pkg/actions/tools/k3d/node.go b/pkg/actions/tools/k3d/node.go index ab230372d7..aef214a9da 100644 --- a/pkg/actions/tools/k3d/node.go +++ b/pkg/actions/tools/k3d/node.go @@ -8,6 +8,18 @@ import ( "github.com/rsteube/carapace/pkg/style" ) +type NodeOpts struct { + Cluster string + Running bool + Stopped bool +} + +func (n NodeOpts) Default() NodeOpts { + n.Running = true + n.Stopped = true + return n +} + type node struct { Name string Role string @@ -19,6 +31,19 @@ type node struct { } } +func (n node) applies(opts NodeOpts) bool { + switch { + case opts.Cluster != "" && opts.Cluster != n.RuntimeLabels.K3dCluster: + return false + case opts.Running && n.State.Running: + return true + case opts.Stopped && !n.State.Running: + return true + default: + return false + } +} + func (n node) style() string { if n.State.Running { return style.Carapace.KeywordPositive @@ -26,7 +51,7 @@ func (n node) style() string { return style.Carapace.KeywordNegative } -func ActionNodes() carapace.Action { +func ActionNodes(opts NodeOpts) carapace.Action { return carapace.ActionExecCommand("k3d", "node", "list", "--output", "json")(func(output []byte) carapace.Action { var nodes []node if err := json.Unmarshal(output, &nodes); err != nil { @@ -35,7 +60,9 @@ func ActionNodes() carapace.Action { vals := make([]string, 0) for _, n := range nodes { - vals = append(vals, n.Name, fmt.Sprintf("%v.%v", n.RuntimeLabels.K3dCluster, n.Role), n.style()) + if n.applies(opts) { + vals = append(vals, n.Name, fmt.Sprintf("%v.%v", n.RuntimeLabels.K3dCluster, n.Role), n.style()) + } } return carapace.ActionStyledValuesDescribed(vals...) })