-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathload.go
73 lines (61 loc) · 1.63 KB
/
load.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
package main
import (
"context"
"io"
"os"
"github.com/spf13/cobra"
)
// loadDescription is used to describe load command in detail and auto generate command doc.
var loadDescription = "load a set of images by tar stream.\n" +
"for docker image format, no need to set the image name because pouch" +
" will parse image name from tar stream."
// LoadCommand use to implement 'load' command.
type LoadCommand struct {
baseCommand
input string
}
// Init initialize load command.
func (l *LoadCommand) Init(c *Cli) {
l.cli = c
l.cmd = &cobra.Command{
Use: "load [OPTIONS] [IMAGE_NAME]",
Short: "load a set of images from a tar archive or STDIN",
Long: loadDescription,
Args: cobra.MaximumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
return l.runLoad(args)
},
Example: loadExample(),
}
l.addFlags()
}
// addFlags adds flags for specific command.
func (l *LoadCommand) addFlags() {
flagSet := l.cmd.Flags()
flagSet.StringVarP(&l.input, "input", "i", "", "Read from tar archive file, instead of STDIN")
}
// runLoad is the entry of load command.
func (l *LoadCommand) runLoad(args []string) error {
ctx := context.Background()
apiClient := l.cli.Client()
var (
in io.Reader = os.Stdin
imageName = ""
)
if l.input != "" {
file, err := os.Open(l.input)
if err != nil {
return err
}
defer file.Close()
in = file
}
if len(args) > 0 {
imageName = args[0]
}
return apiClient.ImageLoad(ctx, imageName, in)
}
// loadExample shows examples in load command, and is used in auto-generated cli docs.
func loadExample() string {
return `$ pouch load -i busybox.tar busybox`
}