Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/command/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ func TestInitializeFromClient(t *testing.T) {

for _, tc := range testcases {
t.Run(tc.doc, func(t *testing.T) {
apiclient := &fakeClient{
apiClient := &fakeClient{
pingFunc: tc.pingFunc,
version: defaultVersion,
}

cli := &DockerCli{client: apiclient}
cli := &DockerCli{client: apiClient}
err := cli.Initialize(flags.NewClientOptions())
assert.NilError(t, err)
assert.DeepEqual(t, cli.ServerInfo(), tc.expectedServer)
assert.Equal(t, apiclient.negotiated, tc.negotiated)
assert.Equal(t, apiClient.negotiated, tc.negotiated)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/container/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
t.Run(tc.PullPolicy, func(t *testing.T) {
pullCounter := 0

client := &fakeClient{
apiClient := &fakeClient{
createContainerFunc: func(
config *container.Config,
hostConfig *container.HostConfig,
Expand All @@ -140,7 +140,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
return system.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
},
}
fakeCLI := test.NewFakeCli(client)
fakeCLI := test.NewFakeCli(apiClient)
id, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
name: "name",
platform: runtime.GOOS,
Expand Down
4 changes: 2 additions & 2 deletions cli/command/container/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ func TestGetExecExitStatus(t *testing.T) {
}

for _, testcase := range testcases {
client := &fakeClient{
apiClient := &fakeClient{
execInspectFunc: func(id string) (container.ExecInspect, error) {
assert.Check(t, is.Equal(execID, id))
return container.ExecInspect{ExitCode: testcase.exitCode}, testcase.inspectError
},
}
err := getExecExitStatus(context.Background(), client, execID)
err := getExecExitStatus(context.Background(), apiClient, execID)
assert.Check(t, is.Equal(testcase.expectedError, err))
}
}
Expand Down
20 changes: 10 additions & 10 deletions cli/command/idresolver/idresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import (
)

func TestResolveError(t *testing.T) {
cli := &fakeClient{
apiClient := &fakeClient{
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, errors.New("error inspecting node")
},
}

idResolver := New(cli, false)
idResolver := New(apiClient, false)
_, err := idResolver.Resolve(context.Background(), struct{}{}, "nodeID")

assert.Error(t, err, "unsupported type")
}

func TestResolveWithNoResolveOption(t *testing.T) {
resolved := false
cli := &fakeClient{
apiClient := &fakeClient{
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
resolved = true
return swarm.Node{}, []byte{}, nil
Expand All @@ -37,7 +37,7 @@ func TestResolveWithNoResolveOption(t *testing.T) {
},
}

idResolver := New(cli, true)
idResolver := New(apiClient, true)
id, err := idResolver.Resolve(context.Background(), swarm.Node{}, "nodeID")

assert.NilError(t, err)
Expand All @@ -47,14 +47,14 @@ func TestResolveWithNoResolveOption(t *testing.T) {

func TestResolveWithCache(t *testing.T) {
inspectCounter := 0
cli := &fakeClient{
apiClient := &fakeClient{
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
inspectCounter++
return *builders.Node(builders.NodeName("node-foo")), []byte{}, nil
},
}

idResolver := New(cli, false)
idResolver := New(apiClient, false)

ctx := context.Background()
for i := 0; i < 2; i++ {
Expand Down Expand Up @@ -97,10 +97,10 @@ func TestResolveNode(t *testing.T) {

ctx := context.Background()
for _, tc := range testCases {
cli := &fakeClient{
apiClient := &fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
}
idResolver := New(cli, false)
idResolver := New(apiClient, false)
id, err := idResolver.Resolve(ctx, swarm.Node{}, tc.nodeID)

assert.NilError(t, err)
Expand Down Expand Up @@ -132,10 +132,10 @@ func TestResolveService(t *testing.T) {

ctx := context.Background()
for _, tc := range testCases {
cli := &fakeClient{
apiClient := &fakeClient{
serviceInspectFunc: tc.serviceInspectFunc,
}
idResolver := New(cli, false)
idResolver := New(apiClient, false)
id, err := idResolver.Resolve(ctx, swarm.Service{}, tc.serviceID)

assert.NilError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions cli/command/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
client := dockerCli.Client()
networkResources, err := client.NetworkList(ctx, network.ListOptions{Filters: options.filter.Value()})
func runList(ctx context.Context, dockerCLI command.Cli, options listOptions) error {
apiClient := dockerCLI.Client()
networkResources, err := apiClient.NetworkList(ctx, network.ListOptions{Filters: options.filter.Value()})
if err != nil {
return err
}

format := options.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().NetworksFormat
if len(dockerCLI.ConfigFile().NetworksFormat) > 0 && !options.quiet {
format = dockerCLI.ConfigFile().NetworksFormat
} else {
format = formatter.TableFormatKey
}
Expand All @@ -66,7 +66,7 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
})

networksCtx := formatter.Context{
Output: dockerCli.Out(),
Output: dockerCLI.Out(),
Format: newFormat(format, options.quiet),
Trunc: !options.noTrunc,
}
Expand Down
2 changes: 2 additions & 0 deletions cli/command/node/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Reference(ctx context.Context, apiClient client.APIClient, ref string) (str
// If there's no node ID in /info, the node probably
// isn't a manager. Call a swarm-specific endpoint to
// get a more specific error message.
//
// FIXME(thaJeztah): this should not require calling a Swarm endpoint, and we could just suffice with info / ping (which has swarm status).
_, err = apiClient.NodeList(ctx, swarm.NodeListOptions{})
if err != nil {
return "", err
Expand Down
11 changes: 5 additions & 6 deletions cli/command/node/demote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ func newDemoteCommand(dockerCli command.Cli) *cobra.Command {
}
}

func runDemote(ctx context.Context, dockerCli command.Cli, nodes []string) error {
func runDemote(ctx context.Context, dockerCLI command.Cli, nodes []string) error {
demote := func(node *swarm.Node) error {
if node.Spec.Role == swarm.NodeRoleWorker {
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s is already a worker.\n", node.ID)
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s is already a worker.\n", node.ID)
return errNoRoleChange
}
node.Spec.Role = swarm.NodeRoleWorker
return nil
}
success := func(nodeID string) {
_, _ = fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", nodeID)
}
return updateNodes(ctx, dockerCli, nodes, demote, success)
return updateNodes(ctx, dockerCLI.Client(), nodes, demote, func(nodeID string) {
_, _ = fmt.Fprintf(dockerCLI.Out(), "Manager %s demoted in the swarm.\n", nodeID)
})
}
15 changes: 7 additions & 8 deletions cli/command/node/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,31 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
client := dockerCli.Client()
func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
apiClient := dockerCLI.Client()

if opts.pretty {
opts.format = "pretty"
}

getRef := func(ref string) (any, []byte, error) {
nodeRef, err := Reference(ctx, client, ref)
nodeRef, err := Reference(ctx, apiClient, ref)
if err != nil {
return nil, nil, err
}
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeRef)
return node, nil, err
}
f := opts.format

// check if the user is trying to apply a template to the pretty format, which
// is not supported
if strings.HasPrefix(f, "pretty") && f != "pretty" {
if strings.HasPrefix(opts.format, "pretty") && opts.format != "pretty" {
return errors.New("cannot supply extra formatting options to the pretty template")
}

nodeCtx := formatter.Context{
Output: dockerCli.Out(),
Format: newFormat(f, false),
Output: dockerCLI.Out(),
Format: newFormat(opts.format, false),
}

if err := inspectFormatWrite(nodeCtx, opts.nodeIds, getRef); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions cli/command/node/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
client := dockerCli.Client()
func runList(ctx context.Context, dockerCLI command.Cli, options listOptions) error {
apiClient := dockerCLI.Client()

nodes, err := client.NodeList(
ctx,
swarm.NodeListOptions{Filters: options.filter.Value()})
nodes, err := apiClient.NodeList(ctx, swarm.NodeListOptions{
Filters: options.filter.Value(),
})
if err != nil {
return err
}

info := system.Info{}
var info system.Info
if len(nodes) > 0 && !options.quiet {
// only non-empty nodes and not quiet, should we call /info api
info, err = client.Info(ctx)
info, err = apiClient.Info(ctx)
if err != nil {
return err
}
Expand All @@ -72,13 +72,13 @@ func runList(ctx context.Context, dockerCli command.Cli, options listOptions) er
format := options.format
if len(format) == 0 {
format = formatter.TableFormatKey
if len(dockerCli.ConfigFile().NodesFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().NodesFormat
if len(dockerCLI.ConfigFile().NodesFormat) > 0 && !options.quiet {
format = dockerCLI.ConfigFile().NodesFormat
}
}

nodesCtx := formatter.Context{
Output: dockerCli.Out(),
Output: dockerCLI.Out(),
Format: newFormat(format, options.quiet),
}
sort.Slice(nodes, func(i, j int) bool {
Expand Down
11 changes: 5 additions & 6 deletions cli/command/node/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ func newPromoteCommand(dockerCli command.Cli) *cobra.Command {
}
}

func runPromote(ctx context.Context, dockerCli command.Cli, nodes []string) error {
func runPromote(ctx context.Context, dockerCLI command.Cli, nodes []string) error {
promote := func(node *swarm.Node) error {
if node.Spec.Role == swarm.NodeRoleManager {
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s is already a manager.\n", node.ID)
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s is already a manager.\n", node.ID)
return errNoRoleChange
}
node.Spec.Role = swarm.NodeRoleManager
return nil
}
success := func(nodeID string) {
_, _ = fmt.Fprintf(dockerCli.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
}
return updateNodes(ctx, dockerCli, nodes, promote, success)
return updateNodes(ctx, dockerCLI.Client(), nodes, promote, func(nodeID string) {
_, _ = fmt.Fprintf(dockerCLI.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
})
}
14 changes: 7 additions & 7 deletions cli/command/node/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error {
client := dockerCli.Client()
func runPs(ctx context.Context, dockerCLI command.Cli, options psOptions) error {
apiClient := dockerCLI.Client()

var (
errs []string
tasks []swarm.Task
)

for _, nodeID := range options.nodeIDs {
nodeRef, err := Reference(ctx, client, nodeID)
nodeRef, err := Reference(ctx, apiClient, nodeID)
if err != nil {
errs = append(errs, err.Error())
continue
}

node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeRef)
if err != nil {
errs = append(errs, err.Error())
continue
Expand All @@ -83,7 +83,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error
filter := options.filter.Value()
filter.Add("node", node.ID)

nodeTasks, err := client.TaskList(ctx, swarm.TaskListOptions{Filters: filter})
nodeTasks, err := apiClient.TaskList(ctx, swarm.TaskListOptions{Filters: filter})
if err != nil {
errs = append(errs, err.Error())
continue
Expand All @@ -94,11 +94,11 @@ func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error

format := options.format
if len(format) == 0 {
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
format = task.DefaultFormat(dockerCLI.ConfigFile(), options.quiet)
}

if len(errs) == 0 || len(tasks) != 0 {
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
if err := task.Print(ctx, dockerCLI, tasks, idresolver.New(apiClient, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
errs = append(errs, err.Error())
}
}
Expand Down
18 changes: 8 additions & 10 deletions cli/command/node/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/opts"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -47,18 +48,15 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
success := func(_ string) {
fmt.Fprintln(dockerCli.Out(), nodeID)
}
return updateNodes(ctx, dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, nodeID string) error {
return updateNodes(ctx, dockerCLI.Client(), []string{nodeID}, mergeNodeUpdate(flags), func(_ string) {
_, _ = fmt.Fprintln(dockerCLI.Out(), nodeID)
})
}

func updateNodes(ctx context.Context, dockerCli command.Cli, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
client := dockerCli.Client()

func updateNodes(ctx context.Context, apiClient client.NodeAPIClient, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
for _, nodeID := range nodes {
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeID)
if err != nil {
return err
}
Expand All @@ -70,7 +68,7 @@ func updateNodes(ctx context.Context, dockerCli command.Cli, nodes []string, mer
}
return err
}
err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
err = apiClient.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
if err != nil {
return err
}
Expand Down
Loading
Loading