Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 28, 2023
1 parent 06348f4 commit b696749
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions completers/k3d_completer/cmd/cluster_delete.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 @@ -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(),
)
}
41 changes: 41 additions & 0 deletions pkg/actions/tools/k3d/cluster.go
Original file line number Diff line number Diff line change
@@ -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...)
})
}

0 comments on commit b696749

Please sign in to comment.