/
apppath.go
90 lines (76 loc) · 1.6 KB
/
apppath.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
// +build windows
package windowsapi
import (
"os"
"os/exec"
"path/filepath"
"strings"
)
func GetAppDir() string {
lp, _ := exec.LookPath(os.Args[0])
p, err := filepath.Abs(lp)
if err != nil {
p = os.Args[0]
}
dir := filepath.Dir(p)
return dir
}
func GetAppExePath() string {
lp, err := exec.LookPath(os.Args[0])
if err != nil {
lp = os.Args[0]
}
p, err := filepath.Abs(lp)
if err != nil {
p = lp
}
return p
}
//ParseCommand is spilt command to args
//https://github.com/golang-devops/parsecommand
func ParseCommand(line string) ([]string) {
args := [][]rune{}
var quoteChar rune
var isInQuote bool = false
trimmedLine := strings.TrimSpace(line)
currentArg := []rune{}
trimmedLineLen := len(trimmedLine)
for i, c := range trimmedLine {
isLastChar := i + len(string(c)) == trimmedLineLen
if !isInQuote && (c == '"' || c == '\'') {
isInQuote = true
quoteChar = c
if isLastChar {
args = append(args, currentArg)
}
continue
}
if isInQuote && c == quoteChar {
//Ensure it is not escaped with a slash beforehand
if i == 0 || trimmedLine[i-1] != '\\' {
isInQuote = false
if isLastChar {
args = append(args, currentArg)
}
continue
}
}
if !isInQuote && c == ' ' {
//Ignore multiple spaces
if i > 0 && trimmedLine[i-1] != ' ' {
args = append(args, currentArg)
currentArg = []rune{}
continue
}
}
currentArg = append(currentArg, c)
if isLastChar {
args = append(args, currentArg)
}
}
strArgs := []string{}
for _, a := range args {
strArgs = append(strArgs, strings.TrimSpace(string(a)))
}
return strArgs
}