/
cmd.go
296 lines (253 loc) · 7.54 KB
/
cmd.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
package crane
import (
"fmt"
"github.com/alecthomas/kingpin"
"os"
"strings"
)
var cfg Config
var dependencyGraph DependencyGraph
var excluded []string
var (
app = kingpin.New("crane", "Lift containers with ease")
interspersed = app.Interspersed(false)
verboseFlag = app.Flag("verbose", "Enable verbose output.").Short('v').Bool()
configFlag = app.Flag(
"config",
"Location of config file.",
).Short('c').PlaceHolder("~/crane.yaml").String()
liftCommand = app.Command(
"lift",
"Build or pull images if they don't exist, then run or start the containers.",
)
liftNoCacheFlag = liftCommand.Flag(
"no-cache",
"Build the image without any cache.",
).Short('n').Bool()
liftExcludeFlag = liftCommand.Flag(
"exclude",
"Exclude group or container",
).Short('e').String()
liftTargetArg = liftCommand.Arg("target", "Target of command").String()
liftCmdArg = liftCommand.Arg("cmd", "Command for container").Strings()
versionCommand = app.Command(
"version",
"Displays the version of Crane.",
)
graphCommand = app.Command(
"graph",
"Dumps the dependency graph as a DOT file.",
)
graphTargetArg = graphCommand.Arg("target", "Target of command").String()
statsCommand = app.Command(
"stats",
"Displays statistics about containers.",
)
statsTargetArg = statsCommand.Arg("target", "Target of command").String()
statusCommand = app.Command(
"status",
"Displays status of containers.",
)
noTruncFlag = liftCommand.Flag(
"no-trunc",
"Don't truncate output.",
).Bool()
statusTargetArg = statusCommand.Arg("target", "Target of command").String()
pushCommand = app.Command(
"push",
"Push the containers to the registry.",
)
pushTargetArg = pushCommand.Arg("target", "Target of command").String()
pauseCommand = app.Command(
"pause",
"Pause the containers.",
)
pauseTargetArg = pauseCommand.Arg("target", "Target of command").String()
unpauseCommand = app.Command(
"unpause",
"Unpause the containers.",
)
unpauseTargetArg = unpauseCommand.Arg("target", "Target of command").String()
startCommand = app.Command(
"start",
"Start the containers.",
)
startTargetArg = startCommand.Arg("target", "Target of command").String()
stopCommand = app.Command(
"stop",
"Stop the containers.",
)
stopTargetArg = stopCommand.Arg("target", "Target of command").String()
killCommand = app.Command(
"kill",
"Kill the containers.",
)
killTargetArg = killCommand.Arg("target", "Target of command").String()
rmCommand = app.Command(
"rm",
"Remove the containers.",
)
forceRmFlag = rmCommand.Flag(
"force",
"Kill containers if they are running first.",
).Short('f').Bool()
rmTargetArg = rmCommand.Arg("target", "Target of command").String()
runCommand = app.Command(
"run",
"Run the containers.",
)
runExcludeFlag = runCommand.Flag(
"exclude",
"Exclude group or container",
).Short('e').String()
runTargetArg = runCommand.Arg("target", "Target of command").String()
runCmdArg = runCommand.Arg("cmd", "Command for container").Strings()
createCommand = app.Command(
"create",
"Create the containers.",
)
createExcludeFlag = createCommand.Flag(
"exclude",
"Exclude group or container",
).Short('e').String()
createTargetArg = createCommand.Arg("target", "Target of command").String()
createCmdArg = createCommand.Arg("cmd", "Command for container").Strings()
provisionCommand = app.Command(
"provision",
"Build or pull images.",
)
provisionNoCacheFlag = provisionCommand.Flag(
"no-cache",
"Build the image without any cache.",
).Short('n').Bool()
provisionTargetArg = provisionCommand.Arg("target", "Target of command").String()
pullCommand = app.Command(
"pull",
"Pull images.",
)
pullTargetArg = pullCommand.Arg("target", "Target of command").String()
logsCommand = app.Command(
"logs",
"Display container logs.",
)
followFlag = logsCommand.Flag(
"follow",
"Follow log output.",
).Short('f').Bool()
tailFlag = logsCommand.Flag(
"tail",
"Output the specified number of lines at the end of logs.",
).String()
timestampsFlag = logsCommand.Flag(
"timestamps",
"Show timestamps.",
).Short('t').Bool()
colorizeFlag = logsCommand.Flag(
"colorize",
"Output the lines with one color per container.",
).Short('z').Bool()
sinceFlag = logsCommand.Flag(
"since",
"Show logs since timestamp (Docker >= 1.7).",
).String()
logsTargetArg = logsCommand.Arg("target", "Target of command").String()
)
func isVerbose() bool {
return *verboseFlag
}
func commandAction(targetFlag string, wrapped func(unitOfWork *UnitOfWork), mightStartRelated bool) {
cfg = NewConfig(*configFlag)
excluded = excludedContainers([]string{*liftExcludeFlag, *createExcludeFlag, *runExcludeFlag})
dependencyGraph = cfg.DependencyGraph(excluded)
target := NewTarget(dependencyGraph, targetFlag)
unitOfWork, err := NewUnitOfWork(dependencyGraph, target.all())
if err != nil {
panic(StatusError{err, 78})
}
if isVerbose() {
printInfof("Command will be applied to: %s", strings.Join(unitOfWork.targeted, ", "))
if mightStartRelated && len(unitOfWork.Affected()) > 0 {
printInfof("\nIf needed, also starts: %s", strings.Join(unitOfWork.Affected(), ", "))
}
fmt.Println("\n")
}
wrapped(unitOfWork)
}
func excludedContainers(flags []string) []string {
for _, flag := range flags {
if len(flag) > 0 {
return cfg.ContainersForReference(flag)
}
}
return []string{}
}
func handleCmd() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case liftCommand.FullCommand():
commandAction(*liftTargetArg, func(uow *UnitOfWork) {
uow.Lift(*liftCmdArg, excluded, *liftNoCacheFlag)
}, true)
case versionCommand.FullCommand():
fmt.Println("v2.0.0-rc1")
case graphCommand.FullCommand():
commandAction(*graphTargetArg, func(uow *UnitOfWork) {
dependencyGraph.DOT(os.Stdout, uow.Targeted())
}, false)
case statsCommand.FullCommand():
commandAction(*statsTargetArg, func(uow *UnitOfWork) {
uow.Stats()
}, false)
case statusCommand.FullCommand():
commandAction(*statusTargetArg, func(uow *UnitOfWork) {
uow.Status(*noTruncFlag)
}, false)
case pushCommand.FullCommand():
commandAction(*pushTargetArg, func(uow *UnitOfWork) {
uow.Push()
}, false)
case unpauseCommand.FullCommand():
commandAction(*unpauseTargetArg, func(uow *UnitOfWork) {
uow.Unpause()
}, false)
case pauseCommand.FullCommand():
commandAction(*pauseTargetArg, func(uow *UnitOfWork) {
uow.Pause()
}, false)
case startCommand.FullCommand():
commandAction(*startTargetArg, func(uow *UnitOfWork) {
uow.Start()
}, true)
case stopCommand.FullCommand():
commandAction(*stopTargetArg, func(uow *UnitOfWork) {
uow.Stop()
}, false)
case killCommand.FullCommand():
commandAction(*killTargetArg, func(uow *UnitOfWork) {
uow.Kill()
}, false)
case rmCommand.FullCommand():
commandAction(*rmTargetArg, func(uow *UnitOfWork) {
uow.Rm(*forceRmFlag)
}, false)
case runCommand.FullCommand():
commandAction(*runTargetArg, func(uow *UnitOfWork) {
uow.Run(*runCmdArg, excluded)
}, true)
case createCommand.FullCommand():
commandAction(*createTargetArg, func(uow *UnitOfWork) {
uow.Create(*createCmdArg, excluded)
}, true)
case provisionCommand.FullCommand():
commandAction(*provisionTargetArg, func(uow *UnitOfWork) {
uow.Provision(*provisionNoCacheFlag)
}, false)
case pullCommand.FullCommand():
commandAction(*pullTargetArg, func(uow *UnitOfWork) {
uow.PullImage()
}, false)
case logsCommand.FullCommand():
commandAction(*logsTargetArg, func(uow *UnitOfWork) {
uow.Logs(*followFlag, *timestampsFlag, *tailFlag, *colorizeFlag, *sinceFlag)
}, false)
}
}