-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.go
More file actions
57 lines (47 loc) · 1.44 KB
/
Copy pathexecutor.go
File metadata and controls
57 lines (47 loc) · 1.44 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
package taskrunner
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/ATB-Bremen/taskrunner/platform"
)
func (t *TaskRunner) KillProcesses(processSubStrings []string) {
platform.KillProcesses(processSubStrings)
}
func (t *TaskRunner) ExecuteInDir(dir string, commandStr string, envs ...string) {
shortDir := strings.Replace(dir, t.Config.parentDir, "", -1)
t.Log.Info("in directory '%s', executing '%s'", shortDir, commandStr)
cmd := platform.BuildCommand(dir, commandStr)
appendEnvsToCommand(cmd, envs)
var stdoutBuf, stderrBuf bytes.Buffer
stdoutMulti := io.MultiWriter(os.Stdout, &stdoutBuf)
stderrMulti := io.MultiWriter(os.Stderr, &stderrBuf)
cmd.Stdout = stdoutMulti
cmd.Stderr = stderrMulti
startTime := time.Now()
err := cmd.Run()
elapsed := time.Since(startTime)
elapsedStr := fmt.Sprintf("%.3f", elapsed.Seconds())
elapsedTimeSummary := fmt.Sprintf("Time taken: %s seconds.", elapsedStr)
if err != nil {
t.Log.Error(" => Command failed. %s. Error: %v", elapsedTimeSummary, err)
t.ExitWithError()
} else {
t.Log.Info(" => Command successful. %s", elapsedTimeSummary)
}
}
func (t *TaskRunner) Execute(commandStr string, envs ...string) {
t.ExecuteInDir(".", commandStr, envs...)
}
func (t *TaskRunner) PromptForContinuation(prompt string) {
fmt.Printf("%s (y/N): ", prompt)
var response string
fmt.Scanln(&response)
if response != "y" && response != "Y" {
fmt.Println("Command aborted.")
os.Exit(0)
}
}