-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.go
More file actions
74 lines (59 loc) · 1.36 KB
/
task.go
File metadata and controls
74 lines (59 loc) · 1.36 KB
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
package main
import (
"fmt"
"io"
"os/exec"
"strings"
)
type Task struct {
extWriter io.Writer
newline bool
}
type TaskFailed struct {}
func (task *Task) Write(p []byte) (n int, err error) {
// TODO handle the case when p[len(np)-1] != "\n"
np := ""
if task.newline {
np = "\t"
task.newline = false
}
np += strings.Replace(string(p), "\n", "\n\t", -1)
if strings.HasSuffix(np, "\n\t") {
np = np[:len(np)-1]
task.newline = true
}
n, err = task.extWriter.Write([]byte(np))
return n-len(np)+len(p), err
}
func (task *Task) Finish() {
//task.extWriter.Write([]byte("finished\n"))
}
func (task *Task) Assert(test bool, err error) {
if !test {
task.Require(err)
}
}
func (task *Task) Require(err error) {
if err != nil {
fmt.Fprintln(task, "ERROR: " + err.Error())
panic(TaskFailed{})
}
}
func (task *Task) RequireAndFinish(err error) {
defer task.Finish()
task.Require(err)
}
func (task *Task) RunCmd(cmd *exec.Cmd) error {
subtask := NewTask(task, cmd.Args[0] + " (" + strings.Join(cmd.Args[1:], ") (") + ")"); defer subtask.Finish()
cmd.Stdout = subtask
cmd.Stderr = subtask
return cmd.Run()
}
func (task *Task) RunCommand(name string, arg ...string) error {
return task.RunCmd(exec.Command(name, arg...))
}
func NewTask(outer io.Writer, desc string) *Task {
outer.Write([]byte(desc + "...\n"))
task := &Task{outer, true}
return task
}