1
1
package commands
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"os"
6
7
8
+ "github.com/docker/docker/api/types"
7
9
"github.com/docker/app/internal/cnab"
8
10
"github.com/docker/app/internal/packager"
9
11
"github.com/docker/app/internal/store"
10
12
"github.com/docker/cli/cli"
11
13
"github.com/docker/cli/cli/command"
12
14
"github.com/docker/cli/cli/config"
13
15
"github.com/docker/distribution/reference"
16
+ "github.com/docker/docker/pkg/jsonmessage"
17
+ "github.com/docker/docker/registry"
14
18
"github.com/pkg/errors"
15
19
"github.com/spf13/cobra"
16
20
)
17
21
22
+ type pullOptions struct {
23
+ serviceImages bool
24
+ }
25
+
18
26
func pullCmd (dockerCli command.Cli ) * cobra.Command {
27
+ var opts pullOptions
28
+
19
29
cmd := & cobra.Command {
20
30
Use : "pull APP_IMAGE" ,
21
31
Short : "Pull an App image from a registry" ,
22
32
Example : `$ docker app pull myrepo/myapp:0.1.0` ,
23
33
Args : cli .ExactArgs (1 ),
24
34
RunE : func (cmd * cobra.Command , args []string ) error {
25
- return runPull (dockerCli , args [0 ])
35
+ return runPull (dockerCli , opts , args [0 ])
26
36
},
27
37
}
38
+ cmd .Flags ().BoolVar (& opts .serviceImages , "service-images" , false , "Also pull down service images to this host's context" )
28
39
return cmd
29
40
}
30
41
31
- func runPull (dockerCli command.Cli , name string ) error {
42
+ func pullImage (ctx context.Context , cli command.Cli , image string ) error {
43
+ ref , err := reference .ParseNormalizedNamed (image )
44
+ if err != nil {
45
+ return err
46
+ }
47
+
48
+ // Resolve the Repository name from fqn to RepositoryInfo
49
+ repoInfo , err := registry .ParseRepositoryInfo (ref )
50
+ if err != nil {
51
+ return err
52
+ }
53
+ authConfig := command .ResolveAuthConfig (ctx , cli , repoInfo .Index )
54
+ encodedAuth , err := command .EncodeAuthToBase64 (authConfig )
55
+ if err != nil {
56
+ return err
57
+ }
58
+ options := types.ImagePullOptions {
59
+ RegistryAuth : encodedAuth ,
60
+ }
61
+ responseBody , err := cli .Client ().ImagePull (ctx , image , options )
62
+ if err != nil {
63
+ return err
64
+ }
65
+ defer responseBody .Close ()
66
+
67
+ return jsonmessage .DisplayJSONMessagesStream (responseBody , cli .Out (), cli .Out ().FD (), false , nil )
68
+ }
69
+
70
+ func runPull (dockerCli command.Cli , opts pullOptions , name string ) error {
32
71
appstore , err := store .NewApplicationStore (config .Dir ())
33
72
if err != nil {
34
73
return err
@@ -52,5 +91,15 @@ func runPull(dockerCli command.Cli, name string) error {
52
91
}
53
92
fmt .Fprintf (os .Stdout , "Successfully pulled %q (%s) from %s\n " , bndl .Name , bndl .Version , ref .String ())
54
93
94
+ if opts .serviceImages {
95
+ ctx := context .Background ()
96
+ for name , image := range bndl .Images {
97
+ fmt .Fprintf (os .Stdout , "Pulling: %s -> %s\n " , name , image .Image )
98
+ if err := pullImage (ctx , dockerCli , image .Image ); err != nil {
99
+ return err
100
+ }
101
+ }
102
+ }
103
+
55
104
return nil
56
105
}
0 commit comments