This repository was archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexec.go
124 lines (96 loc) · 2.84 KB
/
exec.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"os/exec"
"strings"
"github.com/talwat/spingo"
)
// Runs a command and prints the output to the terminal in real time.
func runCommandRealTime(workDir string, cmd string, args ...string) {
cmdObj := exec.Command(cmd, args...)
cmdObj.Dir = workDir
// Get output pipes
cmdReader, err := cmdObj.StdoutPipe()
cmdObj.Stderr = cmdObj.Stdout
errorLogNewlineBefore(err, "An error occurred while creating stdout pipe")
if debug {
rawLog("\n")
}
err = cmdObj.Start()
errorLogNewlineBefore(err, "An error occurred while starting command")
for {
tmp := make([]byte, 1024)
_, err := cmdReader.Read(tmp)
rawLog(string(tmp))
if err != nil {
break
}
}
err = cmdObj.Wait()
errorLogNewlineBefore(err, "An error occurred while running command")
}
// Runs a command and displays dots to indicate progress.
func runCommandDot(commandName string, workDir string, forceCmd bool, cmd string, args ...string) {
spinner := spingo.Spinner{ // nolint:exhaustruct
Prefix: logType[1] + " Running command " + bolden(commandName) + " ",
SpinnerArt: []string{
textCol.Violet + "/" + RESETCOL,
textCol.Violet + "|" + RESETCOL,
textCol.Violet + "\\" + RESETCOL,
textCol.Violet + "-" + RESETCOL,
},
}
debugLog("Progress indicator: %s", config.Progress.CompileProgressIndicator)
if config.Progress.CompileProgressIndicator == "dots" {
logNoNewline(1, "Running command %s", bolden(commandName))
} else {
spinner.DisplaySpinner()
}
parsedCmd := strings.TrimPrefix(cmd, "!(FORCE)! ")
cmdObj := exec.Command(parsedCmd, args...)
cmdObj.Dir = workDir
cmdReader, err := cmdObj.StdoutPipe()
cmdObj.Stderr = cmdObj.Stdout
errorLogNewlineBefore(err, "An error occurred while creating stdout pipe")
if debug {
rawLog("\n")
}
err = cmdObj.Start()
errorLogNewlineBefore(err, "An error occurred while starting command")
output := ""
scanner := bufio.NewScanner(cmdReader)
for scanner.Scan() {
output += scanner.Text() + "\n"
if debug {
rawLogf(logType[5]+(" %s\n"), scanner.Text())
} else {
if config.Progress.CompileProgressIndicator == "dots" {
rawLog(textCol.Violet + "." + RESETCOL)
} else {
spinner.DisplaySpinner()
}
}
}
output = strings.TrimSpace(output)
err = cmdObj.Wait()
if forceCmd {
log(1, "Command is marked as force, so not checking for error.")
} else {
errorLogNewlineBefore(err, "An error occurred while running command. Output: %s", output)
}
}
// Runs a command.
func runCommand(workDir string, cmd string, args ...string) (string, error) {
cmdObj := exec.Command(cmd, args...)
cmdObj.Dir = workDir
data, err := cmdObj.CombinedOutput()
if err != nil {
return string(data), err
}
return strings.TrimSuffix(string(data), "\n"), nil
}
// Check if a command exists.
func checkIfCommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}