-
Notifications
You must be signed in to change notification settings - Fork 5
/
actions.go
73 lines (56 loc) · 1.74 KB
/
actions.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
package main
import (
"fmt"
"github.com/centerorbit/mustache"
"io"
"os"
"os/exec"
"strings"
)
/// *** Actions *** ///
var execCommand = exec.Command
func defaultAction(complete chan<- bool, dep dep, perform perform) {
mustachedActionParams := applyMustache(dep.Params, perform.Action, perform.Verbose)
if perform.DryRun && perform.Verbose {
fmt.Println("Dry run of: `", perform.Kind, strings.Join(mustachedActionParams, " "), "` for: ", dep.Location)
} else {
if perform.Verbose {
fmt.Println("Running: `", perform.Kind, strings.Join(mustachedActionParams, " "), "` for: ", dep.Location)
}
cmd := execCommand(perform.Kind, mustachedActionParams...)
cmd.Dir = dep.Location
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
if perform.Verbose {
fmt.Println("Couldn't start command: ", perform.Kind+" "+strings.Join(mustachedActionParams, " "))
fmt.Println("Due to this error:", err)
}
} else {
go func() { _, _ = io.Copy(os.Stdout, stdout) }()
go func() { _, _ = io.Copy(os.Stderr, stderr) }()
err = cmd.Wait()
if err != nil {
if perform.Verbose {
fmt.Println("Command finished with error: ", err)
fmt.Println(perform.Kind + " " + strings.Join(mustachedActionParams, " "))
}
}
}
}
complete <- true
}
/// *** Helpers *** ///
func applyMustache(params map[string]string, actionParams []string, verbose bool) []string {
var mustachedActionParams []string
mustache.AllowMissingVariables = false
for _, value := range actionParams {
data, err := mustache.Render(value, params)
if err != nil && verbose {
fmt.Println("Warning: ", err)
}
mustachedActionParams = append(mustachedActionParams, data)
}
return mustachedActionParams
}