From a16b2d1c74ee203e8c9f0d160e94657b86f3b0c0 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Sat, 1 Mar 2025 22:31:58 +1000 Subject: [PATCH] feat: service ls command include ID column if there are multiple services with same name --- cmd/uncloud/service/list.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/uncloud/service/list.go b/cmd/uncloud/service/list.go index a3f84c68..09f348a2 100644 --- a/cmd/uncloud/service/list.go +++ b/cmd/uncloud/service/list.go @@ -40,8 +40,25 @@ func list(ctx context.Context, uncli *cli.CLI, clusterName string) error { return fmt.Errorf("list services: %w", err) } + serviceNames := make(map[string]struct{}, len(services)) + haveDuplicateNames := false + for _, svc := range services { + if _, exists := serviceNames[svc.Name]; exists { + haveDuplicateNames = true + break + } + serviceNames[svc.Name] = struct{}{} + } + // Print the list of services in a table format. tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + + // Include the ID column if there are duplicate service names to differentiate them. + if haveDuplicateNames { + if _, err = fmt.Fprintf(tw, "ID\t"); err != nil { + return fmt.Errorf("write header: %w", err) + } + } if _, err = fmt.Fprintln(tw, "NAME\tMODE\tREPLICAS\tENDPOINTS"); err != nil { return fmt.Errorf("write header: %w", err) } @@ -49,6 +66,11 @@ func list(ctx context.Context, uncli *cli.CLI, clusterName string) error { endpointsSlice := s.Endpoints() endpoints := strings.Join(endpointsSlice, ", ") + if haveDuplicateNames { + if _, err = fmt.Fprintf(tw, "%s\t", s.ID); err != nil { + return fmt.Errorf("write row: %w", err) + } + } if _, err = fmt.Fprintf(tw, "%s\t%s\t%d\t%s\n", s.Name, s.Mode, len(s.Containers), endpoints); err != nil { return fmt.Errorf("write row: %w", err) }