forked from keegancsmith/shell
/
command.go
44 lines (39 loc) · 917 Bytes
/
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
package shell
import (
"fmt"
"io"
"os/exec"
)
// 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)
}
s += string(c)
formatted := fmt.Sprintf(s, e.x)
io.WriteString(f, ReadableEscapeArg(formatted))
}