forked from cortesi/modd
/
prep.go
89 lines (82 loc) · 1.85 KB
/
prep.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
78
79
80
81
82
83
84
85
86
87
88
89
package modd
import (
"bytes"
"os/exec"
"sync"
"github.com/cortesi/modd/conf"
"github.com/cortesi/modd/notify"
"github.com/cortesi/modd/varcmd"
"github.com/cortesi/modd/watch"
"github.com/cortesi/termlog"
)
// ProcError is a process error, possibly containing command output
type ProcError struct {
shorttext string
Output string
}
func (p ProcError) Error() string {
return p.shorttext
}
// RunProc runs a process to completion, sending output to log
func RunProc(cmd string, log termlog.Stream) error {
log.Header()
sh, error := getShell()
if error != nil {
return error
}
c := exec.Command(sh, "-c", cmd)
stdo, err := c.StdoutPipe()
if err != nil {
return err
}
stde, err := c.StderrPipe()
if err != nil {
return err
}
buff := new(bytes.Buffer)
mut := sync.Mutex{}
err = c.Start()
if err != nil {
return err
}
wg := sync.WaitGroup{}
wg.Add(2)
go logOutput(
&wg, stde,
func(s string, args ...interface{}) {
log.Warn(s)
mut.Lock()
defer mut.Unlock()
buff.WriteString(s + "\n")
},
)
go logOutput(&wg, stdo, log.Say)
wg.Wait()
err = c.Wait()
if err != nil {
log.Shout("%s", c.ProcessState.String())
return ProcError{err.Error(), buff.String()}
}
log.Notice(">> done (%s)", c.ProcessState.UserTime())
return nil
}
// RunPreps runs all commands in sequence. Stops if any command returns an error.
func RunPreps(b conf.Block, vars map[string]string, mod *watch.Mod, log termlog.TermLog, notifiers []notify.Notifier) error {
vcmd := varcmd.VarCmd{Block: &b, Mod: mod, Vars: vars}
for _, p := range b.Preps {
cmd, err := vcmd.Render(p.Command)
if err != nil {
return err
}
err = RunProc(cmd, log.Stream(niceHeader("prep: ", cmd)))
if err != nil {
if pe, ok := err.(ProcError); ok {
for _, n := range notifiers {
n.Push("modd error", pe.Output, "")
}
}
return err
}
}
return nil
}