-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 881 Bytes
/
main.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
package main
import (
"fmt"
"os"
"github.com/zdz1715/go-sh"
)
func main() {
e, err := sh.NewExec(&sh.ExecOptions{
Storage: &sh.Storage{
Dir: "/tmp",
NotAutoClean: true,
},
})
if err != nil {
fmt.Printf("new exec fail:%s\n", err)
return
}
dir, _ := os.Getwd()
fmt.Printf("[%s] %s\n", dir, e.String())
e.AddRawCommand([]byte(`
# check user privileges
root_privs() {
if [[ $(id -u) -eq 0 ]]; then
return 0
else
return 1
fi
}
# check for required files and deps first
# check if command exists
command_exists() {
type "${1}" > /dev/null 2>&1
}
if (command_exists ls); then
echo "command_exists: ls"
fi
`))
if err = e.Run(); err != nil {
fmt.Printf("exec fail:%s\n", err)
}
}
/*
[go-sh] /bin/bash -ex -o pipefail /tmp/clc9uvco47mm9mrmcbfg
+ command_exists ls
+ type ls
+ echo 'command_exists: ls'
command_exists: ls
*/