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) }