forked from cortesi/modd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep.go
83 lines (75 loc) · 1.8 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
package modd
import (
"time"
"github.com/cortesi/modd/conf"
"github.com/cortesi/modd/notify"
"github.com/cortesi/modd/shell"
"github.com/cortesi/modd/varcmd"
"github.com/cortesi/moddwatch"
"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, shellMethod string, dir string, log termlog.Stream) error {
log.Header()
ex, err := shell.NewExecutor(shellMethod, cmd, dir)
if err != nil {
return err
}
start := time.Now()
err, estate := ex.Run(log, true)
if err != nil {
return err
} else if estate.Error != nil {
log.Shout("%s", estate.Error)
return ProcError{estate.Error.Error(), estate.ErrOutput}
}
log.Notice(">> done (%s)", time.Since(start))
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 *moddwatch.Mod,
log termlog.TermLog,
notifiers []notify.Notifier,
initial bool,
) error {
sh, err := shell.GetShellName(vars[shellVarName])
if err != nil {
return err
}
var modified []string
if mod != nil {
modified = mod.All()
}
vcmd := varcmd.VarCmd{Block: &b, Modified: modified, Vars: vars}
for _, p := range b.Preps {
cmd, err := vcmd.Render(p.Command)
if initial && p.Onchange {
log.Say(niceHeader("skipping prep: ", cmd))
continue
}
if err != nil {
return err
}
err = RunProc(cmd, sh, b.InDir, 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
}