11package manifest
22
33import (
4+ "context"
45 "fmt"
6+ "path/filepath"
57
68 "github.com/docker/cli/cli"
79 "github.com/docker/cli/cli/command"
10+ "github.com/docker/cli/cli/config"
811 "github.com/docker/cli/cli/manifest/store"
12+ registryclient "github.com/docker/cli/cli/registry/client"
13+ "github.com/docker/docker/api/types/registry"
914 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1015 "github.com/pkg/errors"
1116 "github.com/spf13/cobra"
@@ -21,6 +26,37 @@ type annotateOptions struct {
2126 osVersion string
2227}
2328
29+ // manifestStoreProvider is used in tests to provide a dummy store.
30+ type manifestStoreProvider interface {
31+ // ManifestStore returns a store for local manifests
32+ ManifestStore () store.Store
33+ RegistryClient (bool ) registryclient.RegistryClient
34+ }
35+
36+ // newManifestStore returns a store for local manifests
37+ func newManifestStore (dockerCLI command.Cli ) store.Store {
38+ if msp , ok := dockerCLI .(manifestStoreProvider ); ok {
39+ // manifestStoreProvider is used in tests to provide a dummy store.
40+ return msp .ManifestStore ()
41+ }
42+
43+ // TODO: support override default location from config file
44+ return store .NewStore (filepath .Join (config .Dir (), "manifests" ))
45+ }
46+
47+ // newRegistryClient returns a client for communicating with a Docker distribution
48+ // registry
49+ func newRegistryClient (dockerCLI command.Cli , allowInsecure bool ) registryclient.RegistryClient {
50+ if msp , ok := dockerCLI .(manifestStoreProvider ); ok {
51+ // manifestStoreProvider is used in tests to provide a dummy store.
52+ return msp .RegistryClient (allowInsecure )
53+ }
54+ resolver := func (ctx context.Context , index * registry.IndexInfo ) registry.AuthConfig {
55+ return command .ResolveAuthConfig (dockerCLI .ConfigFile (), index )
56+ }
57+ return registryclient .NewRegistryClient (resolver , command .UserAgent (), allowInsecure )
58+ }
59+
2460// NewAnnotateCommand creates a new `docker manifest annotate` command
2561func newAnnotateCommand (dockerCli command.Cli ) * cobra.Command {
2662 var opts annotateOptions
@@ -47,7 +83,7 @@ func newAnnotateCommand(dockerCli command.Cli) *cobra.Command {
4783 return cmd
4884}
4985
50- func runManifestAnnotate (dockerCli command.Cli , opts annotateOptions ) error {
86+ func runManifestAnnotate (dockerCLI command.Cli , opts annotateOptions ) error {
5187 targetRef , err := normalizeReference (opts .target )
5288 if err != nil {
5389 return errors .Wrapf (err , "annotate: error parsing name for manifest list %s" , opts .target )
@@ -57,7 +93,7 @@ func runManifestAnnotate(dockerCli command.Cli, opts annotateOptions) error {
5793 return errors .Wrapf (err , "annotate: error parsing name for manifest %s" , opts .image )
5894 }
5995
60- manifestStore := dockerCli . ManifestStore ( )
96+ manifestStore := newManifestStore ( dockerCLI )
6197 imageManifest , err := manifestStore .Get (targetRef , imgRef )
6298 switch {
6399 case store .IsNotFound (err ):
0 commit comments