/
capture.go
62 lines (53 loc) · 1.4 KB
/
capture.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
package job
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/dnephin/dobi/config"
"github.com/dnephin/dobi/logging"
"github.com/dnephin/dobi/tasks/context"
"github.com/dnephin/dobi/tasks/task"
"github.com/dnephin/dobi/tasks/types"
)
func newCaptureTask(variable string) types.TaskBuilder {
return func(name task.Name, conf config.Resource) types.Task {
buffer := bytes.NewBufferString("")
return &captureTask{
runTask: &Task{
name: name,
config: conf.(*config.JobConfig),
outStream: buffer,
},
variable: variable,
buffer: buffer,
}
}
}
type captureTask struct {
types.NoStop
runTask *Task
variable string
buffer *bytes.Buffer
}
// Name returns the name of the task
func (t *captureTask) Name() task.Name {
return t.runTask.name
}
// Repr formats the task for logging
func (t *captureTask) Repr() string {
return fmt.Sprintf("%s capture %s", t.runTask.name.Format("job"), t.variable)
}
// Run the job to capture the output in a variable
func (t *captureTask) Run(ctx *context.ExecuteContext, depsModified bool) (bool, error) {
modified, err := t.runTask.Run(ctx, depsModified)
if err != nil {
return modified, err
}
out := strings.TrimSpace(t.buffer.String())
if current, ok := os.LookupEnv(t.variable); ok && current == out {
return false, nil
}
logging.ForTask(t).Debugf("Setting %q to: %s", t.variable, out)
return true, os.Setenv(t.variable, out)
}