forked from cfregly/Nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
61 lines (51 loc) · 1.38 KB
/
archive.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
package commands
import (
"errors"
"flag"
"fmt"
"github.com/PagerDuty/nut/container"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
"strings"
)
type ArchiveCommand struct{}
func Archive() (cli.Command, error) {
command := &ArchiveCommand{}
return command, nil
}
func (command *ArchiveCommand) Help() string {
helpText := `
Usage: nut archive [options] <container> <image>
nut archive is used to build tarball image from an existing
container.
-sudo Use sudo while invoking tar
`
return strings.TrimSpace(helpText)
}
func (command *ArchiveCommand) Synopsis() string {
return "Create tarball images of existing container"
}
func (command *ArchiveCommand) Run(args []string) int {
flagSet := flag.NewFlagSet("archive", flag.ExitOnError)
flagSet.Usage = func() { fmt.Println(command.Help()) }
sudo := flagSet.Bool("sudo", false, "Use sudo while invoking tar")
if err := flagSet.Parse(args); err != nil {
log.Errorln(err)
return -1
}
args = flagSet.Args()
if len(args) != 2 {
log.Errorln(errors.New("Insufficient argument. Please pass container name and image file name"))
return -1
}
image, err := container.NewImage(args[0], args[1])
if err != nil {
log.Errorf("Failed to initialize container. Error: %s\n", err)
return -1
}
if err := image.Create(*sudo); err != nil {
log.Errorf("Failed to create image. Error: %s\n", err)
return -1
}
return 0
}