-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.go
144 lines (127 loc) · 3.16 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"os"
"github.com/rancher/dapper/file"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
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",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Make Docker build quieter",
},
cli.BoolFlag{
Name: "keep",
Usage: "Don't remove the container that was used to build",
},
cli.BoolFlag{
Name: "no-context, X",
Usage: "send Dockerfile via stdin to docker build command",
EnvVar: "DAPPER_NO_CONTEXT",
},
cli.StringFlag{
Name: "mount-suffix, S",
Usage: "Add a suffix to the source directory mount",
},
cli.StringFlag{
Name: "target",
Usage: "The multistage build target to use",
},
}
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")
dapperFile.Quiet = c.Bool("quiet")
dapperFile.Keep = c.Bool("keep")
dapperFile.NoContext = c.Bool("no-context")
dapperFile.MountSuffix = c.String("mount-suffix")
dapperFile.Target = c.String("target")
if shell {
return dapperFile.Shell(c.Args())
}
if build {
return dapperFile.Build(c.Args())
}
return dapperFile.Run(c.Args())
}