mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: sort services in 'uc ls' output by name
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/psviderski/uncloud/internal/cli"
|
"github.com/psviderski/uncloud/internal/cli"
|
||||||
|
"github.com/psviderski/uncloud/pkg/api"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,15 +38,15 @@ func list(ctx context.Context, uncli *cli.CLI) error {
|
|||||||
return fmt.Errorf("list services: %w", err)
|
return fmt.Errorf("list services: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceNames := make(map[string]struct{}, len(services))
|
// Sort services by name.
|
||||||
haveDuplicateNames := false
|
haveDuplicateNames := false
|
||||||
for _, svc := range services {
|
slices.SortFunc(services, func(a, b api.Service) int {
|
||||||
if _, exists := serviceNames[svc.Name]; exists {
|
if a.Name == b.Name {
|
||||||
haveDuplicateNames = true
|
haveDuplicateNames = true
|
||||||
break
|
return strings.Compare(a.ID, b.ID)
|
||||||
}
|
}
|
||||||
serviceNames[svc.Name] = struct{}{}
|
return strings.Compare(a.Name, b.Name)
|
||||||
}
|
})
|
||||||
|
|
||||||
// Print the list of services in a table format.
|
// Print the list of services in a table format.
|
||||||
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user