cli/manifest, cli/command/manifest: use stdlib errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-11 14:45:56 +02:00
parent 097cc9ca64
commit adbe04b5fc
6 changed files with 28 additions and 33 deletions

View File

@ -12,7 +12,6 @@ import (
registryclient "github.com/docker/cli/cli/registry/client"
"github.com/moby/moby/api/types/registry"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -86,11 +85,11 @@ func newAnnotateCommand(dockerCli command.Cli) *cobra.Command {
func runManifestAnnotate(dockerCLI command.Cli, opts annotateOptions) error {
targetRef, err := normalizeReference(opts.target)
if err != nil {
return errors.Wrapf(err, "annotate: error parsing name for manifest list %s", opts.target)
return fmt.Errorf("annotate: error parsing name for manifest list %s: %w", opts.target, err)
}
imgRef, err := normalizeReference(opts.image)
if err != nil {
return errors.Wrapf(err, "annotate: error parsing name for manifest %s", opts.image)
return fmt.Errorf("annotate: error parsing name for manifest %s: %w", opts.image, err)
}
manifestStore := newManifestStore(dockerCLI)
@ -123,7 +122,7 @@ func runManifestAnnotate(dockerCLI command.Cli, opts annotateOptions) error {
}
if !isValidOSArch(imageManifest.Descriptor.Platform.OS, imageManifest.Descriptor.Platform.Architecture) {
return errors.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch)
return fmt.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch)
}
return manifestStore.Save(targetRef, imgRef, imageManifest)
}

View File

@ -2,12 +2,12 @@ package manifest
import (
"context"
"errors"
"fmt"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/manifest/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -38,7 +38,7 @@ func createManifestList(ctx context.Context, dockerCLI command.Cli, args []strin
newRef := args[0]
targetRef, err := normalizeReference(newRef)
if err != nil {
return errors.Wrapf(err, "error parsing name for manifest list %s", newRef)
return fmt.Errorf("error parsing name for manifest list %s: %w", newRef, err)
}
manifestStore := newManifestStore(dockerCLI)
@ -49,7 +49,7 @@ func createManifestList(ctx context.Context, dockerCLI command.Cli, args []strin
case err != nil:
return err
case !opts.amend:
return errors.Errorf("refusing to amend an existing manifest list with no --amend flag")
return errors.New("refusing to amend an existing manifest list with no --amend flag")
}
// Now create the local manifest list transaction by looking up the manifest schemas

View File

@ -11,7 +11,6 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/manifest/types"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -99,14 +98,14 @@ func printManifest(dockerCli command.Cli, manifest types.ImageManifest, opts ins
if err := json.Indent(buffer, raw, "", "\t"); err != nil {
return err
}
fmt.Fprintln(dockerCli.Out(), buffer.String())
_, _ = fmt.Fprintln(dockerCli.Out(), buffer.String())
return nil
}
jsonBytes, err := json.MarshalIndent(manifest, "", "\t")
if err != nil {
return err
}
dockerCli.Out().Write(append(jsonBytes, '\n'))
_, _ = dockerCli.Out().Write(append(jsonBytes, '\n'))
return nil
}
@ -114,12 +113,12 @@ func printManifestList(dockerCli command.Cli, namedRef reference.Named, list []t
if !opts.verbose {
targetRepo := reference.TrimNamed(namedRef)
manifests := []manifestlist.ManifestDescriptor{}
manifests := make([]manifestlist.ManifestDescriptor, 0, len(list))
// More than one response. This is a manifest list.
for _, img := range list {
mfd, err := buildManifestDescriptor(targetRepo, img)
if err != nil {
return errors.Wrap(err, "failed to assemble ManifestDescriptor")
return fmt.Errorf("failed to assemble ManifestDescriptor: %w", err)
}
manifests = append(manifests, mfd)
}
@ -131,13 +130,13 @@ func printManifestList(dockerCli command.Cli, namedRef reference.Named, list []t
if err != nil {
return err
}
fmt.Fprintln(dockerCli.Out(), string(jsonBytes))
_, _ = fmt.Fprintln(dockerCli.Out(), string(jsonBytes))
return nil
}
jsonBytes, err := json.MarshalIndent(list, "", "\t")
if err != nil {
return err
}
dockerCli.Out().Write(append(jsonBytes, '\n'))
_, _ = dockerCli.Out().Write(append(jsonBytes, '\n'))
return nil
}

View File

@ -15,7 +15,6 @@ import (
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/manifest/ocischema"
"github.com/docker/distribution/manifest/schema2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -73,7 +72,7 @@ func runPush(ctx context.Context, dockerCli command.Cli, opts pushOpts) error {
return err
}
if len(manifests) == 0 {
return errors.Errorf("%s not found", targetRef)
return fmt.Errorf("%s not found", targetRef)
}
req, err := buildPushRequest(manifests, targetRef, opts.insecure)
@ -123,13 +122,12 @@ func buildPushRequest(manifests []types.ImageManifest, targetRef reference.Named
func buildManifestList(manifests []types.ImageManifest, targetRef reference.Named) (*manifestlist.DeserializedManifestList, error) {
targetRepo := reference.TrimNamed(targetRef)
descriptors := []manifestlist.ManifestDescriptor{}
descriptors := make([]manifestlist.ManifestDescriptor, 0, len(manifests))
for _, imageManifest := range manifests {
if imageManifest.Descriptor.Platform == nil ||
imageManifest.Descriptor.Platform.Architecture == "" ||
imageManifest.Descriptor.Platform.OS == "" {
return nil, errors.Errorf(
"manifest %s must have an OS and Architecture to be pushed to a registry", imageManifest.Ref)
return nil, fmt.Errorf("manifest %s must have an OS and Architecture to be pushed to a registry", imageManifest.Ref)
}
descriptor, err := buildManifestDescriptor(targetRepo, imageManifest)
if err != nil {
@ -145,7 +143,7 @@ func buildManifestDescriptor(targetRepo reference.Named, imageManifest types.Ima
manifestRepoHostname := reference.Domain(reference.TrimNamed(imageManifest.Ref))
targetRepoHostname := reference.Domain(reference.TrimNamed(targetRepo))
if manifestRepoHostname != targetRepoHostname {
return manifestlist.ManifestDescriptor{}, errors.Errorf("cannot use source images from a different registry than the target image: %s != %s", manifestRepoHostname, targetRepoHostname)
return manifestlist.ManifestDescriptor{}, fmt.Errorf("cannot use source images from a different registry than the target image: %s != %s", manifestRepoHostname, targetRepoHostname)
}
manifest := manifestlist.ManifestDescriptor{
@ -162,8 +160,7 @@ func buildManifestDescriptor(targetRepo reference.Named, imageManifest types.Ima
}
if err := manifest.Descriptor.Digest.Validate(); err != nil {
return manifestlist.ManifestDescriptor{}, errors.Wrapf(err,
"digest parse of image %q failed", imageManifest.Ref)
return manifestlist.ManifestDescriptor{}, fmt.Errorf("digest parse of image %q failed: %w", imageManifest.Ref, err)
}
return manifest, nil
@ -215,7 +212,7 @@ func buildPutManifestRequest(imageManifest types.ImageManifest, targetRef refere
dig := imageManifest.Descriptor.Digest
if dig2 := dig.Algorithm().FromBytes(dt); dig != dig2 {
return mountRequest{}, errors.Errorf("internal digest mismatch for %s: expected %s, got %s", imageManifest.Ref, dig, dig2)
return mountRequest{}, fmt.Errorf("internal digest mismatch for %s: expected %s, got %s", imageManifest.Ref, dig, dig2)
}
var manifest schema2.DeserializedManifest
@ -234,7 +231,7 @@ func buildPutManifestRequest(imageManifest types.ImageManifest, targetRef refere
dig := imageManifest.Descriptor.Digest
if dig2 := dig.Algorithm().FromBytes(dt); dig != dig2 {
return mountRequest{}, errors.Errorf("internal digest mismatch for %s: expected %s, got %s", imageManifest.Ref, dig, dig2)
return mountRequest{}, fmt.Errorf("internal digest mismatch for %s: expected %s, got %s", imageManifest.Ref, dig, dig2)
}
var manifest ocischema.DeserializedManifest
@ -248,15 +245,15 @@ func buildPutManifestRequest(imageManifest types.ImageManifest, targetRef refere
}
func pushList(ctx context.Context, dockerCLI command.Cli, req pushRequest) error {
rclient := newRegistryClient(dockerCLI, req.insecure)
registryClient := newRegistryClient(dockerCLI, req.insecure)
if err := mountBlobs(ctx, rclient, req.targetRef, req.manifestBlobs); err != nil {
if err := mountBlobs(ctx, registryClient, req.targetRef, req.manifestBlobs); err != nil {
return err
}
if err := pushReferences(ctx, dockerCLI.Out(), rclient, req.mountRequests); err != nil {
if err := pushReferences(ctx, dockerCLI.Out(), registryClient, req.mountRequests); err != nil {
return err
}
dgst, err := rclient.PutManifest(ctx, req.targetRef, req.list)
dgst, err := registryClient.PutManifest(ctx, req.targetRef, req.list)
if err != nil {
return err
}

View File

@ -2,6 +2,7 @@ package store
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
@ -11,7 +12,6 @@ import (
"github.com/docker/distribution/manifest/manifestlist"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
// Store manages local storage of image distribution manifests
@ -72,7 +72,7 @@ func (*fsStore) getFromFilename(ref reference.Reference, filename string) (types
return types.ImageManifest{}, err
}
if dgst := digest.FromBytes(raw); dgst != manifestInfo.Digest {
return types.ImageManifest{}, errors.Errorf("invalid manifest file %v: image manifest digest mismatch (%v != %v)", filename, manifestInfo.Digest, dgst)
return types.ImageManifest{}, fmt.Errorf("invalid manifest file %v: image manifest digest mismatch (%v != %v)", filename, manifestInfo.Digest, dgst)
}
manifestInfo.ImageManifest.Descriptor = ocispec.Descriptor{
Digest: manifestInfo.Digest,

View File

@ -2,6 +2,7 @@ package types
import (
"encoding/json"
"fmt"
"github.com/distribution/reference"
"github.com/docker/distribution"
@ -10,7 +11,6 @@ import (
"github.com/docker/distribution/manifest/schema2"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
// ImageManifest contains info to output for a manifest object.
@ -82,7 +82,7 @@ func (i ImageManifest) Payload() (string, []byte, error) {
case i.OCIManifest != nil:
return i.OCIManifest.Payload()
default:
return "", nil, errors.Errorf("%s has no payload", i.Ref)
return "", nil, fmt.Errorf("%s has no payload", i.Ref)
}
}
@ -141,7 +141,7 @@ type SerializableNamed struct {
func (s *SerializableNamed) UnmarshalJSON(b []byte) error {
var raw string
if err := json.Unmarshal(b, &raw); err != nil {
return errors.Wrapf(err, "invalid named reference bytes: %s", b)
return fmt.Errorf("invalid named reference bytes: %s: %w", b, err)
}
var err error
s.Named, err = reference.ParseNamed(raw)