-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
77 lines (68 loc) · 1.34 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package config
import (
"errors"
"github.com/goccy/go-yaml"
)
type Hooks struct {
Pre string `yaml:"pre"`
Post string `yaml:"post"`
}
type Cmds []string
type Tasks map[string]*Task
type Task struct {
IsDefault bool
Name string
fullName string
Shell string `yaml:"shell"`
Cmds Cmds `yaml:"run"`
Config *Config
Parent *Task
Description string `yaml:"desc"`
Hooks Hooks `yaml:"hooks"`
Tasks Tasks `yaml:"tasks"`
}
func (t *Task) setup(c *Config, parent *Task, name string) {
t.Name = name
t.Config = c
t.Parent = parent
if parent != nil {
t.fullName = parent.fullName + "." + t.Name
} else {
t.fullName = t.Name
}
if t.fullName == c.Default {
t.IsDefault = true
}
for name, sub := range t.Tasks {
sub.setup(c, t, name)
}
}
func (cmds *Cmds) UnmarshalYAML(data []byte) error {
var str string
if err := yaml.Unmarshal(data, &str); err == nil {
*cmds = []string{str}
return nil
}
ss := []string{}
err := yaml.Unmarshal(data, &ss)
*cmds = ss
return err
}
func (t *Task) Verify() error {
if len(t.Cmds) == 0 {
return errors.New("Nothing to run")
}
return nil
}
func (t *Task) GetShell() string {
if len(t.Shell) > 0 {
return t.Shell
}
if t.Parent == nil {
if len(t.Config.Shell) == 0 {
return "sh"
}
return t.Config.Shell
}
return t.Parent.GetShell()
}