forked from rancher/dapper
/
main.go
118 lines (101 loc) · 2.45 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/rancher/dapper/file"
)
var (
VERSION = "0.0.0"
)
func main() {
exit := func(err error) {
if err == file.ErrSkipBuild {
logrus.Infof("Build not supported on this architecture")
os.Exit(42)
}
if err != nil {
logrus.Fatal(err)
}
}
app := cli.NewApp()
app.Author = "Rancher Labs"
app.EnableBashCompletion = true
app.Version = VERSION
app.Usage = `Docker build wrapper
Dockerfile variables
DAPPER_SOURCE The destination directory in the container to bind/copy the source
DAPPER_CP The location in the host to find the source
DAPPER_OUTPUT The files you want copied to the host in CP mode
DAPPER_DOCKER_SOCKET Whether the Docker socket should be bound in
DAPPER_RUN_ARGS Args to add to the docker run command when building
DAPPER_ENV Env vars that should be copied into the build`
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "Dockerfile.dapper",
Usage: "Dockerfile to build from",
},
cli.BoolFlag{
Name: "socket, k",
Usage: "Bind in the Docker socket",
},
cli.StringFlag{
Name: "mode, m",
Value: "auto",
Usage: "Execution mode for Dapper bind/cp/auto",
EnvVar: "DAPPER_MODE",
},
cli.BoolFlag{
Name: "no-out, O",
Usage: "Do not copy the output back (in --mode cp)",
},
cli.BoolFlag{
Name: "build",
Usage: "Perform Dapperfile build",
},
cli.StringFlag{
Name: "directory, C",
Value: ".",
Usage: "The directory in which to run, --file is relative to this",
},
cli.BoolFlag{
Name: "shell, s",
Usage: "Launch a shell",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Print debugging",
},
}
app.Action = func(c *cli.Context) {
exit(run(c))
}
exit(app.Run(os.Args))
}
func run(c *cli.Context) error {
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
dir := c.String("directory")
shell := c.Bool("shell")
build := c.Bool("build")
if err := os.Chdir(dir); err != nil {
return fmt.Errorf("Failed to change to directory %s: %v", dir, err)
}
dapperFile, err := file.Lookup(c.String("file"))
if err != nil {
return err
}
dapperFile.Mode = c.String("mode")
dapperFile.Socket = c.Bool("socket")
dapperFile.NoOut = c.Bool("no-out")
if shell {
return dapperFile.Shell(c.Args())
}
if build {
return dapperFile.Build(c.Args())
}
return dapperFile.Run(c.Args())
}