From 6647e229bef7dd75cd7c987c64ad342e47e4a77e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Sep 2025 09:37:06 +0200 Subject: [PATCH] cli/command/stack: move GetStacks and StackWrite internal These were deprecated in 036d3a6bab54fdffd9804ab2367fb9a14e62893b and 30774ed1f261493fa94bbdebc448807d4fdd70b6, and were originally in the cli/command/stack package, but moved for the (now deprecated) Compose on Kubernetes feature in 4d947de2927ab35a734a0c816a01f54744d3d429. Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/cmd.go | 3 +- cli/command/stack/common.go | 6 ++ cli/command/stack/formatter/formatter.go | 80 ------------------- cli/command/stack/list.go | 21 ++--- cli/command/stack/list_formatter.go | 53 ++++++++++++ ...rmatter_test.go => list_formatter_test.go} | 6 +- .../stack/{swarm/list.go => list_utils.go} | 13 ++- cli/command/stack/swarm/common.go | 6 -- 8 files changed, 76 insertions(+), 112 deletions(-) delete mode 100644 cli/command/stack/formatter/formatter.go create mode 100644 cli/command/stack/list_formatter.go rename cli/command/stack/{formatter/formatter_test.go => list_formatter_test.go} (92%) rename cli/command/stack/{swarm/list.go => list_utils.go} (59%) diff --git a/cli/command/stack/cmd.go b/cli/command/stack/cmd.go index 85af208f7..eb23b5d73 100644 --- a/cli/command/stack/cmd.go +++ b/cli/command/stack/cmd.go @@ -6,7 +6,6 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" - "github.com/docker/cli/cli/command/stack/swarm" "github.com/docker/cli/internal/commands" "github.com/spf13/cobra" ) @@ -53,7 +52,7 @@ func newStackCommand(dockerCLI command.Cli) *cobra.Command { // completeNames offers completion for swarm stacks func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - list, err := swarm.GetStacks(cmd.Context(), dockerCLI.Client()) + list, err := getStacks(cmd.Context(), dockerCLI.Client()) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cli/command/stack/common.go b/cli/command/stack/common.go index 40b1db2cf..cbf72486f 100644 --- a/cli/command/stack/common.go +++ b/cli/command/stack/common.go @@ -39,3 +39,9 @@ func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args { filter.Add("label", convert.LabelNamespace+"="+namespace) return filter } + +func getAllStacksFilter() filters.Args { + filter := filters.NewArgs() + filter.Add("label", convert.LabelNamespace) + return filter +} diff --git a/cli/command/stack/formatter/formatter.go b/cli/command/stack/formatter/formatter.go deleted file mode 100644 index 8605c01f3..000000000 --- a/cli/command/stack/formatter/formatter.go +++ /dev/null @@ -1,80 +0,0 @@ -package formatter - -import ( - "strconv" - - "github.com/docker/cli/cli/command/formatter" -) - -const ( - // SwarmStackTableFormat is the default Swarm stack format - // - // Deprecated: this type was for internal use and will be removed in the next release. - SwarmStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}" - - stackServicesHeader = "SERVICES" - - // TableFormatKey is an alias for formatter.TableFormatKey - // - // Deprecated: this type was for internal use and will be removed in the next release. - TableFormatKey = formatter.TableFormatKey -) - -// Context is an alias for formatter.Context -// -// Deprecated: this type was for internal use and will be removed in the next release. -type Context = formatter.Context - -// Format is an alias for formatter.Format -// -// Deprecated: this type was for internal use and will be removed in the next release. -type Format = formatter.Format - -// Stack contains deployed stack information. -// -// Deprecated: this type was for internal use and will be removed in the next release. -type Stack struct { - // Name is the name of the stack - Name string - // Services is the number of the services - Services int -} - -// StackWrite writes formatted stacks using the Context -// -// Deprecated: this function was for internal use and will be removed in the next release. -func StackWrite(ctx formatter.Context, stacks []Stack) error { - fmtCtx := &stackContext{ - HeaderContext: formatter.HeaderContext{ - Header: formatter.SubHeaderContext{ - "Name": formatter.NameHeader, - "Services": stackServicesHeader, - }, - }, - } - return ctx.Write(fmtCtx, func(format func(subContext formatter.SubContext) error) error { - for _, stack := range stacks { - if err := format(&stackContext{s: stack}); err != nil { - return err - } - } - return nil - }) -} - -type stackContext struct { - formatter.HeaderContext - s Stack -} - -func (s *stackContext) MarshalJSON() ([]byte, error) { - return formatter.MarshalJSON(s) -} - -func (s *stackContext) Name() string { - return s.s.Name -} - -func (s *stackContext) Services() string { - return strconv.Itoa(s.s.Services) -} diff --git a/cli/command/stack/list.go b/cli/command/stack/list.go index 084b6aab3..6676982e2 100644 --- a/cli/command/stack/list.go +++ b/cli/command/stack/list.go @@ -2,13 +2,11 @@ package stack import ( "context" - "io" "sort" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "github.com/docker/cli/cli/command/stack/formatter" - "github.com/docker/cli/cli/command/stack/swarm" + "github.com/docker/cli/cli/command/formatter" flagsHelper "github.com/docker/cli/cli/flags" "github.com/fvbommel/sortorder" "github.com/spf13/cobra" @@ -40,24 +38,21 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command { // runList performs a stack list against the specified swarm cluster func runList(ctx context.Context, dockerCLI command.Cli, opts listOptions) error { - stacks, err := swarm.GetStacks(ctx, dockerCLI.Client()) + stacks, err := getStacks(ctx, dockerCLI.Client()) if err != nil { return err } - return format(dockerCLI.Out(), opts, stacks) -} -func format(out io.Writer, opts listOptions, stacks []formatter.Stack) error { - fmt := formatter.Format(opts.format) - if fmt == "" || fmt == formatter.TableFormatKey { - fmt = formatter.SwarmStackTableFormat + format := formatter.Format(opts.format) + if format == "" || format == formatter.TableFormatKey { + format = stackTableFormat } stackCtx := formatter.Context{ - Output: out, - Format: fmt, + Output: dockerCLI.Out(), + Format: format, } sort.Slice(stacks, func(i, j int) bool { return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) }) - return formatter.StackWrite(stackCtx, stacks) + return stackWrite(stackCtx, stacks) } diff --git a/cli/command/stack/list_formatter.go b/cli/command/stack/list_formatter.go new file mode 100644 index 000000000..1aa4f97e0 --- /dev/null +++ b/cli/command/stack/list_formatter.go @@ -0,0 +1,53 @@ +package stack + +import ( + "strconv" + + "github.com/docker/cli/cli/command/formatter" +) + +// stackTableFormat is the default Swarm stack format +const stackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}" + +// stackSummary contains deployed stack information. +type stackSummary struct { + Name string // Name is the name of the stack. + Services int // Services is the number services in the stack. +} + +// stackWrite writes formatted stacks using the Context +func stackWrite(fmtCtx formatter.Context, stacks []stackSummary) error { + stackCtx := &stackContext{ + HeaderContext: formatter.HeaderContext{ + Header: formatter.SubHeaderContext{ + "Name": formatter.NameHeader, + "Services": "SERVICES", + }, + }, + } + return fmtCtx.Write(stackCtx, func(format func(subContext formatter.SubContext) error) error { + for _, stack := range stacks { + if err := format(&stackContext{s: stack}); err != nil { + return err + } + } + return nil + }) +} + +type stackContext struct { + formatter.HeaderContext + s stackSummary +} + +func (s *stackContext) MarshalJSON() ([]byte, error) { + return formatter.MarshalJSON(s) +} + +func (s *stackContext) Name() string { + return s.s.Name +} + +func (s *stackContext) Services() string { + return strconv.Itoa(s.s.Services) +} diff --git a/cli/command/stack/formatter/formatter_test.go b/cli/command/stack/list_formatter_test.go similarity index 92% rename from cli/command/stack/formatter/formatter_test.go rename to cli/command/stack/list_formatter_test.go index 5285af008..99dcd43d4 100644 --- a/cli/command/stack/formatter/formatter_test.go +++ b/cli/command/stack/list_formatter_test.go @@ -1,4 +1,4 @@ -package formatter +package stack import ( "bytes" @@ -26,7 +26,7 @@ func TestStackContextWrite(t *testing.T) { }, { name: "table format", - format: SwarmStackTableFormat, + format: stackTableFormat, expected: `NAME SERVICES baz 2 bar 1 @@ -56,7 +56,7 @@ bar Format: tc.format, Output: &out, } - if err := StackWrite(fmtCtx, []Stack{ + if err := stackWrite(fmtCtx, []stackSummary{ {Name: "baz", Services: 2}, {Name: "bar", Services: 1}, }); err != nil { diff --git a/cli/command/stack/swarm/list.go b/cli/command/stack/list_utils.go similarity index 59% rename from cli/command/stack/swarm/list.go rename to cli/command/stack/list_utils.go index f8b21051e..b085642ea 100644 --- a/cli/command/stack/swarm/list.go +++ b/cli/command/stack/list_utils.go @@ -1,18 +1,15 @@ -package swarm +package stack import ( "context" - "github.com/docker/cli/cli/command/stack/formatter" "github.com/docker/cli/cli/compose/convert" "github.com/moby/moby/client" "github.com/pkg/errors" ) -// 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) { +// getStacks lists the swarm stacks with the number of services they contain. +func getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) { services, err := apiClient.ServiceList(ctx, client.ServiceListOptions{ Filters: getAllStacksFilter(), }) @@ -21,7 +18,7 @@ func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]format } idx := make(map[string]int, len(services)) - out := make([]formatter.Stack, 0, len(services)) + out := make([]stackSummary, 0, len(services)) for _, svc := range services { name, ok := svc.Spec.Labels[convert.LabelNamespace] @@ -33,7 +30,7 @@ func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]format continue } idx[name] = len(out) - out = append(out, formatter.Stack{Name: name, Services: 1}) + out = append(out, stackSummary{Name: name, Services: 1}) } return out, nil } diff --git a/cli/command/stack/swarm/common.go b/cli/command/stack/swarm/common.go index 3d0578aa5..691508efa 100644 --- a/cli/command/stack/swarm/common.go +++ b/cli/command/stack/swarm/common.go @@ -16,12 +16,6 @@ func getStackFilter(namespace string) filters.Args { return filter } -func getAllStacksFilter() filters.Args { - filter := filters.NewArgs() - filter.Add("label", convert.LabelNamespace) - return filter -} - func getStackServices(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Service, error) { return apiclient.ServiceList(ctx, client.ServiceListOptions{Filters: getStackFilter(namespace)}) }