From b696749a26b3c84c70a562c71aabc581307561b8 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 28 Sep 2023 11:10:24 +0200 Subject: [PATCH] tmp --- .../k3d_completer/cmd/cluster_delete.go | 5 +++ pkg/actions/tools/k3d/cluster.go | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 pkg/actions/tools/k3d/cluster.go diff --git a/completers/k3d_completer/cmd/cluster_delete.go b/completers/k3d_completer/cmd/cluster_delete.go index 29143faf74..38aceedce2 100644 --- a/completers/k3d_completer/cmd/cluster_delete.go +++ b/completers/k3d_completer/cmd/cluster_delete.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" ) @@ -18,4 +19,8 @@ func init() { cluster_deleteCmd.Flags().BoolP("all", "a", false, "Delete all existing clusters") cluster_deleteCmd.Flags().StringP("config", "c", "", "Path of a config file to use") clusterCmd.AddCommand(cluster_deleteCmd) + + carapace.Gen(cluster_deleteCmd).PositionalAnyCompletion( + k3d.ActionClusters().FilterArgs(), + ) } diff --git a/pkg/actions/tools/k3d/cluster.go b/pkg/actions/tools/k3d/cluster.go new file mode 100644 index 0000000000..6f3bc47854 --- /dev/null +++ b/pkg/actions/tools/k3d/cluster.go @@ -0,0 +1,41 @@ +package k3d + +import ( + "encoding/json" + + "github.com/rsteube/carapace" + "github.com/rsteube/carapace/pkg/style" +) + +type cluster struct { + Name string + ImageVolume string + ServersRunning int + ServersCount int +} + +func (c cluster) style() string { + switch { + case c.ServersRunning == 0: + return style.Carapace.KeywordNegative + case c.ServersRunning < c.ServersCount: + return style.Carapace.KeywordAmbiguous + default: + return style.Carapace.KeywordPositive + } +} + +func ActionClusters() carapace.Action { + return carapace.ActionExecCommand("k3d", "cluster", "list", "--output", "json")(func(output []byte) carapace.Action { + var clusters []cluster + if err := json.Unmarshal(output, &clusters); err != nil { + return carapace.ActionMessage(err.Error()) + } + + vals := make([]string, 0) + for _, c := range clusters { + vals = append(vals, c.Name, c.ImageVolume, c.style()) + } + return carapace.ActionStyledValuesDescribed(vals...) + }) +}