cli/command/image: runPush: minor cleanups and linting issues

- Remove redundant intermediate variables
- Explicitly use an early return on error instead of combining with
  other checks.
- Fix unhandled errors and combine defers
- Remove outstanding TODO that unlikely will be addressed

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-23 16:18:09 +02:00
parent abe4aa7893
commit c36e67d7b6

View File

@ -92,9 +92,11 @@ To push the complete multi-platform image, remove the --platform flag.
}
ref, err := reference.ParseNormalizedNamed(opts.remote)
switch {
case err != nil:
if err != nil {
return err
}
switch {
case opts.all && !reference.IsNameOnly(ref):
return errors.New("tag can't be used with --all-tags/-a")
case !opts.all && reference.IsNameOnly(ref):
@ -113,34 +115,32 @@ To push the complete multi-platform image, remove the --platform flag.
if err != nil {
return err
}
options := image.PushOptions{
responseBody, err := dockerCli.Client().ImagePush(ctx, reference.FamiliarString(ref), image.PushOptions{
All: opts.all,
RegistryAuth: encodedAuth,
PrivilegeFunc: nil,
Platform: platform,
}
responseBody, err := dockerCli.Client().ImagePush(ctx, reference.FamiliarString(ref), options)
})
if err != nil {
return err
}
defer func() {
_ = responseBody.Close()
for _, note := range notes {
out.PrintNote(note)
}
}()
defer responseBody.Close()
if !opts.untrusted {
// TODO pushTrustedReference currently doesn't respect `--quiet`
return pushTrustedReference(ctx, dockerCli, indexInfo, ref, authConfig, responseBody)
}
if opts.quiet {
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux()))
if err == nil {
fmt.Fprintln(dockerCli.Out(), ref.String())
_, _ = fmt.Fprintln(dockerCli.Out(), ref.String())
}
return err
}