-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.go
61 lines (56 loc) · 1.33 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
package shell
import (
"fmt"
"io"
"os/exec"
"reflect"
"strings"
"unicode"
)
// Commandf runs a shell command based on the format string
func Commandf(format string, a ...interface{}) *exec.Cmd {
shellCmd := Sprintf(format, a...)
return exec.Command("/bin/sh", "-c", shellCmd)
}
// Sprintf generates a shell command with the format arguments escaped.
func Sprintf(format string, a ...interface{}) string {
wrapped := make([]interface{}, len(a))
for i, v := range a {
wrapped[i] = escapable{v}
}
return fmt.Sprintf(format, wrapped...)
}
type escapable struct {
x interface{}
}
func (e escapable) Format(f fmt.State, c rune) {
s := "%"
for i := 0; i < 128; i++ {
if f.Flag(i) {
s += string(i)
}
}
if w, ok := f.Width(); ok {
s += fmt.Sprintf("%d", w)
}
if p, ok := f.Precision(); ok {
s += fmt.Sprintf(".%d", p)
}
// If we have an uppercase format char and a slice, format each slice
// element
if unicode.IsUpper(c) && reflect.TypeOf(e.x).Kind() == reflect.Slice {
s += strings.ToLower(string(c))
v := reflect.ValueOf(e.x)
for i := 0; i < v.Len(); i++ {
formatted := fmt.Sprintf(s, v.Index(i))
io.WriteString(f, ReadableEscapeArg(formatted))
if i+1 != v.Len() {
io.WriteString(f, " ")
}
}
return
}
s += string(c)
formatted := fmt.Sprintf(s, e.x)
io.WriteString(f, ReadableEscapeArg(formatted))
}