forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
92 lines (74 loc) · 2.02 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/containerd/containerd/namespaces"
"github.com/docker/go-units"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/appcontext"
"golang.org/x/sync/errgroup"
)
const pullUsageShortHelp = `Pull an image or a repository from a registry.`
const pullUsageLongHelp = `Pull an image or a repository from a registry.`
func newPullCommand() *cobra.Command {
pull := &pullCommand{}
cmd := &cobra.Command{
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: pullUsageShortHelp,
Long: pullUsageLongHelp,
Args: validatePullImageArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return pull.Run(args)
},
}
return cmd
}
func validatePullImageArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must pass an image or repository to pull")
}
return nil
}
type pullCommand struct {
image string
}
func (cmd *pullCommand) Run(args []string) (err error) {
reexec()
// Get the specified image.
cmd.image = args[0]
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
fmt.Printf("Pulling %s...\n", cmd.image)
var listedImage *client.ListedImage
// Create the context.
ctx := appcontext.Context()
sess, sessDialer, err := c.Session(ctx)
if err != nil {
return err
}
ctx = session.NewContext(ctx, sess.ID())
ctx = namespaces.WithNamespace(ctx, "buildkit")
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return sess.Run(ctx, sessDialer)
})
eg.Go(func() error {
defer sess.Close()
var err error
listedImage, err = c.Pull(ctx, cmd.image)
return err
})
if err := eg.Wait(); err != nil {
return err
}
fmt.Printf("Pulled: %s\n", listedImage.Target.Digest)
fmt.Printf("Size: %s\n", units.BytesSize(float64(listedImage.ContentSize)))
return nil
}