cli/command/service: minor cleanups: use Println, rename vars

- use Println to print newline instead of custom format
- use apiClient instead of client for the API client to
  prevent shadowing imports.
- use dockerCLI with Go's standard camelCase casing.
- suppress some errors to make my IDE and linters happier

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-01 22:51:46 +01:00
parent 016dbef449
commit aa74f931d3
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 28 additions and 28 deletions

View File

@ -78,8 +78,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error { func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error {
apiClient := dockerCli.Client() apiClient := dockerCLI.Client()
createOpts := types.ServiceCreateOptions{} createOpts := types.ServiceCreateOptions{}
service, err := opts.ToService(ctx, apiClient, flags) service, err := opts.ToService(ctx, apiClient, flags)
@ -87,7 +87,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
return err return err
} }
if err = validateAPIVersion(service, dockerCli.Client().ClientVersion()); err != nil { if err = validateAPIVersion(service, dockerCLI.Client().ClientVersion()); err != nil {
return err return err
} }
@ -105,14 +105,14 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
return err return err
} }
if err := resolveServiceImageDigestContentTrust(dockerCli, &service); err != nil { if err := resolveServiceImageDigestContentTrust(dockerCLI, &service); err != nil {
return err return err
} }
// only send auth if flag was set // only send auth if flag was set
if opts.registryAuth { if opts.registryAuth {
// Retrieve encoded auth token from the image reference // Retrieve encoded auth token from the image reference
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), opts.image) encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), opts.image)
if err != nil { if err != nil {
return err return err
} }
@ -130,16 +130,16 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
} }
for _, warning := range response.Warnings { for _, warning := range response.Warnings {
fmt.Fprintln(dockerCli.Err(), warning) _, _ = fmt.Fprintln(dockerCLI.Err(), warning)
} }
fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID) _, _ = fmt.Fprintln(dockerCLI.Out(), response.ID)
if opts.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") { if opts.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") {
return nil return nil
} }
return WaitOnService(ctx, dockerCli, response.ID, opts.quiet) return WaitOnService(ctx, dockerCLI, response.ID, opts.quiet)
} }
// setConfigs does double duty: it both sets the ConfigReferences of the // setConfigs does double duty: it both sets the ConfigReferences of the

View File

@ -29,17 +29,17 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runRemove(ctx context.Context, dockerCli command.Cli, sids []string) error { func runRemove(ctx context.Context, dockerCLI command.Cli, serviceIDs []string) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
var errs []string var errs []string
for _, sid := range sids { for _, id := range serviceIDs {
err := client.ServiceRemove(ctx, sid) err := apiClient.ServiceRemove(ctx, id)
if err != nil { if err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
continue continue
} }
_, _ = fmt.Fprintf(dockerCli.Out(), "%s\n", sid) _, _ = fmt.Fprintln(dockerCLI.Out(), id)
} }
if len(errs) > 0 { if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n")) return errors.New(strings.Join(errs, "\n"))

View File

@ -34,8 +34,8 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runRollback(ctx context.Context, dockerCli command.Cli, options *serviceOptions, serviceID string) error { func runRollback(ctx context.Context, dockerCLI command.Cli, options *serviceOptions, serviceID string) error {
apiClient := dockerCli.Client() apiClient := dockerCLI.Client()
service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{}) service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil { if err != nil {
@ -53,14 +53,14 @@ func runRollback(ctx context.Context, dockerCli command.Cli, options *serviceOpt
} }
for _, warning := range response.Warnings { for _, warning := range response.Warnings {
fmt.Fprintln(dockerCli.Err(), warning) _, _ = fmt.Fprintln(dockerCLI.Err(), warning)
} }
fmt.Fprintf(dockerCli.Out(), "%s\n", serviceID) _, _ = fmt.Fprintln(dockerCLI.Out(), serviceID)
if options.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") { if options.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") {
return nil return nil
} }
return WaitOnService(ctx, dockerCli, serviceID, options.quiet) return WaitOnService(ctx, dockerCLI, serviceID, options.quiet)
} }

View File

@ -117,9 +117,9 @@ func runServiceScale(ctx context.Context, dockerCli command.Cli, serviceID strin
} }
for _, warning := range response.Warnings { for _, warning := range response.Warnings {
fmt.Fprintln(dockerCli.Err(), warning) _, _ = fmt.Fprintln(dockerCli.Err(), warning)
} }
fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale) _, _ = fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale)
return nil return nil
} }

View File

@ -73,7 +73,7 @@ func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference
return nil, trust.NotaryError(repoInfo.Name.Name(), errors.Errorf("No trust data for %s", reference.FamiliarString(ref))) return nil, trust.NotaryError(repoInfo.Name.Name(), errors.Errorf("No trust data for %s", reference.FamiliarString(ref)))
} }
logrus.Debugf("retrieving target for %s role\n", t.Role) logrus.Debugf("retrieving target for %s role", t.Role)
h, ok := t.Hashes["sha256"] h, ok := t.Hashes["sha256"]
if !ok { if !ok {
return nil, errors.New("no valid hash, expecting sha256") return nil, errors.New("no valid hash, expecting sha256")

View File

@ -129,8 +129,8 @@ func newListOptsVarWithValidator(validator opts.ValidatorFctType) *opts.ListOpts
} }
//nolint:gocyclo //nolint:gocyclo
func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, options *serviceOptions, serviceID string) error { func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, options *serviceOptions, serviceID string) error {
apiClient := dockerCli.Client() apiClient := dockerCLI.Client()
service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{}) service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil { if err != nil {
@ -188,7 +188,7 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
} }
if flags.Changed("image") { if flags.Changed("image") {
if err := resolveServiceImageDigestContentTrust(dockerCli, spec); err != nil { if err := resolveServiceImageDigestContentTrust(dockerCLI, spec); err != nil {
return err return err
} }
if !options.noResolveImage && versions.GreaterThanOrEqualTo(apiClient.ClientVersion(), "1.30") { if !options.noResolveImage && versions.GreaterThanOrEqualTo(apiClient.ClientVersion(), "1.30") {
@ -225,7 +225,7 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
// Retrieve encoded auth token from the image reference // Retrieve encoded auth token from the image reference
// This would be the old image if it didn't change in this update // This would be the old image if it didn't change in this update
image := spec.TaskTemplate.ContainerSpec.Image image := spec.TaskTemplate.ContainerSpec.Image
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), image) encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image)
if err != nil { if err != nil {
return err return err
} }
@ -242,16 +242,16 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
} }
for _, warning := range response.Warnings { for _, warning := range response.Warnings {
fmt.Fprintln(dockerCli.Err(), warning) _, _ = fmt.Fprintln(dockerCLI.Err(), warning)
} }
fmt.Fprintf(dockerCli.Out(), "%s\n", serviceID) _, _ = fmt.Fprintln(dockerCLI.Out(), serviceID)
if options.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") { if options.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") {
return nil return nil
} }
return WaitOnService(ctx, dockerCli, serviceID, options.quiet) return WaitOnService(ctx, dockerCLI, serviceID, options.quiet)
} }
//nolint:gocyclo //nolint:gocyclo