-
Notifications
You must be signed in to change notification settings - Fork 0
/
defined_task.go
60 lines (50 loc) · 1.16 KB
/
defined_task.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
package main
import (
"github.com/fatih/color"
"strings"
)
type DefinedTask interface {
Name() string
AddCommand(string)
Commands() []string
Run(bool, []string) error
}
type DefinedTaskImpl struct {
name string
commands []string
}
var NewDefinedTask = func(name string) DefinedTask {
Debug("Define Task: %s", name)
return &DefinedTaskImpl{
name: name,
}
}
func (d *DefinedTaskImpl) Name() string {
return d.name
}
func (d *DefinedTaskImpl) AddCommand(command string) {
Debug(" Add Command: %s", command)
d.commands = append(d.commands, strings.TrimSpace(command))
}
func (d *DefinedTaskImpl) Commands() []string {
return d.commands
}
func (d *DefinedTaskImpl) Run(dryRun bool, args []string) error {
Info(color.HiYellowString("Execute task: %s", d.name))
commands := d.Commands()
for _, command := range commands {
if err := d.runOnce(dryRun, command, args); err != nil {
return err
}
}
return nil
}
func (d *DefinedTaskImpl) runOnce(dryRun bool, command string, args []string) error {
executor := NewExecutor(dryRun, command, args)
if err := executor.Execute(); err != nil {
Error(err.Error())
return err
} else {
return nil
}
}