-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
64 lines (56 loc) · 1.33 KB
/
app.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
package main
import (
"context"
"fmt"
"github.com/docker/docker/client"
cli "gopkg.in/urfave/cli.v1"
)
// App
type App struct {
ctx context.Context
client *client.Client
}
func NewApp(ctx context.Context) *App {
dockerCli, err := client.NewClientWithOpts(client.FromEnv)
dockerCli.NegotiateAPIVersion(context.Background())
if err != nil {
panic(err)
}
return &App{ctx: ctx,
client: dockerCli,
}
}
func (app *App) Run(args []string) {
var cliApp = cli.NewApp()
cliApp.Name = "Save docker image without layers in `exclude`"
cliApp.Usage = ""
cliApp.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "image, i",
Usage: "The image to save",
},
cli.StringSliceFlag{
Name: "exclude, e",
Usage: "The existing image that does not need to export",
},
cli.StringFlag{
Name: "output, o",
Value: "image.tgz",
Usage: "The output tar.gz file",
},
}
cliApp.Action = func(c *cli.Context) error {
if err := app.process(c); err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to process: %v", err), 1)
}
return nil
}
cliApp.Run(args)
}
// Process command
func (app *App) process(c *cli.Context) error {
images := ImageList(c.StringSlice("image"))
excludeIDs := ImageList(c.StringSlice("exclude"))
output := c.String("output")
return SaveImage(app.ctx, images, excludeIDs, output, app.client)
}