-
Notifications
You must be signed in to change notification settings - Fork 172
/
layer.go
71 lines (56 loc) · 1.49 KB
/
layer.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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/genuinetools/reg/registry"
)
const layerHelp = `Download a layer for a repository.`
func (cmd *layerCommand) Name() string { return "layer" }
func (cmd *layerCommand) Args() string { return "[OPTIONS] NAME[:TAG|@DIGEST]" }
func (cmd *layerCommand) ShortHelp() string { return layerHelp }
func (cmd *layerCommand) LongHelp() string { return layerHelp }
func (cmd *layerCommand) Hidden() bool { return false }
func (cmd *layerCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.output, "output", "", "output file, defaults to stdout")
fs.StringVar(&cmd.output, "o", "", "output file, defaults to stdout")
}
type layerCommand struct {
output string
}
func (cmd *layerCommand) Run(ctx context.Context, args []string) error {
if len(args) < 1 {
return fmt.Errorf("pass the name of the repository")
}
image, err := registry.ParseImage(args[0])
if err != nil {
return err
}
// Create the registry client.
r, err := createRegistryClient(ctx, image.Domain)
if err != nil {
return err
}
// Get the digest.
digest, err := r.Digest(ctx, image)
if err != nil {
return err
}
// Download the layer.
layer, err := r.DownloadLayer(ctx, image.Path, digest)
if err != nil {
return err
}
defer layer.Close()
b, err := ioutil.ReadAll(layer)
if err != nil {
return err
}
if len(cmd.output) > 0 {
return ioutil.WriteFile(cmd.output, b, 0644)
}
fmt.Fprint(os.Stdout, string(b))
return nil
}