cli/command/service: runScale: use errors.Join, and cleanup

- Use stdlib multi-errors instead of creating our own
- use apiClient instead of client for the API client to
  prevent shadowing imports.
- use dockerCLI with Go's standard camelCase casing.
- rewrite runServiceScale to return warnings, instead of printing them

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-03 19:46:59 +01:00
parent 09b513ecfd
commit 81da375c4d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -2,6 +2,7 @@ package service
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -10,7 +11,7 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions" "github.com/docker/docker/api/types/versions"
"github.com/pkg/errors" "github.com/docker/docker/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -44,8 +45,8 @@ func scaleArgs(cmd *cobra.Command, args []string) error {
} }
for _, arg := range args { for _, arg := range args {
if k, v, ok := strings.Cut(arg, "="); !ok || k == "" || v == "" { if k, v, ok := strings.Cut(arg, "="); !ok || k == "" || v == "" {
return errors.Errorf( return fmt.Errorf(
"Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s", "invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
arg, arg,
cmd.CommandPath(), cmd.CommandPath(),
cmd.UseLine(), cmd.UseLine(),
@ -56,49 +57,48 @@ func scaleArgs(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func runScale(ctx context.Context, dockerCli command.Cli, options *scaleOptions, args []string) error { func runScale(ctx context.Context, dockerCLI command.Cli, options *scaleOptions, args []string) error {
var errs []string apiClient := dockerCLI.Client()
var serviceIDs []string var (
errs []error
serviceIDs = make([]string, 0, len(args))
)
for _, arg := range args { for _, arg := range args {
serviceID, scaleStr, _ := strings.Cut(arg, "=") serviceID, scaleStr, _ := strings.Cut(arg, "=")
// validate input arg scale number // validate input arg scale number
scale, err := strconv.ParseUint(scaleStr, 10, 64) scale, err := strconv.ParseUint(scaleStr, 10, 64)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err)) errs = append(errs, fmt.Errorf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err))
continue continue
} }
if err := runServiceScale(ctx, dockerCli, serviceID, scale); err != nil { warnings, err := runServiceScale(ctx, apiClient, serviceID, scale)
errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) if err != nil {
} else { errs = append(errs, fmt.Errorf("%s: %v", serviceID, err))
serviceIDs = append(serviceIDs, serviceID) continue
} }
for _, warning := range warnings {
_, _ = fmt.Fprintln(dockerCLI.Err(), warning)
}
_, _ = fmt.Fprintf(dockerCLI.Out(), "%s scaled to %d\n", serviceID, scale)
serviceIDs = append(serviceIDs, serviceID)
} }
if len(serviceIDs) > 0 { if len(serviceIDs) > 0 && !options.detach && versions.GreaterThanOrEqualTo(dockerCLI.Client().ClientVersion(), "1.29") {
if !options.detach && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.29") { for _, serviceID := range serviceIDs {
for _, serviceID := range serviceIDs { if err := WaitOnService(ctx, dockerCLI, serviceID, false); err != nil {
if err := WaitOnService(ctx, dockerCli, serviceID, false); err != nil { errs = append(errs, fmt.Errorf("%s: %v", serviceID, err))
errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err))
}
} }
} }
} }
return errors.Join(errs...)
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
} }
func runServiceScale(ctx context.Context, dockerCli command.Cli, serviceID string, scale uint64) error { func runServiceScale(ctx context.Context, apiClient client.ServiceAPIClient, serviceID string, scale uint64) (warnings []string, _ error) {
client := dockerCli.Client() service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil { if err != nil {
return err return nil, err
} }
serviceMode := &service.Spec.Mode serviceMode := &service.Spec.Mode
@ -108,18 +108,12 @@ func runServiceScale(ctx context.Context, dockerCli command.Cli, serviceID strin
case serviceMode.ReplicatedJob != nil: case serviceMode.ReplicatedJob != nil:
serviceMode.ReplicatedJob.TotalCompletions = &scale serviceMode.ReplicatedJob.TotalCompletions = &scale
default: default:
return errors.Errorf("scale can only be used with replicated or replicated-job mode") return nil, errors.New("scale can only be used with replicated or replicated-job mode")
} }
response, err := client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) response, err := apiClient.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
if err != nil { if err != nil {
return err return nil, err
} }
return response.Warnings, nil
for _, warning := range response.Warnings {
_, _ = fmt.Fprintln(dockerCli.Err(), warning)
}
_, _ = fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale)
return nil
} }