This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (150 loc) · 3.58 KB
/
main.go
File metadata and controls
176 lines (150 loc) · 3.58 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/docker/docker/pkg/term"
"github.com/erikh/box/builder"
"github.com/erikh/box/log"
"github.com/erikh/box/repl"
"github.com/fatih/color"
"github.com/urfave/cli"
)
var (
// Version is the version of the application
Version = "0.3.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 = "0.3.1"
app.Usage = Usage
app.Author = Author
app.Copyright = Copyright
app.UsageText = UsageText
app.HideHelp = true
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "no-cache, n",
Usage: "Disable the build cache",
},
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.",
},
}
app.Commands = []cli.Command{
{
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) {
if ctx.Bool("help") {
cli.ShowAppHelp(ctx)
os.Exit(0)
}
args := ctx.Args()
tty := !ctx.Bool("no-tty")
if !term.IsTerminal(0) {
tty = ctx.Bool("force-tty")
}
b, err := builder.NewBuilder(tty, ctx.StringSlice("omit"))
if err != nil {
panic(err)
}
defer b.Close()
var content []byte
if len(args) == 1 {
content, err = ioutil.ReadFile(args[0])
} else {
cli.ShowAppHelp(ctx)
color.Red("!!! Please provide a filename to process!\n\n")
os.Exit(1)
}
if err != nil {
fmt.Printf("\n\n!!! Error: %v\n", err.Error())
os.Exit(2)
}
if ctx.Bool("no-cache") {
b.SetCache(false)
}
response, err := b.Run(string(content))
if err != nil {
fmt.Printf("\n\n!!! Error: %v\n", err.Error())
os.Exit(1)
}
if response.String() != "" {
log.EvalResponse(response.String())
}
tag := ctx.String("tag")
if tag != "" {
if err := b.Tag(tag); err != nil {
fmt.Printf("!!! Can't tag with tag %q: %v\n", tag, err)
os.Exit(1)
}
log.Tag(tag)
}
id := b.ImageID()
if strings.Contains(id, ":") {
id = strings.SplitN(id, ":", 2)[1]
}
log.Finish(id)
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "\n\n!!! Error: %v\n", err)
os.Exit(1)
}
}
func runRepl(ctx *cli.Context) {
r, err := repl.NewRepl()
if err != nil {
fmt.Fprintf(os.Stderr, "\n\n!!! Error bootstrapping repl: %v\n", err)
os.Exit(1)
}
if err := r.Loop(); err != nil {
fmt.Printf("\n\n!!! Error: %v\n", err)
os.Exit(1)
}
}