From 4f7f42df0eed6a1b7f03cb2d103db28d5f2db499 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Aug 2025 23:40:43 +0200 Subject: [PATCH] cli/command/stack/swarm: GetStacks: tidy up Preserve the original order by avoiding the intermediate map[string] and keeping an index for the first occurrence of a stack; this also avoids looping multiple times. Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/swarm/list.go | 40 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/cli/command/stack/swarm/list.go b/cli/command/stack/swarm/list.go index c135b06c17..53f968f7b2 100644 --- a/cli/command/stack/swarm/list.go +++ b/cli/command/stack/swarm/list.go @@ -9,37 +9,31 @@ import ( "github.com/pkg/errors" ) -// GetStacks lists the swarm stacks. +// GetStacks lists the swarm stacks with the number of services they contain. // // Deprecated: this function was for internal use and will be removed in the next release. func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]*formatter.Stack, error) { - services, err := apiClient.ServiceList( - ctx, - client.ServiceListOptions{Filters: getAllStacksFilter()}) + services, err := apiClient.ServiceList(ctx, client.ServiceListOptions{ + Filters: getAllStacksFilter(), + }) if err != nil { return nil, err } - m := make(map[string]*formatter.Stack) - for _, service := range services { - labels := service.Spec.Labels - name, ok := labels[convert.LabelNamespace] + + idx := make(map[string]int, len(services)) + out := make([]*formatter.Stack, 0, len(services)) + + for _, svc := range services { + name, ok := svc.Spec.Labels[convert.LabelNamespace] if !ok { - return nil, errors.Errorf("cannot get label %s for service %s", - convert.LabelNamespace, service.ID) + return nil, errors.New("cannot get label " + convert.LabelNamespace + " for service " + svc.ID) } - ztack, ok := m[name] - if !ok { - m[name] = &formatter.Stack{ - Name: name, - Services: 1, - } - } else { - ztack.Services++ + if i, ok := idx[name]; ok { + out[i].Services++ + continue } + idx[name] = len(out) + out = append(out, &formatter.Stack{Name: name, Services: 1}) } - stacks := make([]*formatter.Stack, 0, len(m)) - for _, stack := range m { - stacks = append(stacks, stack) - } - return stacks, nil + return out, nil }