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
}
func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error {
apiClient := dockerCli.Client()
func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error {
apiClient := dockerCLI.Client()
createOpts := types.ServiceCreateOptions{}
service, err := opts.ToService(ctx, apiClient, flags)
@ -87,7 +87,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
return err
}
if err = validateAPIVersion(service, dockerCli.Client().ClientVersion()); err != nil {
if err = validateAPIVersion(service, dockerCLI.Client().ClientVersion()); err != nil {
return err
}
@ -105,14 +105,14 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
return err
}
if err := resolveServiceImageDigestContentTrust(dockerCli, &service); err != nil {
if err := resolveServiceImageDigestContentTrust(dockerCLI, &service); err != nil {
return err
}
// only send auth if flag was set
if opts.registryAuth {
// 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 {
return err
}
@ -130,16 +130,16 @@ func runCreate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
}
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") {
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

View File

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

View File

@ -34,8 +34,8 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runRollback(ctx context.Context, dockerCli command.Cli, options *serviceOptions, serviceID string) error {
apiClient := dockerCli.Client()
func runRollback(ctx context.Context, dockerCLI command.Cli, options *serviceOptions, serviceID string) error {
apiClient := dockerCLI.Client()
service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil {
@ -53,14 +53,14 @@ func runRollback(ctx context.Context, dockerCli command.Cli, options *serviceOpt
}
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") {
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 {
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
}

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)))
}
logrus.Debugf("retrieving target for %s role\n", t.Role)
logrus.Debugf("retrieving target for %s role", t.Role)
h, ok := t.Hashes["sha256"]
if !ok {
return nil, errors.New("no valid hash, expecting sha256")

View File

@ -129,8 +129,8 @@ func newListOptsVarWithValidator(validator opts.ValidatorFctType) *opts.ListOpts
}
//nolint:gocyclo
func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, options *serviceOptions, serviceID string) error {
apiClient := dockerCli.Client()
func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, options *serviceOptions, serviceID string) error {
apiClient := dockerCLI.Client()
service, _, err := apiClient.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil {
@ -188,7 +188,7 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
}
if flags.Changed("image") {
if err := resolveServiceImageDigestContentTrust(dockerCli, spec); err != nil {
if err := resolveServiceImageDigestContentTrust(dockerCLI, spec); err != nil {
return err
}
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
// This would be the old image if it didn't change in this update
image := spec.TaskTemplate.ContainerSpec.Image
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), image)
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image)
if err != nil {
return err
}
@ -242,16 +242,16 @@ func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet,
}
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") {
return nil
}
return WaitOnService(ctx, dockerCli, serviceID, options.quiet)
return WaitOnService(ctx, dockerCLI, serviceID, options.quiet)
}
//nolint:gocyclo