-
Notifications
You must be signed in to change notification settings - Fork 231
/
tag.go
75 lines (58 loc) · 1.64 KB
/
tag.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
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/containerd/containerd/namespaces"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const tagUsageShortHelp = `Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE.`
const tagUsageLongHelp = `Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE.`
func newTagCommand() *cobra.Command {
tag := &tagCommand{}
cmd := &cobra.Command{
Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: tagUsageShortHelp,
Long: tagUsageLongHelp,
Args: tag.ValidateArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return tag.Run(args)
},
}
return cmd
}
type tagCommand struct {
image string
target string
}
func (cmd *tagCommand) ValidateArgs(c *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("must pass an image or repository and target to tag")
}
return nil
}
func (cmd *tagCommand) Run(args []string) (err error) {
reexec()
// Get the specified image and target.
cmd.image = args[0]
cmd.target = args[1]
// Create the context.
id := identity.NewID()
ctx := session.NewContext(context.Background(), id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
if err := c.TagImage(ctx, cmd.image, cmd.target); err != nil {
return err
}
fmt.Printf("Successfully tagged %s as %s\n", cmd.image, cmd.target)
return nil
}