forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.go
82 lines (66 loc) · 1.99 KB
/
save.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
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"github.com/containerd/containerd/namespaces"
"github.com/docker/docker/pkg/term"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
// TODO(AkihiroSuda): support OCI archive
const saveHelp = `Save an image to a tar archive (streamed to STDOUT by default).`
func (cmd *saveCommand) Name() string { return "save" }
func (cmd *saveCommand) Args() string { return "[OPTIONS] IMAGE [IMAGE...]" }
func (cmd *saveCommand) ShortHelp() string { return saveHelp }
func (cmd *saveCommand) LongHelp() string { return saveHelp }
func (cmd *saveCommand) Hidden() bool { return false }
func (cmd *saveCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.output, "output", "", "write to a file, instead of STDOUT")
fs.StringVar(&cmd.output, "o", "", "write to a file, instead of STDOUT")
fs.StringVar(&cmd.format, "format", "docker", "image output format (docker|oci)")
}
type saveCommand struct {
output string
format string
}
func (cmd *saveCommand) Run(ctx context.Context, args []string) (err error) {
if len(args) < 1 {
return fmt.Errorf("must pass an image to save")
}
reexec()
// Create the context.
id := identity.NewID()
ctx = session.NewContext(ctx, id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
// Create the writer.
writer, err := cmd.writer()
if err != nil {
return err
}
// Loop over the arguments as images and run save.
for _, image := range args {
if err := c.SaveImage(ctx, image, cmd.format, writer); err != nil {
return err
}
}
return nil
}
func (cmd *saveCommand) writer() (io.WriteCloser, error) {
if cmd.output != "" {
return os.Create(cmd.output)
}
if term.IsTerminal(os.Stdout.Fd()) {
return nil, fmt.Errorf("cowardly refusing to save to a terminal. Use the -o flag or redirect")
}
return os.Stdout, nil
}