forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
92 lines (72 loc) · 1.96 KB
/
push.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/genuinetools/img/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/appcontext"
"golang.org/x/sync/errgroup"
)
const pushUsageShortHelp = `Push an image or a repository to a registry.`
const pushUsageLongHelp = `Push an image or a repository to a registry.`
func newPushCommand() *cobra.Command {
push := &pushCommand{}
cmd := &cobra.Command{
Use: "push [OPTIONS] NAME[:TAG]",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: pushUsageShortHelp,
Long: pushUsageLongHelp,
Args: push.ValidateArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return push.Run(args)
},
}
fs := cmd.Flags()
fs.BoolVar(&push.insecure, "insecure-registry", false, "Push to insecure registry")
return cmd
}
type pushCommand struct {
image string
insecure bool
}
func (cmd *pushCommand) ValidateArgs(c *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must pass an image or repository to push")
}
return nil
}
func (cmd *pushCommand) 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("Pushing %s...\n", cmd.image)
// 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()
return c.Push(ctx, cmd.image, cmd.insecure)
})
if err := eg.Wait(); err != nil {
return err
}
fmt.Printf("Successfully pushed %s\n", cmd.image)
return nil
}