This repository has been archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
310 lines (267 loc) · 6.61 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/box-builder/box/builder"
"github.com/box-builder/box/copy"
"github.com/box-builder/box/logger"
"github.com/box-builder/box/multi"
"github.com/box-builder/box/repl"
"github.com/box-builder/box/signal"
"github.com/box-builder/box/types"
"github.com/docker/docker/pkg/term"
"github.com/urfave/cli"
)
const defaultFile = "box.rb"
var (
// Version is the version of the application
Version = "0.4.2"
// Name is the name of the application
Name = "box"
// Email is my email
Email = "github@hollensbe.org"
// Usage is the title of the application
Usage = "Advanced mruby Container Image Builder"
// Author is me
Author = "Erik Hollensbe"
// Copyright is the copyright, generated automatically for each year.
Copyright = fmt.Sprintf("(C) %d %s - Licensed under MIT license", time.Now().Year(), Author)
// UsageText is the description of how to use the program.
UsageText = "box [options] [filename]"
)
func main() {
app := cli.NewApp()
app.Name = Name
app.Email = Email
app.Version = Version
cli.VersionFlag = cli.BoolFlag{
Name: "version",
Usage: "print the version",
}
app.Usage = Usage
app.Author = Author
app.Copyright = Copyright
app.UsageText = UsageText
app.HideHelp = true
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "var, v",
Usage: "Provide a variable to the build plan accepts `key=value` syntax.",
},
cli.BoolFlag{
Name: "no-cache, n",
Usage: "Disable the build cache",
},
cli.BoolFlag{
Name: "no-color",
Usage: "Disable colors this run",
},
cli.BoolFlag{
Name: "force-color",
Usage: "Force colors this run",
},
cli.BoolFlag{
Name: "no-tty",
Usage: "Disable TTY features this run",
},
cli.BoolFlag{
Name: "force-tty",
Usage: "Force TTY features this run",
},
cli.BoolFlag{
Name: "help, h",
Usage: "Show the help",
},
cli.StringFlag{
Name: "tag, t",
Usage: "Tag the last image with this name",
},
cli.StringSliceFlag{
Name: "omit, o",
Usage: "Omit functions/verbs. One per option, repeatable.",
},
cli.BoolFlag{
Name: "no-trim",
Usage: "Do not trim the output to terminal width.",
},
}
app.Commands = []cli.Command{
{
Name: "multi",
Action: runMulti,
Description: "Run the multi build functionality; supply multiple plans to build",
Usage: "Run the multi build functionality; supply multiple plans to build",
ArgsUsage: "[filename] [filename]",
},
{
Name: "repl",
Action: runRepl,
Description: "Run the read-eval-print loop to interactively work with box",
Usage: "Run the read-eval-print loop to interactively work with box",
ArgsUsage: " ",
},
{
Name: "shell",
Action: runRepl,
Description: "Run the read-eval-print loop to interactively work with box",
Usage: "Run the read-eval-print loop to interactively work with box",
ArgsUsage: " ",
},
}
app.Action = func(ctx *cli.Context) {
notrim := ctx.GlobalBool("no-trim")
log := logger.New("main", notrim)
if ctx.Bool("help") {
cli.ShowAppHelp(ctx)
os.Exit(0)
}
filename := detectFile(ctx)
tty := term.IsTerminal(1)
if ctx.Bool("no-tty") {
tty = false
}
if ctx.Bool("force-tty") {
tty = true
}
color := tty
if ctx.Bool("no-color") {
color = false
}
if ctx.Bool("force-color") {
color = true
}
cancelCtx, cancel := context.WithCancel(context.Background())
runChan := make(chan struct{})
buildConfig := builder.BuildConfig{
Globals: &types.Global{
ShowRun: true,
Color: color,
TTY: tty,
OmitFuncs: ctx.GlobalStringSlice("omit"),
Cache: getCache(ctx),
Logger: logger.New(filename, notrim),
Context: cancelCtx,
},
Runner: runChan,
FileName: filename,
Vars: parseVars(ctx),
}
b, err := mkBuilder(cancel, buildConfig)
if err != nil {
log.Error(err)
os.Exit(1)
}
defer b.Close()
result := b.Run()
if result.Err != nil {
log.Error(result.Err)
os.Exit(1)
}
if result.Value != "" {
log.EvalResponse(result.Value)
}
tag := ctx.String("tag")
if tag != "" {
if err := b.Tag(tag); err != nil {
log.Error(fmt.Sprintf("Can't tag with tag %q: %v", tag, err))
os.Exit(1)
}
log.Tag(tag)
}
id := result.Value
if strings.Contains(id, ":") {
id = strings.SplitN(id, ":", 2)[1]
}
log.Finish(id)
}
if err := app.Run(os.Args); err != nil {
logger.New("main", false).Error(err)
os.Exit(1)
}
}
func runMulti(ctx *cli.Context) {
copy.NoOut = true
notrim := ctx.GlobalBool("no-trim")
builders := []*builder.Builder{}
log := logger.New("main", notrim)
args := ctx.Args()
for _, filename := range args {
cancelCtx, cancel := context.WithCancel(context.Background())
runChan := make(chan struct{})
buildConfig := builder.BuildConfig{
Globals: &types.Global{
ShowRun: false,
Color: true,
TTY: true,
OmitFuncs: append(ctx.StringSlice("omit"), "debug"),
Cache: getCache(ctx),
Logger: logger.New(filename, notrim),
Context: cancelCtx,
},
Runner: runChan,
FileName: filename,
Vars: parseVars(ctx),
}
signal.Handler.AddFunc(cancel)
signal.Handler.AddRunner(runChan)
b, err := builder.NewBuilder(buildConfig)
if err != nil {
log.Error(err)
os.Exit(1)
}
builders = append(builders, b)
}
mb := multi.NewBuilder(builders)
mb.Build()
if err := mb.Wait(); err != nil {
log.Error(err)
os.Exit(2)
}
}
func getCache(ctx *cli.Context) bool {
cache := os.Getenv("NO_CACHE") == ""
if ctx.GlobalBool("no-cache") {
cache = false
}
return cache
}
func runRepl(ctx *cli.Context) {
log := logger.New("repl", ctx.GlobalBool("no-trim"))
r, err := repl.NewRepl(ctx.GlobalStringSlice("omit"), log, parseVars(ctx))
if err != nil {
log.Error(fmt.Sprintf("bootstrapping repl: %v\n", err))
os.Exit(1)
}
r.Loop() // the REPL manages its own exit states
}
func mkBuilder(cancel context.CancelFunc, buildConfig builder.BuildConfig) (*builder.Builder, error) {
b, err := builder.NewBuilder(buildConfig)
if err != nil {
return nil, err
}
signal.Handler.AddFunc(cancel)
signal.Handler.AddRunner(buildConfig.Runner)
return b, nil
}
func detectFile(ctx *cli.Context) string {
a := ctx.Args()
if len(a) < 1 {
if _, err := os.Stat("box.rb"); os.IsNotExist(err) {
cli.ShowAppHelp(ctx)
os.Exit(0)
}
return defaultFile
}
return a[0]
}
func parseVars(ctx *cli.Context) map[string]string {
vars := map[string]string{}
for _, v := range ctx.StringSlice("var") {
parts := strings.SplitN(v, "=", 2)
vars[parts[0]] = parts[1]
}
return vars
}