Skip to content

Commit 994537b

Browse files
committed
tmp
1 parent a10807c commit 994537b

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

completers/k3d_completer/cmd/cluster_edit.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"github.com/rsteube/carapace"
5+
"github.com/rsteube/carapace-bin/pkg/actions/tools/k3d"
56
"github.com/spf13/cobra"
67
)
78

@@ -17,4 +18,24 @@ func init() {
1718

1819
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]`)")
1920
clusterCmd.AddCommand(cluster_editCmd)
21+
22+
carapace.Gen(cluster_editCmd).FlagCompletion(carapace.ActionMap{
23+
"port-add": carapace.ActionMultiPartsN("@", 2, func(c carapace.Context) carapace.Action {
24+
cluster := ""
25+
if len(c.Args) > 0 {
26+
cluster = c.Args[0]
27+
}
28+
29+
switch len(c.Parts) {
30+
case 0:
31+
return carapace.ActionValues()
32+
default:
33+
return k3d.ActionNodes(k3d.NodeOpts{Cluster: cluster, Running: true, Stopped: true}).UniqueList(",") // TODO nodefilter?
34+
}
35+
}),
36+
})
37+
38+
carapace.Gen(cluster_editCmd).PositionalCompletion(
39+
k3d.ActionClusters(),
40+
)
2041
}

completers/k3d_completer/cmd/node_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ func init() {
2020
nodeCmd.AddCommand(node_deleteCmd)
2121

2222
carapace.Gen(node_deleteCmd).PositionalAnyCompletion(
23-
k3d.ActionNodes().FilterArgs(),
23+
k3d.ActionNodes(k3d.NodeOpts{}.Default()).FilterArgs(),
2424
)
2525
}

pkg/actions/tools/k3d/node.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import (
88
"github.com/rsteube/carapace/pkg/style"
99
)
1010

11+
type NodeOpts struct {
12+
Cluster string
13+
Running bool
14+
Stopped bool
15+
}
16+
17+
func (n NodeOpts) Default() NodeOpts {
18+
n.Running = true
19+
n.Stopped = true
20+
return n
21+
}
22+
1123
type node struct {
1224
Name string
1325
Role string
@@ -19,14 +31,27 @@ type node struct {
1931
}
2032
}
2133

34+
func (n node) applies(opts NodeOpts) bool {
35+
switch {
36+
case opts.Cluster != "" && opts.Cluster != n.RuntimeLabels.K3dCluster:
37+
return false
38+
case opts.Running && n.State.Running:
39+
return true
40+
case opts.Stopped && !n.State.Running:
41+
return true
42+
default:
43+
return false
44+
}
45+
}
46+
2247
func (n node) style() string {
2348
if n.State.Running {
2449
return style.Carapace.KeywordPositive
2550
}
2651
return style.Carapace.KeywordNegative
2752
}
2853

29-
func ActionNodes() carapace.Action {
54+
func ActionNodes(opts NodeOpts) carapace.Action {
3055
return carapace.ActionExecCommand("k3d", "node", "list", "--output", "json")(func(output []byte) carapace.Action {
3156
var nodes []node
3257
if err := json.Unmarshal(output, &nodes); err != nil {
@@ -35,7 +60,9 @@ func ActionNodes() carapace.Action {
3560

3661
vals := make([]string, 0)
3762
for _, n := range nodes {
38-
vals = append(vals, n.Name, fmt.Sprintf("%v.%v", n.RuntimeLabels.K3dCluster, n.Role), n.style())
63+
if n.applies(opts) {
64+
vals = append(vals, n.Name, fmt.Sprintf("%v.%v", n.RuntimeLabels.K3dCluster, n.Role), n.style())
65+
}
3966
}
4067
return carapace.ActionStyledValuesDescribed(vals...)
4168
})

0 commit comments

Comments
 (0)