Browse code

Merge pull request #23256 from vdemeester/migrate-pull-push-to-cobra

Use spf13/cobra for docker push and docker pull

Alexander Morozov authored on 2016/06/14 06:16:42
Showing 8 changed files
... ...
@@ -8,8 +8,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
8 8
 		"info":    cli.CmdInfo,
9 9
 		"inspect": cli.CmdInspect,
10 10
 		"ps":      cli.CmdPs,
11
-		"pull":    cli.CmdPull,
12
-		"push":    cli.CmdPush,
13 11
 		"update":  cli.CmdUpdate,
14 12
 	}[name]
15 13
 }
16 14
new file mode 100644
... ...
@@ -0,0 +1,85 @@
0
+package image
1
+
2
+import (
3
+	"errors"
4
+	"fmt"
5
+
6
+	"golang.org/x/net/context"
7
+
8
+	"github.com/docker/docker/api/client"
9
+	"github.com/docker/docker/cli"
10
+	"github.com/docker/docker/reference"
11
+	"github.com/docker/docker/registry"
12
+	"github.com/spf13/cobra"
13
+)
14
+
15
+type pullOptions struct {
16
+	remote string
17
+	all    bool
18
+}
19
+
20
+// NewPullCommand creates a new `docker pull` command
21
+func NewPullCommand(dockerCli *client.DockerCli) *cobra.Command {
22
+	var opts pullOptions
23
+
24
+	cmd := &cobra.Command{
25
+		Use:   "pull [OPTIONS] NAME[:TAG|@DIGEST]",
26
+		Short: "Pull an image or a repository from a registry",
27
+		Args:  cli.ExactArgs(1),
28
+		RunE: func(cmd *cobra.Command, args []string) error {
29
+			opts.remote = args[0]
30
+			return runPull(dockerCli, opts)
31
+		},
32
+	}
33
+
34
+	flags := cmd.Flags()
35
+
36
+	flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
37
+	client.AddTrustedFlags(flags, true)
38
+
39
+	return cmd
40
+}
41
+
42
+func runPull(dockerCli *client.DockerCli, opts pullOptions) error {
43
+	distributionRef, err := reference.ParseNamed(opts.remote)
44
+	if err != nil {
45
+		return err
46
+	}
47
+	if opts.all && !reference.IsNameOnly(distributionRef) {
48
+		return errors.New("tag can't be used with --all-tags/-a")
49
+	}
50
+
51
+	if !opts.all && reference.IsNameOnly(distributionRef) {
52
+		distributionRef = reference.WithDefaultTag(distributionRef)
53
+		fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", reference.DefaultTag)
54
+	}
55
+
56
+	var tag string
57
+	switch x := distributionRef.(type) {
58
+	case reference.Canonical:
59
+		tag = x.Digest().String()
60
+	case reference.NamedTagged:
61
+		tag = x.Tag()
62
+	}
63
+
64
+	registryRef := registry.ParseReference(tag)
65
+
66
+	// Resolve the Repository name from fqn to RepositoryInfo
67
+	repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
68
+	if err != nil {
69
+		return err
70
+	}
71
+
72
+	ctx := context.Background()
73
+
74
+	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
75
+	requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
76
+
77
+	if client.IsTrusted() && !registryRef.HasDigest() {
78
+		// Check if tag is digest
79
+		return dockerCli.TrustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
80
+	}
81
+
82
+	return dockerCli.ImagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, opts.all)
83
+
84
+}
0 85
new file mode 100644
... ...
@@ -0,0 +1,62 @@
0
+package image
1
+
2
+import (
3
+	"golang.org/x/net/context"
4
+
5
+	"github.com/docker/docker/api/client"
6
+	"github.com/docker/docker/cli"
7
+	"github.com/docker/docker/pkg/jsonmessage"
8
+	"github.com/docker/docker/reference"
9
+	"github.com/docker/docker/registry"
10
+	"github.com/spf13/cobra"
11
+)
12
+
13
+// NewPushCommand creates a new `docker push` command
14
+func NewPushCommand(dockerCli *client.DockerCli) *cobra.Command {
15
+	cmd := &cobra.Command{
16
+		Use:   "push NAME[:TAG]",
17
+		Short: "Push an image or a repository to a registry",
18
+		Args:  cli.ExactArgs(1),
19
+		RunE: func(cmd *cobra.Command, args []string) error {
20
+			return runPush(dockerCli, args[0])
21
+		},
22
+	}
23
+
24
+	flags := cmd.Flags()
25
+
26
+	client.AddTrustedFlags(flags, true)
27
+
28
+	return cmd
29
+}
30
+
31
+func runPush(dockerCli *client.DockerCli, remote string) error {
32
+	ref, err := reference.ParseNamed(remote)
33
+	if err != nil {
34
+		return err
35
+	}
36
+
37
+	// Resolve the Repository name from fqn to RepositoryInfo
38
+	repoInfo, err := registry.ParseRepositoryInfo(ref)
39
+	if err != nil {
40
+		return err
41
+	}
42
+
43
+	ctx := context.Background()
44
+
45
+	// Resolve the Auth config relevant for this server
46
+	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
47
+	requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
48
+
49
+	if client.IsTrusted() {
50
+		return dockerCli.TrustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
51
+	}
52
+
53
+	responseBody, err := dockerCli.ImagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
54
+	if err != nil {
55
+		return err
56
+	}
57
+
58
+	defer responseBody.Close()
59
+
60
+	return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
61
+}
0 62
deleted file mode 100644
... ...
@@ -1,90 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"errors"
5
-	"fmt"
6
-
7
-	"golang.org/x/net/context"
8
-
9
-	Cli "github.com/docker/docker/cli"
10
-	"github.com/docker/docker/pkg/jsonmessage"
11
-	flag "github.com/docker/docker/pkg/mflag"
12
-	"github.com/docker/docker/reference"
13
-	"github.com/docker/docker/registry"
14
-	"github.com/docker/engine-api/types"
15
-)
16
-
17
-// CmdPull pulls an image or a repository from the registry.
18
-//
19
-// Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
20
-func (cli *DockerCli) CmdPull(args ...string) error {
21
-	cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
22
-	allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
23
-	addTrustedFlags(cmd, true)
24
-	cmd.Require(flag.Exact, 1)
25
-
26
-	cmd.ParseFlags(args, true)
27
-	remote := cmd.Arg(0)
28
-
29
-	distributionRef, err := reference.ParseNamed(remote)
30
-	if err != nil {
31
-		return err
32
-	}
33
-	if *allTags && !reference.IsNameOnly(distributionRef) {
34
-		return errors.New("tag can't be used with --all-tags/-a")
35
-	}
36
-
37
-	if !*allTags && reference.IsNameOnly(distributionRef) {
38
-		distributionRef = reference.WithDefaultTag(distributionRef)
39
-		fmt.Fprintf(cli.out, "Using default tag: %s\n", reference.DefaultTag)
40
-	}
41
-
42
-	var tag string
43
-	switch x := distributionRef.(type) {
44
-	case reference.Canonical:
45
-		tag = x.Digest().String()
46
-	case reference.NamedTagged:
47
-		tag = x.Tag()
48
-	}
49
-
50
-	registryRef := registry.ParseReference(tag)
51
-
52
-	// Resolve the Repository name from fqn to RepositoryInfo
53
-	repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
54
-	if err != nil {
55
-		return err
56
-	}
57
-
58
-	ctx := context.Background()
59
-
60
-	authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
61
-	requestPrivilege := cli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
62
-
63
-	if IsTrusted() && !registryRef.HasDigest() {
64
-		// Check if tag is digest
65
-		return cli.trustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
66
-	}
67
-
68
-	return cli.imagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, *allTags)
69
-}
70
-
71
-func (cli *DockerCli) imagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
72
-
73
-	encodedAuth, err := EncodeAuthToBase64(authConfig)
74
-	if err != nil {
75
-		return err
76
-	}
77
-	options := types.ImagePullOptions{
78
-		RegistryAuth:  encodedAuth,
79
-		PrivilegeFunc: requestPrivilege,
80
-		All:           all,
81
-	}
82
-
83
-	responseBody, err := cli.client.ImagePull(ctx, ref, options)
84
-	if err != nil {
85
-		return err
86
-	}
87
-	defer responseBody.Close()
88
-
89
-	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
90
-}
91 1
deleted file mode 100644
... ...
@@ -1,68 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"io"
5
-
6
-	"golang.org/x/net/context"
7
-
8
-	Cli "github.com/docker/docker/cli"
9
-	"github.com/docker/docker/pkg/jsonmessage"
10
-	flag "github.com/docker/docker/pkg/mflag"
11
-	"github.com/docker/docker/reference"
12
-	"github.com/docker/docker/registry"
13
-	"github.com/docker/engine-api/types"
14
-)
15
-
16
-// CmdPush pushes an image or repository to the registry.
17
-//
18
-// Usage: docker push NAME[:TAG]
19
-func (cli *DockerCli) CmdPush(args ...string) error {
20
-	cmd := Cli.Subcmd("push", []string{"NAME[:TAG]"}, Cli.DockerCommands["push"].Description, true)
21
-	addTrustedFlags(cmd, false)
22
-	cmd.Require(flag.Exact, 1)
23
-
24
-	cmd.ParseFlags(args, true)
25
-
26
-	ref, err := reference.ParseNamed(cmd.Arg(0))
27
-	if err != nil {
28
-		return err
29
-	}
30
-
31
-	// Resolve the Repository name from fqn to RepositoryInfo
32
-	repoInfo, err := registry.ParseRepositoryInfo(ref)
33
-	if err != nil {
34
-		return err
35
-	}
36
-
37
-	ctx := context.Background()
38
-
39
-	// Resolve the Auth config relevant for this server
40
-	authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
41
-	requestPrivilege := cli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
42
-
43
-	if IsTrusted() {
44
-		return cli.trustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
45
-	}
46
-
47
-	responseBody, err := cli.imagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
48
-	if err != nil {
49
-		return err
50
-	}
51
-
52
-	defer responseBody.Close()
53
-
54
-	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
55
-}
56
-
57
-func (cli *DockerCli) imagePushPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc) (io.ReadCloser, error) {
58
-	encodedAuth, err := EncodeAuthToBase64(authConfig)
59
-	if err != nil {
60
-		return nil, err
61
-	}
62
-	options := types.ImagePushOptions{
63
-		RegistryAuth:  encodedAuth,
64
-		PrivilegeFunc: requestPrivilege,
65
-	}
66
-
67
-	return cli.client.ImagePush(ctx, ref, options)
68
-}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"encoding/json"
6 6
 	"errors"
7 7
 	"fmt"
8
+	"io"
8 9
 	"net"
9 10
 	"net/http"
10 11
 	"net/url"
... ...
@@ -51,7 +52,7 @@ func addTrustedFlags(fs *flag.FlagSet, verify bool) {
51 51
 	fs.BoolVar(&untrusted, []string{"-disable-content-trust"}, !trusted, message)
52 52
 }
53 53
 
54
-// AddTrustedFlags adds the trust flags to a FlagSet
54
+// AddTrustedFlags adds content trust flags to the current command flagset
55 55
 func AddTrustedFlags(fs *pflag.FlagSet, verify bool) {
56 56
 	trusted, message := setupTrustedFlag(verify)
57 57
 	fs.BoolVar(&untrusted, "disable-content-trust", !trusted, message)
... ...
@@ -314,7 +315,8 @@ func notaryError(repoName string, err error) error {
314 314
 	return err
315 315
 }
316 316
 
317
-func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
317
+// TrustedPull handles content trust pulling of an image
318
+func (cli *DockerCli) TrustedPull(ctx context.Context, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
318 319
 	var refs []target
319 320
 
320 321
 	notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig, "pull")
... ...
@@ -376,7 +378,7 @@ func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.Reposi
376 376
 		if err != nil {
377 377
 			return err
378 378
 		}
379
-		if err := cli.imagePullPrivileged(ctx, authConfig, ref.String(), requestPrivilege, false); err != nil {
379
+		if err := cli.ImagePullPrivileged(ctx, authConfig, ref.String(), requestPrivilege, false); err != nil {
380 380
 			return err
381 381
 		}
382 382
 
... ...
@@ -398,8 +400,9 @@ func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.Reposi
398 398
 	return nil
399 399
 }
400 400
 
401
-func (cli *DockerCli) trustedPush(ctx context.Context, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
402
-	responseBody, err := cli.imagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
401
+// TrustedPush handles content trust pushing of an image
402
+func (cli *DockerCli) TrustedPush(ctx context.Context, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
403
+	responseBody, err := cli.ImagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
403 404
 	if err != nil {
404 405
 		return err
405 406
 	}
... ...
@@ -564,3 +567,39 @@ func (cli *DockerCli) addTargetToAllSignableRoles(repo *client.NotaryRepository,
564 564
 
565 565
 	return repo.AddTarget(target, signableRoles...)
566 566
 }
567
+
568
+// ImagePullPrivileged pulls the image and displays it to the output
569
+func (cli *DockerCli) ImagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
570
+
571
+	encodedAuth, err := EncodeAuthToBase64(authConfig)
572
+	if err != nil {
573
+		return err
574
+	}
575
+	options := types.ImagePullOptions{
576
+		RegistryAuth:  encodedAuth,
577
+		PrivilegeFunc: requestPrivilege,
578
+		All:           all,
579
+	}
580
+
581
+	responseBody, err := cli.client.ImagePull(ctx, ref, options)
582
+	if err != nil {
583
+		return err
584
+	}
585
+	defer responseBody.Close()
586
+
587
+	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
588
+}
589
+
590
+// ImagePushPrivileged push the image
591
+func (cli *DockerCli) ImagePushPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc) (io.ReadCloser, error) {
592
+	encodedAuth, err := EncodeAuthToBase64(authConfig)
593
+	if err != nil {
594
+		return nil, err
595
+	}
596
+	options := types.ImagePushOptions{
597
+		RegistryAuth:  encodedAuth,
598
+		PrivilegeFunc: requestPrivilege,
599
+	}
600
+
601
+	return cli.client.ImagePush(ctx, ref, options)
602
+}
... ...
@@ -61,6 +61,8 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
61 61
 		image.NewLoadCommand(dockerCli),
62 62
 		image.NewRemoveCommand(dockerCli),
63 63
 		image.NewSaveCommand(dockerCli),
64
+		image.NewPullCommand(dockerCli),
65
+		image.NewPushCommand(dockerCli),
64 66
 		image.NewSearchCommand(dockerCli),
65 67
 		image.NewImportCommand(dockerCli),
66 68
 		image.NewTagCommand(dockerCli),
... ...
@@ -13,8 +13,6 @@ var DockerCommandUsage = []Command{
13 13
 	{"info", "Display system-wide information"},
14 14
 	{"inspect", "Return low-level information on a container or image"},
15 15
 	{"ps", "List containers"},
16
-	{"pull", "Pull an image or a repository from a registry"},
17
-	{"push", "Push an image or a repository to a registry"},
18 16
 	{"update", "Update configuration of one or more containers"},
19 17
 }
20 18