feat: ucind: implement cluster list command (#316)

* ucind: implement cluster list

I keep forgetting my ucind clusters, that add the missing list command:
```
% ./ucind cluster list
NAME    MACHINES
miek1   machine-1
```

Just the name would have been enough, but to actually make use of the
tui table a machines list is handy as well.
(There isn't much bubbletea in ucind, but I thought lets start now).

Signed-off-by: Miek Gieben <miek@miek.nl>

* Filter directly when listing

Filter on ucind managed while listing clusters

Signed-off-by: Miek Gieben <miek@miek.nl>

---------

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2026-04-21 19:43:04 +10:00
committed by GitHub
parent 6409a47c71
commit 7e1b91c372
3 changed files with 61 additions and 1 deletions
+40
View File
@@ -0,0 +1,40 @@
package cluster
import (
"fmt"
"strings"
"github.com/psviderski/uncloud/internal/cli/tui"
"github.com/psviderski/uncloud/internal/ucind"
"github.com/spf13/cobra"
)
func NewListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Short: "List clusters.",
RunE: func(cmd *cobra.Command, args []string) error {
p := cmd.Context().Value("provisioner").(*ucind.Provisioner)
clusters, err := p.ListClusters(cmd.Context())
if err != nil {
return fmt.Errorf("list clusters: %w", err)
}
t := tui.NewTable()
t.Headers("NAME", "MACHINES")
for _, cluster := range clusters {
machines := []string{}
for _, m := range cluster.Machines {
machines = append(machines, m.Name)
}
t.Row(
cluster.Name,
strings.Join(machines, tui.Faint.Render(", ")),
)
}
fmt.Println(t)
return nil
},
}
return cmd
}
+1 -1
View File
@@ -13,7 +13,7 @@ func NewRootCommand() *cobra.Command {
} }
cmd.AddCommand( cmd.AddCommand(
NewCreateCommand(), NewCreateCommand(),
// NewListCommand(), NewListCommand(),
NewRemoveCommand(), NewRemoveCommand(),
) )
return cmd return cmd
+20
View File
@@ -322,6 +322,26 @@ func (p *Provisioner) WaitClusterReady(ctx context.Context, c Cluster, timeout t
return nil return nil
} }
func (p *Provisioner) ListClusters(ctx context.Context) ([]Cluster, error) {
nets, err := p.dockerCli.NetworkList(ctx, network.ListOptions{
Filters: filters.NewArgs(
filters.Arg("label", ManagedLabel),
),
})
if err != nil {
return nil, err
}
clusters := []Cluster{}
for _, net := range nets {
cluster, err := p.InspectCluster(ctx, net.Name)
if err == nil {
clusters = append(clusters, cluster)
}
}
return clusters, nil
}
func (p *Provisioner) RemoveCluster(ctx context.Context, name string) error { func (p *Provisioner) RemoveCluster(ctx context.Context, name string) error {
if _, err := p.InspectCluster(ctx, name); err != nil { if _, err := p.InspectCluster(ctx, name); err != nil {
if errors.Is(err, ErrNotFound) { if errors.Is(err, ErrNotFound) {