-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexec.go
98 lines (84 loc) · 1.98 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
package phpfuncs
import (
"bytes"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
var rPatt *regexp.Regexp
func init() {
rPatt = regexp.MustCompile(`[^\w@%+=:,./-]`)
}
// Exec - Start a command on system
//
// Original : https://www.php.net/manual/tr/function.exec.php
//
// exec() executes the given command.
func Exec(of string) string {
var out bytes.Buffer
cmd := exec.Command(of)
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return out.String()
}
// ShellExec - Execute command via shell and return the complete output as a string
//
// Original : https://www.php.net/manual/en/function.shell-exec.php
//
// This function is identical to the backtick operator.
func ShellExec(of string) string {
var out bytes.Buffer
cmd := exec.Command(of)
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return out.String()
}
// Exit - Output a message and terminate the current script
//
// Original : https://www.php.net/manual/en/function.exit.php
//
// Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
func Exit(of int) {
os.Exit(of)
}
// Die - Equivalent to exit
//
// Original : https://www.php.net/manual/en/function.die.php
//
// This language construct is equivalent to exit().
func Die(of int) {
os.Exit(of)
}
// Escapeshellarg - Escape a string to be used as a shell argument
//
// Original: https://www.php.net/manual/en/function.escapeshellarg.php
//
func Escapeshellarg(s string) string {
if len(s) == 0 {
return "''"
}
if rPatt.MatchString(s) {
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
}
return s
}
// Escapeshellcmd - Escape shell metacharacters
//
// Original: https://www.php.net/manual/en/function.escapeshellcmd.php
//
func Escapeshellcmd(s string) string {
cmds := Explode(s, " ") // I know thats maybe not proper way...
z := make([]string, len(cmds))
for i, s := range cmds {
z[i] = Escapeshellarg(s)
}
return Join(" ", z)
}