-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
67 lines (55 loc) · 1.37 KB
/
utility.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
package process
import (
"os"
"os/signal"
"strings"
)
var dev_null, _ = os.Open(os.DevNull)
// DevNull returns the NULL file and can be used to suppress
// input and output on processes. It can be used as a regular
// os.File
func DevNull() *os.File {
res := new(os.File)
*res = *dev_null
return res
}
func ListenForCTRLC() chan os.Signal {
exitC := make(chan os.Signal, 10)
signal.Notify(exitC, os.Interrupt)
return exitC
}
func StopListenForCTRLC(exitC chan os.Signal) {
signal.Stop(exitC)
close(exitC)
}
// ParseCommandArgs gets a list of strings and parses their content
// splitting them into separated strings. The characters used to parse
// the commands are, in the relevant order, <'>, <"> and < >
func ParseCommandArgs(args ...string) []string {
a := make([]string, 0)
for _, s := range args {
for i, s1 := range strings.Split(s, "'") {
if i%2 == 1 {
a = append(a, s1)
continue
}
for j, s2 := range strings.Split(s1, "\"") {
if j%2 == 1 {
a = append(a, s2)
continue
}
for _, s3 := range strings.Split(s2, " ") {
if s3 != "" {
a = append(a, s3)
}
}
}
}
}
return a
}
// FastCommandParse
func FastCommandParse(args ...string) (wd string, execName string, argV []string) {
a := ParseCommandArgs(args...)
return "", a[0], a[1:]
}