-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (58 loc) · 1.81 KB
/
main.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 (
"errors"
"flag"
"fmt"
"io"
"os"
"code.cloudfoundry.org/diff-exporter/layer"
)
type Exporter interface {
Export() (io.ReadCloser, error)
}
func main() {
outputFile, containerId, bundlePath, err := parseFlags()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing flags: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "USAGE: diff-exporter.exe <-outputFile outputFile> <-containerId containerId> <-bundlePath bundlePath>\n")
os.Exit(1)
}
exporter := layer.New(containerId, bundlePath)
if err := writeTgzFile(exporter, outputFile); err != nil {
fmt.Fprintf(os.Stderr, "Error writing tar.gz file: %s", err.Error())
os.Exit(1)
}
}
func writeTgzFile(exporter Exporter, outputFile string) error {
tgzStream, err := exporter.Export()
if err != nil {
return fmt.Errorf("Error exporting layer: %s", err.Error())
}
defer tgzStream.Close()
outFd, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("Error creating output file: %s", err.Error())
}
defer outFd.Close()
_, err = io.Copy(outFd, tgzStream)
if err != nil {
return fmt.Errorf("Error copying tar stream: %s", err.Error())
}
return nil
}
func parseFlags() (string, string, string, error) {
outputFile := flag.String("outputFile", "", "File to save exported layer")
containerId := flag.String("containerId", "", "Container ID to use")
bundlePath := flag.String("bundlePath", "", "Path to the root of the bundle directory to use")
flag.Parse()
if *outputFile == "" {
return "", "", "", errors.New("must provide output file to save exported layer")
}
if *containerId == "" {
return "", "", "", errors.New("must provide container id to export layer from")
}
if *bundlePath == "" {
return "", "", "", errors.New("must provide bundle path for container")
}
return *outputFile, *containerId, *bundlePath, nil
}