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

98 lines
1.7 KiB
Go
Raw Normal View History

2020-02-12 22:54:25 +00:00
package commands
import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
2020-02-12 23:28:27 +00:00
"strings"
2020-02-12 22:54:25 +00:00
"time"
"gopkg.in/yaml.v2"
)
const timeFormat = "02-01-2006 15:04"
type (
Task struct {
Name string `yaml:"task"`
Score int `yaml:"score"`
}
Group struct {
Name string `yaml:"group"`
Start string `yaml:"start"`
Deadline string `yaml:"deadline"`
Tasks []Task `yaml:"tasks"`
}
Deadlines []Group
)
func (g Group) IsOpen() bool {
t, _ := time.Parse(timeFormat, g.Start)
return time.Until(t) < 0
}
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 := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var d Deadlines
if err := yaml.Unmarshal(b, &d); err != nil {
return nil, fmt.Errorf("error reading deadlines: %w", err)
}
for _, g := range d {
if _, err := time.Parse(timeFormat, g.Start); err != nil {
return nil, fmt.Errorf("invalid time format in task %q: %w", g.Name, err)
}
if _, err := time.Parse(timeFormat, g.Deadline); err != nil {
return nil, fmt.Errorf("invalid time format in task %q: %w", g.Name, err)
}
}
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])
2020-02-12 22:54:25 +00:00
if task == nil {
continue
}
2020-02-12 23:28:27 +00:00
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
}