Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 29, 2023
1 parent a10807c commit 994537b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
21 changes: 21 additions & 0 deletions completers/k3d_completer/cmd/cluster_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/k3d"
"github.com/spf13/cobra"
)

Expand All @@ -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(),
)
}
2 changes: 1 addition & 1 deletion completers/k3d_completer/cmd/node_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func init() {
nodeCmd.AddCommand(node_deleteCmd)

carapace.Gen(node_deleteCmd).PositionalAnyCompletion(
k3d.ActionNodes().FilterArgs(),
k3d.ActionNodes(k3d.NodeOpts{}.Default()).FilterArgs(),
)
}
31 changes: 29 additions & 2 deletions pkg/actions/tools/k3d/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,14 +31,27 @@ 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
}
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 {
Expand All @@ -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...)
})
Expand Down

0 comments on commit 994537b

Please sign in to comment.