forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
70 lines (58 loc) · 1.55 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
67
68
69
70
package main
import (
"context"
"fmt"
"io/ioutil"
"path"
"github.com/shizhMSFT/oras/pkg/oras"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type pullOptions struct {
targetRef string
output string
verbose bool
debug bool
username string
password string
}
func pullCmd() *cobra.Command {
var opts pullOptions
cmd := &cobra.Command{
Use: "pull name[:tag|@digest]",
Short: "Pull files from remote registry",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
return runPull(opts)
},
}
cmd.Flags().StringVarP(&opts.output, "output", "o", "", "output directory")
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "verbose output")
cmd.Flags().BoolVarP(&opts.debug, "debug", "d", false, "debug mode")
cmd.Flags().StringVarP(&opts.username, "username", "u", "", "registry username")
cmd.Flags().StringVarP(&opts.password, "password", "p", "", "registry password")
return cmd
}
func runPull(opts pullOptions) error {
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
}
resolver := newResolver(opts.username, opts.password)
contents, err := oras.Pull(context.Background(), resolver, opts.targetRef)
if err != nil {
return err
}
for name, content := range contents {
if opts.output != "" {
name = path.Join(opts.output, name)
}
if err := ioutil.WriteFile(name, content, 0644); err != nil {
return err
}
if opts.verbose {
fmt.Println(name)
}
}
return nil
}