-
Notifications
You must be signed in to change notification settings - Fork 73
/
shell.go
153 lines (138 loc) · 2.81 KB
/
shell.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
package zero
import (
"flag"
"reflect"
"strings"
)
func isSpace(r rune) bool {
switch r {
case ' ', '\t', '\r', '\n':
return true
}
return false
}
type argType int
const (
argNo argType = iota
argSingle
argQuoted
)
// ParseShell 将指令转换为指令参数.
// modified from https://github.com/mattn/go-shellwords
func ParseShell(s string) []string {
var args []string
buf := strings.Builder{}
var escaped, doubleQuoted, singleQuoted, backQuote bool
backtick := ""
got := argNo
for _, r := range s {
if escaped {
buf.WriteRune(r)
escaped = false
got = argSingle
continue
}
if r == '\\' {
if singleQuoted {
buf.WriteRune(r)
} else {
escaped = true
}
continue
}
if isSpace(r) {
if singleQuoted || doubleQuoted || backQuote {
buf.WriteRune(r)
backtick += string(r)
} else if got != argNo {
args = append(args, buf.String())
buf.Reset()
got = argNo
}
continue
}
switch r {
case '`':
if !singleQuoted && !doubleQuoted {
backtick = ""
backQuote = !backQuote
}
case '"':
if !singleQuoted {
if doubleQuoted {
got = argQuoted
}
doubleQuoted = !doubleQuoted
}
case '\'':
if !doubleQuoted {
if singleQuoted {
got = argSingle
}
singleQuoted = !singleQuoted
}
default:
got = argSingle
buf.WriteRune(r)
if backQuote {
backtick += string(r)
}
}
}
if got != argNo {
args = append(args, buf.String())
}
return args
}
// ShellRule 定义shell-like规则
func ShellRule(cmd string, model interface{}) Rule {
cmdRule := CommandRule(cmd)
t := reflect.TypeOf(model)
return func(ctx *Ctx) bool {
if !cmdRule(ctx) {
return false
}
// bind flag to struct
args := ParseShell(ctx.State["args"].(string))
val := reflect.New(t)
fs := registerFlag(t, val)
err := fs.Parse(args)
if err != nil {
return false
}
ctx.State["args"] = fs.Args()
ctx.State["flag"] = val.Interface()
return true
}
}
var (
boolType = reflect.TypeOf(false)
intType = reflect.TypeOf(0)
stringType = reflect.TypeOf("")
float64Type = reflect.TypeOf(float64(0))
)
func registerFlag(t reflect.Type, v reflect.Value) *flag.FlagSet {
v = v.Elem()
fs := flag.NewFlagSet("", flag.ContinueOnError)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
name := field.Tag.Get("flag")
if name == "" {
continue
}
help := field.Tag.Get("help")
switch field.Type {
case boolType:
fs.BoolVar(v.Field(i).Addr().Interface().(*bool), name, false, help)
case intType:
fs.IntVar(v.Field(i).Addr().Interface().(*int), name, 0, help)
case stringType:
fs.StringVar(v.Field(i).Addr().Interface().(*string), name, "", help)
case float64Type:
fs.Float64Var(v.Field(i).Addr().Interface().(*float64), name, 0, help)
default:
panic("unsupported type")
}
}
return fs
}