-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
308 lines (286 loc) · 6.66 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
package main
import (
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"text/tabwriter"
"github.com/fatih/color"
"github.com/fsouza/go-dockerclient"
"github.com/peterh/liner"
)
const (
defaultPrompt = "cyclops"
defaultImage = "ubuntu:trusty"
)
var (
ErrInvalidCommand = errors.New("Invalid command")
ErrMissingRequiredArg = errors.New("Missing required argument for command")
)
func help() {
usage := `cyclops - help
:h, :help show help
:f, :from [image] set base image
:e, :eval [command ...] execute shell command (ephemeral)
:r, :run [command ...] execute shell command (auto commits image)
:c, :commit commit changes from last command
:b, :back [num] go back in the history (default: 1)
:hs, :history show the current history
:w, :write [path/to/file] write state to file
:q, :quit quit cyclops - <ctrl-d>
`
fmt.Println(usage)
}
func printResults(res EvalResult) {
fmt.Println()
fmt.Println("Exit:", res.Code)
fmt.Println("Took:", res.Duration)
fmt.Println("From:", res.Image)
if res.NewImage != "" {
fmt.Println("Committed:", res.NewImage[:12])
}
printChanges(res.Changes)
}
func printChanges(changes []docker.Change) {
fmt.Println("Changes:")
if len(changes) == 0 {
fmt.Println("<none>")
}
prunedChanges := pruneChanges(changes)
for _, change := range prunedChanges {
if change.Path == "/work" {
continue
}
switch change.Kind {
case 0:
color.Yellow("~ %s", change.Path)
case 1:
color.Green("+ %s", change.Path)
case 2:
color.Red("- %s", change.Path)
}
}
}
func printHistory(history []EvalResult, currentImage string) {
n := 1
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
// print header
fmt.Fprintln(w, "Item\tCommand\tExit\tCreated Image")
for _, entry := range history {
var row string
if entry.Deleted {
row = "\t"
} else {
if currentImage == entry.NewImage {
row += fmt.Sprintf(">%d\t", n)
} else {
row += fmt.Sprintf("%2d\t", n)
}
n += 1
}
row += fmt.Sprintf("%s\t", entry.Command)
row += fmt.Sprintf("%d\t", entry.Code)
if len(entry.NewImage) == 64 {
row += fmt.Sprintf("%s\t", entry.NewImage[:12])
} else {
row += fmt.Sprintf("%s\t", entry.NewImage)
}
fmt.Fprintln(w, row)
}
w.Flush()
}
func pruneChanges(changes []docker.Change) []docker.Change {
var p string
c := []docker.Change{}
for i := len(changes) - 1; i > 0; i -= 1 {
if changes[i].Kind == 0 {
if !strings.Contains(p, changes[i].Path) {
c = append(c, changes[i])
}
} else {
c = append(c, changes[i])
}
p = changes[i].Path
}
// reverse the slice results
for i := len(c)/2 - 1; i >= 0; i-- {
opp := len(c) - 1 - i
c[i], c[opp] = c[opp], c[i]
}
return c
}
func parseCommand(input string) (string, string, error) {
if input == "" {
return "", "", nil
}
if string(input[0]) != ":" {
return "eval", input, nil
}
parts := strings.SplitN(input, " ", 2)
switch parts[0] {
case ":commit", ":c":
return "commit", "", nil
case ":help", ":h":
return "help", "", nil
case ":print", ":p":
return "print", "", nil
case ":history", ":hs":
return "history", "", nil
case ":quit", ":q":
return "quit", "", nil
case ":eval", ":e":
if len(parts) < 2 {
return "eval", "", ErrMissingRequiredArg
}
return "eval", parts[1], nil
case ":from", ":f":
if len(parts) < 2 {
return "from", "", ErrMissingRequiredArg
}
return "from", parts[1], nil
case ":run", ":r":
if len(parts) < 2 {
return "run", "", ErrMissingRequiredArg
}
return "run", parts[1], nil
case ":write", ":w":
if len(parts) < 2 {
return "write", "", ErrMissingRequiredArg
}
return "write", parts[1], nil
case ":back", ":b":
if len(parts) < 2 {
return "back", "1", nil
}
return "back", parts[1], nil
default:
return parts[0], "", ErrInvalidCommand
}
}
func preExit(ws *Workspace) {
fmt.Println("Cleaning up...")
lines := ws.Reset()
for _, line := range lines {
if line.Err != nil {
fmt.Println(line.Err)
} else {
fmt.Printf("Deleted: %s\n", line.Id)
}
}
fmt.Println("Done")
}
func main() {
dc, err := NewDockerClient(os.Getenv("DOCKER_HOST"), os.Getenv("DOCKER_TLS_VERIFY"), os.Getenv("DOCKER_CERT_PATH"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := dc.Ping(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Connected to docker daemon...")
ws := NewWorkspace(dc, "bash", defaultImage)
line := liner.NewLiner()
defer line.Close()
prompt := defaultPrompt
if f, err := os.Open("/tmp/.cyclops_history"); err == nil {
line.ReadHistory(f)
f.Close()
}
defer preExit(ws)
mainloop:
for {
input, err := line.Prompt(prompt + "> ")
if err == io.EOF {
fmt.Println() //Returns user to prompt on a new line
break mainloop
}
command, args, err := parseCommand(input)
if err != nil {
fmt.Println(err, command)
continue
}
switch command {
case "help":
help()
continue
case "quit":
break mainloop
case "commit":
if id, err := ws.CommitLast(); err != nil {
fmt.Println(err)
} else {
fmt.Println("Committed:", id)
}
case "eval":
if res, err := ws.Eval(args); err != nil {
fmt.Println(err)
} else {
printResults(res)
}
case "from":
if ws.CurrentImage != ws.Image {
confirm, err := line.Prompt("Changes will be lost. Continue? <y>: ")
if err != nil || confirm != "y" {
fmt.Println("Aborted")
continue
}
fmt.Println("Wiping history to set new base image")
}
ws.Reset()
if err := ws.SetImage(args); err != nil {
fmt.Println("error setting image:", err)
} else {
fmt.Println("Image: ", args)
}
case "back":
num, err := strconv.Atoi(args)
if err != nil {
fmt.Println("Error: invalid number specified")
break
}
if err := ws.back(num); err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Back %d to %s\n", num, ws.CurrentImage)
}
case "history":
printHistory(ws.history, ws.CurrentImage)
case "print":
if out, err := ws.Sprint(); err == nil {
for _, line := range out {
fmt.Println(line)
}
}
case "run":
if res, err := ws.Run(args); err != nil {
fmt.Println(err)
} else {
printResults(res)
}
case "write":
if args == "" {
fmt.Println("Missing file path: `:write [path/to/file]`")
break
}
if err := ws.Write(args); err != nil {
fmt.Println("Error writing file:", err)
} else {
fmt.Println("File written:", args)
}
default:
continue
}
line.AppendHistory(input)
if f, err := os.Create("/tmp/.cyclops_history"); err != nil {
fmt.Println("error writing history:", err)
} else {
line.WriteHistory(f)
f.Close()
}
}
}