shad-go/tools/testtool/commands/deadlines.go

100 lines
1.6 KiB
Go
Raw Normal View History

2020-02-12 22:54:25 +00:00
package commands
import (
"fmt"
"os"
2020-02-12 22:54:25 +00:00
"path/filepath"
"sort"
2020-02-12 23:28:27 +00:00
"strings"
2020-02-12 22:54:25 +00:00
"gopkg.in/yaml.v2"
)
type (
Task struct {
Name string `yaml:"task"`
Watch []string `yaml:"watch"`
2020-02-12 22:54:25 +00:00
}
Group struct {
Name string `yaml:"group"`
Tasks []Task `yaml:"tasks"`
2020-02-12 22:54:25 +00:00
}
Deadlines []Group
)
func (d Deadlines) Tasks() []*Task {
var tasks []*Task
for _, g := range d {
for i := range g.Tasks {
tasks = append(tasks, &g.Tasks[i])
}
}
return tasks
}
2020-02-12 22:54:25 +00:00
func (d Deadlines) FindTask(name string) (*Group, *Task) {
for _, g := range d {
for _, t := range g.Tasks {
if t.Name == name {
return &g, &t
}
}
}
return nil, nil
}
func loadDeadlines(filename string) (Deadlines, error) {
b, err := os.ReadFile(filename)
2020-02-12 22:54:25 +00:00
if err != nil {
return nil, err
}
var m struct {
Deadlines struct {
Schedule Deadlines `yaml:"schedule"`
} `yaml:"deadlines"`
2020-02-12 22:54:25 +00:00
}
if err := yaml.Unmarshal(b, &m); err != nil {
return nil, fmt.Errorf("error reading deadlines: %w", err)
2020-02-12 22:54:25 +00:00
}
d := m.Deadlines.Schedule
2020-02-12 22:54:25 +00:00
return d, nil
}
func findChangedTasks(d Deadlines, files []string) []string {
tasks := map[string]struct{}{}
for _, f := range files {
2020-02-12 23:28:27 +00:00
components := strings.Split(f, string(filepath.Separator))
if len(components) == 0 {
2020-02-12 22:54:25 +00:00
continue
}
2020-02-12 23:28:27 +00:00
_, task := d.FindTask(components[0])
if task != nil {
tasks[task.Name] = struct{}{}
2020-02-12 22:54:25 +00:00
}
for _, task := range d.Tasks() {
for _, path := range task.Watch {
2022-04-15 14:58:09 +00:00
if strings.HasPrefix(f, path) {
tasks[task.Name] = struct{}{}
}
}
}
2020-02-12 22:54:25 +00:00
}
var l []string
for t := range tasks {
l = append(l, t)
}
sort.Strings(l)
return l
}