-
Notifications
You must be signed in to change notification settings - Fork 688
/
shell.go
37 lines (32 loc) · 853 Bytes
/
shell.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
package tpu
import (
"os/exec"
"strings"
)
func ShellLog(command string, logln func(string)) (string, error) {
return CmdLog([]string{"sh", "-c", command}, logln)
}
func Cmd(command ...string) (string, error) {
return CmdLog(command, func(string) {})
}
func CmdLogf(command []string, logf func(string, ...interface{})) (string, error) {
return CmdLog(command, func(line string) { logf("%s", line) })
}
func CmdLog(command []string, logln func(string)) (string, error) {
logln(strings.Join(command, " "))
cmd := exec.Command(command[0], command[1:]...)
out, err := cmd.CombinedOutput()
str := string(out)
lines := strings.Split(str, "\n")
for idx, line := range lines {
if strings.TrimSpace(line) != "" {
logln(line)
} else if idx != len(lines)-1 {
logln(line)
}
}
if err != nil {
logln(err.Error())
}
return str, err
}