cli/command: use shallower interface for completions

The completion functions only need the API-client, and not all of
the CLI. However, passing the API-client as argument would mean
that the API-client is initialized early, which may not be what
we want, so instead, defining an APIClientProvider interface to
preserve the behavior of initializing when needed only.

While updating, also simplify stack.format to only require an
io.Writer.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-06-04 10:56:26 +02:00
parent ce85b24440
commit 20d1b661bc
7 changed files with 35 additions and 24 deletions

View File

@ -2,6 +2,7 @@ package stack
import (
"context"
"io"
"sort"
"github.com/docker/cli/cli"
@ -36,22 +37,22 @@ 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 options.List) error {
ss, err := swarm.GetStacks(ctx, dockerCli)
ss, err := swarm.GetStacks(ctx, dockerCli.Client())
if err != nil {
return err
}
stacks := make([]*formatter.Stack, 0, len(ss))
stacks = append(stacks, ss...)
return format(dockerCli, opts, stacks)
return format(dockerCli.Out(), opts, stacks)
}
func format(dockerCli command.Cli, opts options.List, stacks []*formatter.Stack) error {
func format(out io.Writer, opts options.List, stacks []*formatter.Stack) error {
fmt := formatter.Format(opts.Format)
if fmt == "" || fmt == formatter.TableFormatKey {
fmt = formatter.SwarmStackTableFormat
}
stackCtx := formatter.Context{
Output: dockerCli.Out(),
Output: out,
Format: fmt,
}
sort.Slice(stacks, func(i, j int) bool {