-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
204 lines (178 loc) · 3.75 KB
/
command.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package script
import (
"strings"
)
type Command interface {
Binary() string
Args() []string
Add(input string)
AddAll(input ...string)
String() string
}
type LocalCommand struct {
elements []string
}
func NewLocalCommand() *LocalCommand {
l := LocalCommand{}
return &l
}
func LocalCommandFrom(command string) *LocalCommand {
c, args := SplitCommand(command)
l := NewLocalCommand()
l.Add(c)
l.AddAll(args...)
return l
}
func (l *LocalCommand) AddAll(input ...string) {
for _, i := range input {
l.Add(i)
}
}
func (l *LocalCommand) Add(input string) {
if l.elements == nil {
l.elements = make([]string, 0)
}
l.elements = append(l.elements, input)
}
func (l *LocalCommand) Binary() string {
if l.elements == nil || len(l.elements) == 0 {
return ""
}
return l.elements[0]
}
func (l *LocalCommand) Args() []string {
if l.elements == nil || len(l.elements) < 2 {
return []string{}
}
return l.elements[1:]
}
func isWrapped(source, s string) bool {
return strings.HasPrefix(source, s) && strings.HasSuffix(source, s)
}
func (l *LocalCommand) String() string {
var b strings.Builder
for i, e := range l.elements {
if i > 0 {
b.WriteString(" ")
}
// contains double quotes? escape them!
if strings.Contains(e, `"`) {
e = strings.ReplaceAll(e, `"`, `\"`)
}
// contains Whitespace? wrap with double quotes
if strings.Contains(e, ` `) {
if !isWrapped(e, `"`) && !isWrapped(e, `'`) {
e = `"` + e + `"`
}
}
b.WriteString(e)
}
return b.String()
}
// SplitCommand helper splits a string to command and arbitrarily many args.
// Does handle bash-like escaping (\) and string delimiters " and '.
func SplitCommand(input string) (command string, args []string) {
quotes := []string{`"`, `'`}
var (
ok bool
length int
value string
index = 0
)
args = make([]string, 0)
outerloop:
for {
if index >= len(input) {
break
}
ok, length, _ = parseWhitespace(input[index:])
if ok {
index += length
continue
}
for _, quote := range quotes {
ok, length, value = parseQuoted(input[index:], quote, `\`+quote)
if ok {
if command == "" {
command = value
} else {
args = append(args, value)
}
index += length
continue outerloop
}
}
ok, length, value = parseUnquoted(input[index:])
if ok {
if command == "" {
command = value
} else {
args = append(args, value)
}
index += length
continue
}
}
return
}
func parseQuoted(input, quoteString, escapeString string) (ok bool, length int, value string) {
if !strings.HasPrefix(input, quoteString) {
return
}
length = len(quoteString)
for {
if length >= len(input) {
break
}
// escaped quoteString? (continue!)
if strings.HasPrefix(input[length:], escapeString) {
length += len(escapeString)
value += quoteString
}
// quoteString (end!)
if strings.HasPrefix(input[length:], quoteString) {
length += len(quoteString)
ok = true
return
}
// otherwise inner content
value += input[length : length+1]
length++
}
return ok, length, value
}
func parseUnquoted(input string) (ok bool, length int, value string) {
length = 0
for {
if length >= len(input) {
ok = true
return
}
// whitespace (end!) // TODO all whitespace!
if strings.HasPrefix(input[length:], " ") {
length++
ok = true
return
}
// otherwise inner content
value += input[length : length+1]
length++
}
}
func parseWhitespace(input string) (ok bool, length int, value string) {
length = 0
for {
if length >= len(input) {
break
}
// no whitespace (end!) // TODO all whitespace!
if !strings.HasPrefix(input[length:], " ") {
ok = length > 0
return
}
// otherwise inner content (whitespace)
value += input[length : length+1]
length++
}
return ok, length, value
}