forked from cnabio/cnab-to-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
66 lines (58 loc) · 1.66 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
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/docker/go/canonical/json"
"github.com/spf13/cobra"
)
type pullOptions struct {
bundle string
relocationMap string
targetRef string
insecureRegistries []string
}
func pullCmd() *cobra.Command {
var opts pullOptions
cmd := &cobra.Command{
Use: "pull <ref> [options]",
Short: "Pulls an image reference",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
return runPull(opts)
},
}
cmd.Flags().StringVar(&opts.bundle, "bundle", "pulled.json", "bundle output file (- to print on standard output)")
cmd.Flags().StringVar(&opts.relocationMap, "relocation-map", "relocation-map.json", "relocation map output file (- to print on standard output)")
cmd.Flags().StringSliceVar(&opts.insecureRegistries, "insecure-registries", nil, "Use plain HTTP for those registries")
return cmd
}
func runPull(opts pullOptions) error {
ref, err := reference.ParseNormalizedNamed(opts.targetRef)
if err != nil {
return err
}
b, relocationMap, err := remotes.Pull(context.Background(), ref, createResolver(opts.insecureRegistries))
if err != nil {
return err
}
if err := writeOutput(opts.bundle, b); err != nil {
return err
}
return writeOutput(opts.relocationMap, relocationMap)
}
func writeOutput(file string, data interface{}) error {
bytes, err := json.MarshalCanonical(data)
if err != nil {
return err
}
if file == "-" {
fmt.Fprintln(os.Stdout, string(bytes))
return nil
}
return ioutil.WriteFile(file, bytes, 0644)
}