-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
54 lines (48 loc) · 1.06 KB
/
exec.go
File metadata and controls
54 lines (48 loc) · 1.06 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
package main
import (
"bytes"
"log"
"os/exec"
"text/template"
)
func execFuncs() template.FuncMap {
return template.FuncMap{
"stdout": commandStdout,
"stderr": commandStderr,
"combinedout": commandCombinedout,
"shell": shellExec,
}
}
func commandStdout(command string, args ...string) string {
cmd := exec.Command(command, args...)
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func commandStderr(command string, args ...string) string {
cmd := exec.Command(command, args...)
stderr := bytes.Buffer{}
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return stderr.String()
}
func commandCombinedout(command string, args ...string) string {
cmd := exec.Command(command, args...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func shellExec(command string) string {
cmd := exec.Command("sh", "-c", command)
out, err := cmd.Output()
if err != nil {
log.Fatalf("sh -c %s failed with %+v\n", command, err)
}
return string(out)
}