This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
441 lines (383 loc) · 11.2 KB
/
cli.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"text/template"
"time"
"github.com/fatih/color"
"github.com/pkg/errors"
pb "gopkg.in/cheggaaa/pb.v1"
)
var (
errNotProvided = fmt.Errorf("command not provided")
errNotFound = fmt.Errorf("command not found")
errExecution = fmt.Errorf("command execution error")
)
// Command defines behavior to interact with user input.
type Command interface {
// FlagSet should return configured FlagSet to handle command arguments.
FlagSet() *flag.FlagSet
// Name should return a command name.
Name() string
// Desc should return a command description.
Desc() string
// Do should exec a command.
Do() error
}
// Commands is a container provides the method to search an appropriate command.
type Commands []Command
// Parse parses the arguments and searches an appropriate command for them.
func (l Commands) Parse(args []string) (Command, error) {
if len(args) == 0 {
return nil, errNotProvided
}
cmdName := args[0]
if _, found := map[string]struct{}{"-h": {}, "-help": {}, "--help": {}}[cmdName]; found {
return nil, flag.ErrHelp
}
for _, cmd := range l {
if cmd.Name() == cmdName {
return cmd, errors.Wrapf(cmd.FlagSet().Parse(args[1:]), "invalid arguments for command %s", cmd.Name())
}
}
return nil, errNotFound
}
// BaseCommand contains general fields for other commands.
type BaseCommand struct {
Debug bool
BinName string
FileName string
Mode flag.ErrorHandling
}
// FlagSet creates and configures new general FlagSet.
func (c *BaseCommand) FlagSet(name string) *flag.FlagSet {
fs := flag.NewFlagSet(name, c.Mode)
fs.BoolVar(&c.Debug, "debug", false, "show error stack trace")
fs.StringVar(&c.FileName, "filename", filepath.Join(os.TempDir(), c.BinName+".json"),
"an absolute path to semaphore context")
return fs
}
// CreateCommand is a command to init a semaphore context.
type CreateCommand struct {
*BaseCommand
CmdName string
Capacity int
Flags *flag.FlagSet
}
// FlagSet returns a configured FlagSet to handle CreateCommand arguments.
func (c *CreateCommand) FlagSet() *flag.FlagSet {
if c.Flags == nil {
c.Flags = c.BaseCommand.FlagSet(c.CmdName)
}
return c.Flags
}
// Name returns a CreateCommand name.
func (c *CreateCommand) Name() string {
return c.CmdName
}
// Desc returns a CreateCommand description.
func (c *CreateCommand) Desc() string {
return "is a command to init a semaphore context"
}
// Do creates a file to store a semaphore context.
func (c *CreateCommand) Do() error {
var err error
args := c.FlagSet().Args()
capacity := c.Capacity
if len(args) > 0 {
if capacity, err = strconv.Atoi(args[0]); err != nil || capacity < 1 {
return errors.Wrapf(err, "invalid capacity: capacity must be a valid integer greater than zero")
}
}
file, err := os.Create(c.FileName)
if err != nil {
return errors.Wrapf(err, "could not create a file %s", c.FileName)
}
task := Task{Capacity: capacity}
if err := json.NewEncoder(file).Encode(task); err != nil {
return errors.Wrapf(err, "could not store a context %+v into a file %s", task, c.FileName)
}
return nil
}
// AddCommand is a command to add a job into a semaphore context.
type AddCommand struct {
*BaseCommand
CmdName string
Edit bool
Command []string
Flags *flag.FlagSet
}
// FlagSet returns configured FlagSet to handle AddCommand arguments.
func (c *AddCommand) FlagSet() *flag.FlagSet {
if c.Flags == nil {
c.Flags = c.BaseCommand.FlagSet(c.CmdName)
c.Flags.BoolVar(&c.Edit, "edit", false, "switch to edit mode to read arguments from input (not implemented yet)")
}
return c.Flags
}
// Name returns an AddCommand name.
func (c *AddCommand) Name() string {
return c.CmdName
}
// Desc returns an AddCommand description.
func (c *AddCommand) Desc() string {
return "is a command to add a job into a semaphore context"
}
// Do adds a job into a semaphore context and stores it.
func (c *AddCommand) Do() error {
if c.Edit {
// TODO each new line from os.Stdin should be converted to Task
_, _ = color.New(color.FgYellow).Fprintln(os.Stderr, "edit component is not ready yet")
}
args := c.FlagSet().Args()
if len(args) == 0 {
return fmt.Errorf("the add command requires arguments to create a job based on them")
}
file, err := os.OpenFile(c.FileName, os.O_RDWR, 0644)
if err != nil {
return errors.Wrapf(err, "could not open a file %s. did you create it before?", c.FileName)
}
var task Task
if err := json.NewDecoder(file).Decode(&task); err != nil {
return errors.Wrapf(err, "could not restore a context from a file %s. is it a valid JSON?", c.FileName)
}
task.AddJob(Job{Name: args[0], Args: args[1:]})
data, err := json.Marshal(task)
if err != nil {
return errors.Wrapf(err, "could not encode a context %+v into a JSON", task)
}
if _, err := file.WriteAt(data, 0); err != nil {
return errors.Wrapf(err, "could not store a context %+v into a file %s", task, c.FileName)
}
return nil
}
// DefaultReport is a default template for report.
var DefaultReport = `
command: {{ .Name }} {{ range .Args }}{{ . }} {{ end }}
error: {{ .Error }}
details: started at {{ .Start }}, finished at {{ .End }}, elapsed {{ .Elapsed }}
stdout:
{{ .Stdout }}
stderr:
{{ .Stderr }}
---
`
// ColoredOutput wraps another output and colorizes input data before to pass it.
type ColoredOutput struct {
clr *color.Color
dst io.Writer
}
// Write implements `io.Writer` interface.
func (o *ColoredOutput) Write(p []byte) (int, error) {
return o.clr.Fprint(o.dst, string(p))
}
// LimitedOutput wraps another output and writes to it with specified velocity.
type LimitedOutput struct {
dst io.Writer
speed int
}
// For sets limiter for passed io.Writer.
func (o *LimitedOutput) For(dst io.Writer) *LimitedOutput {
o.dst = dst
return o
}
// Write implements `io.Writer` interface.
func (o *LimitedOutput) Write(p []byte) (int, error) {
if o.speed != 0 {
pause := time.Second / time.Duration(o.speed)
var (
total, n int
err error
)
for _, r := range string(p) {
if r == 0 {
continue
}
n, err = o.dst.Write([]byte(string(r)))
total += n
if err != nil {
break
}
time.Sleep(pause)
}
return total, err
}
return o.dst.Write(p)
}
// WaitCommand is a command to execute a semaphore task.
type WaitCommand struct {
*BaseCommand
CmdName string
Notify bool
Output io.Writer
Speed int
Template *template.Template
Timeout time.Duration
Flags *flag.FlagSet
}
// FlagSet returns a configured FlagSet to handle WaitCommand arguments.
func (c *WaitCommand) FlagSet() *flag.FlagSet {
if c.Flags == nil {
c.Flags = c.BaseCommand.FlagSet(c.CmdName)
c.Flags.BoolVar(&c.Notify, "notify", false, "show notification at the end (not implemented yet)")
c.Flags.DurationVar(&c.Timeout, "timeout", time.Minute, "timeout for task execution")
c.Flags.IntVar(&c.Speed, "speed", 0, "a velocity of report output (characters per second)")
}
return c.Flags
}
// Name returns a WaitCommand name.
func (c *WaitCommand) Name() string {
return c.CmdName
}
// Desc returns a WaitCommand description.
func (c *WaitCommand) Desc() string {
return "is a command to execute a semaphore task"
}
// Do executes a semaphore task.
func (c *WaitCommand) Do() error {
file, err := os.OpenFile(c.FileName, os.O_RDWR, 0644)
if err != nil {
return errors.Wrapf(err, "could not open a file %s. did you create it before?", c.FileName)
}
var task Task
if err := json.NewDecoder(file).Decode(&task); err != nil {
return errors.Wrapf(err, "could not restore a context from a file %s. is it a valid JSON?", c.FileName)
}
if c.Timeout > 0 {
task.Timeout = c.Timeout
}
var (
bar = pb.New(len(task.Jobs))
results = &Results{}
red = &ColoredOutput{clr: color.New(color.FgHiRed), dst: c.Output}
limiter = &LimitedOutput{speed: c.Speed}
success, failure = 0, 0
output io.Writer
start, end time.Time
)
bar.Output = c.Output
bar.ShowTimeLeft = false
bar.Start()
start = time.Now()
for result := range task.Run() {
success++
if result.Error != nil {
bar.Output = red
failure++
success--
}
bar.Increment()
results.Append(result)
}
end = time.Now()
bar.Finish()
for _, result := range results.Sort() {
output = c.Output
if result.Error != nil {
output = red
}
stdout, _ := ioutil.ReadAll(result.Stdout)
stderr, _ := ioutil.ReadAll(result.Stderr)
err = errors.Wrap(c.Template.Execute(limiter.For(output), struct {
Name string
Args []string
Error error
Start, End string
Elapsed time.Duration
Stdout string
Stderr string
}{
Name: result.Job.Name,
Args: result.Job.Args,
Error: result.Error,
Start: result.Start.Format("2006-01-02 15:04:05.99"),
End: result.End.Format("2006-01-02 15:04:05.99"),
Elapsed: result.End.Sub(result.Start),
Stdout: string(stdout),
Stderr: string(stderr),
}), "template execution")
}
output = c.Output
if failure > 0 {
output = red
}
_, _ = fmt.Fprintf(output, "total: %d; successful: %d; failed: %d; elapsed: %s \n",
results.Len(), success, failure, end.Sub(start))
if c.Notify {
// TODO try to find or implement by myself
// - https://github.com/variadico/noti
// - https://github.com/jolicode/JoliNotif
_, _ = color.New(color.FgYellow).Fprintln(os.Stderr, "notify component is not ready yet")
}
if failure > 0 {
return errExecution
}
return err
}
// HelpCommand is command to show help message.
type HelpCommand struct {
*BaseCommand
CmdName string
Commit, BuildDate, Version string
Compiler, Platform, GoVersion string
Commands Commands
Error error
Output io.Writer
Flags *flag.FlagSet
}
// FlagSet returns a configured FlagSet to handle HelpCommand arguments.
func (c *HelpCommand) FlagSet() *flag.FlagSet {
if c.Flags == nil {
c.Flags = c.BaseCommand.FlagSet(c.CmdName)
}
return c.Flags
}
// Name returns a HelpCommand name.
func (c *HelpCommand) Name() string {
return c.CmdName
}
// Desc returns a HelpCommand description.
func (c *HelpCommand) Desc() string {
return "is command to show help message"
}
// Do handles inner error and shows a specific message.
func (c *HelpCommand) Do() error {
switch c.Error {
case nil, errNotProvided, flag.ErrHelp:
c.Usage()
return nil
case errNotFound:
c.Usage()
fallthrough
case errExecution:
return c.Error
default:
_, _ = color.New(color.FgRed).Fprintf(c.Output, "an error occurred: %v\n", c.Error)
return c.Error
}
}
// Usage shows help message.
func (c *HelpCommand) Usage() {
_, _ = fmt.Fprintf(c.Output, `
Usage: %s COMMAND
Semaphore provides functionality to execute terminal commands in parallel.
`, c.BinName)
if len(c.Commands) > 0 {
_, _ = fmt.Fprintln(c.Output, "Commands:")
for _, cmd := range c.Commands {
_, _ = fmt.Fprintf(c.Output, "\n%s\t%s\n", cmd.Name(), cmd.Desc())
fs := cmd.FlagSet()
fs.SetOutput(c.Output)
fs.PrintDefaults()
_, _ = fmt.Fprintln(c.Output)
}
}
_, _ = fmt.Fprintf(c.Output, "Version %s (commit: %s, build date: %s, go version: %s, compiler: %s, platform: %s)\n",
c.Version, c.Commit, c.BuildDate, c.GoVersion, c.Compiler, c.Platform)
}