-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
205 lines (194 loc) · 4.65 KB
/
Copy pathcommand.go
File metadata and controls
205 lines (194 loc) · 4.65 KB
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
205
package main
import (
"fmt"
"os"
"os/exec"
"strings"
shellquote "github.com/kballard/go-shellquote"
)
type group struct {
hidden bool
terms []string
// paths which precede a group externally
// are used by files/dirs/all directives
externalPath string
}
type command struct {
tokens []string
original string
template string
groups []group
commands []string
}
func newCommand(tokens ...string) (c command) {
c.tokens = tokens
c.checkFlags()
c.getGroups()
c.getCommands(0, "", []string{})
return
}
func newGroup(s string, externalPath string, inSingles state, inDoubles state) (g group) {
var terms []string
var escaping state
lastComma := -1
g.hidden, s = isHidden(s)
for i, char := range s {
if !escaping.on && char == ',' {
terms = append(terms, expand(stripSlashes(s[lastComma+1:i]), externalPath, inSingles.on, inDoubles.on)...)
lastComma = i
}
escaping.toggle(char, '\\', true)
}
terms = append(terms, expand(stripSlashes(s[lastComma+1:len(s)]), externalPath, inSingles.on, inDoubles.on)...)
g.externalPath = externalPath
g.terms = terms
return
}
func (c *command) getCommands(startGroup int, s string, curTerms []string) {
var curTerm string
if s == "" {
s = c.template
}
if len(c.groups) == 0 {
c.commands = append(c.commands, s)
return
}
for _, t := range c.groups[startGroup].terms {
if len(c.groups[startGroup].terms) == 1 {
t = backref(t, curTerms)
}
if c.groups[startGroup].hidden {
curTerm = ""
} else {
curTerm = t
}
if startGroup < len(c.groups)-1 {
c.getCommands(startGroup+1, strings.Replace(s, lupGroup(startGroup), curTerm, 1), append(curTerms, t))
} else {
newCommand := strings.Replace(s, lupGroup(startGroup), curTerm, 1)
c.commands = append(c.commands, newCommand)
}
}
}
func (c *command) getGroups() {
var escaping bool
var path string
var curGroup int
inSingles := state{on: false}
inDoubles := state{on: false}
pathStart := -1
groupStart := -1
for i, char := range c.original {
if !escaping {
if groupStart == -1 {
if pathStart == -1 && char == '/' {
pathStart = i
}
if pathStart > -1 && char == ' ' {
pathStart = -1
}
inSingles.toggle(char, '\'', false)
inDoubles.toggle(char, '"', false)
}
if char == '\\' {
escaping = true
} else if char == delimiter {
if pathStart > -1 {
path = c.original[pathStart:i]
}
pathStart = -1
if groupStart == -1 {
groupStart = i
} else {
c.groups = append(c.groups, newGroup(c.original[groupStart+1:i], path, inSingles, inDoubles))
c.template = strings.Replace(c.template, c.original[groupStart-len(path):i+1], lupGroup(curGroup), 1)
path = ""
curGroup++
groupStart = -1
}
}
} else {
if char != '\\' {
escaping = false
}
}
}
c.template = stripSlashes(c.template)
}
func (c *command) checkFlags() {
if len(c.tokens) > 0 {
for i := 0; i < len(c.tokens); i++ {
if !strings.HasPrefix(c.tokens[i], "-") {
c.tokens = c.tokens[i:]
c.original = shellquote.Join(c.tokens...)
c.template = c.original
break
}
switch c.tokens[i] {
case "-V", "--version":
fmt.Println(version)
os.Exit(0)
case "-h", "--help":
showHelp()
os.Exit(0)
case "-t", "--test":
dryRun = true
default:
fmt.Fprintf(os.Stderr, "Flag not recognised (%s), try using lup -h to see the help\n", c.tokens[i])
os.Exit(2)
}
}
}
}
func (c *command) run() int {
var retcode int
var cmd *exec.Cmd
for _, command := range c.commands {
switch shell {
case "powershell.exe":
command = "powershell -C " + strings.Replace(command, "\\!", "!", -1)
if input != "" {
command = shell + " -c \"Write-Host " + shellquote.Join(input) + " | " + command + "\""
}
case "cmd.exe":
//todo
default:
if input != "" {
command = shell + " -c \"echo " + shellquote.Join(input) + " | " + command + "\""
}
}
if shell == "powershell" {
if dryRun {
fmt.Println("powershell -Command", command)
} else {
cmd = exec.Command("powershell", "-Command", command)
cmd.Stdout, cmd.Stdin, cmd.Stderr = os.Stdout, os.Stdin, os.Stderr
if err := cmd.Run(); err != nil {
retcode = 1
}
}
} else {
t, err := shellquote.Split(command)
if err != nil {
errOn(err, "Couldn't split command", 5)
}
if dryRun {
fmt.Println(command)
} else if len(t) > 0 {
if len(t) > 1 {
cmd = exec.Command(t[0], t[1:]...)
} else if len(t) == 1 {
cmd = exec.Command(t[0])
} else {
showHelp()
os.Exit(0)
}
cmd.Stdout, cmd.Stdin, cmd.Stderr = os.Stdout, os.Stdin, os.Stderr
if err := cmd.Run(); err != nil {
retcode = 1
}
}
}
}
return retcode
}