-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptrunner.go
76 lines (59 loc) · 1.29 KB
/
scriptrunner.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
package main
import (
"bytes"
"os"
"os/exec"
log "github.com/sirupsen/logrus"
)
// ShellScript is ..
var scriptRunner *Script
// Script is ...
type Script struct {
workDir string
script string
args []string
}
// Script is ..
func (s *Script) Script() string {
return s.script
}
// Args is ..
func (s *Script) Args() []string {
return s.args
}
// Exec is ..
func (s *Script) Exec() (int, bytes.Buffer, bytes.Buffer, error) {
e := s.script
a := s.args
log.Printf("Running [%v] with [%v]", e, a)
var err error
tmpfile, err := os.CreateTemp(os.TempDir(), "unmarked.*.script")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write([]byte(e)); err != nil {
tmpfile.Close()
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
err = os.Chmod(tmpfile.Name(), 0700)
if err != nil {
log.Fatal(err)
}
cmd := exec.Command(tmpfile.Name(), a...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
err = cmd.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
log.Errorf("exitcode: %d: %s (%s)", exiterr.ExitCode(), err, stderr.String())
log.Printf("\a")
return exiterr.ExitCode(), stdout, stderr, err
}
}
return 0, stdout, stderr, err
}