-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.go
253 lines (213 loc) · 5.02 KB
/
cli.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cli
import (
"github.com/platinasystems/elib/elog"
"github.com/platinasystems/elib/iomux"
"github.com/platinasystems/elib/parse"
"errors"
"fmt"
"io"
"strings"
)
type Writer interface {
io.Writer
}
type Input struct{ parse.Input }
type Commander interface {
CliName() string
CliAction(w Writer, in *Input) error
}
type Helper interface {
CliHelp() string
}
type ShortHelper interface {
CliShortHelp() string
}
type LoopStarter interface {
CliLoopStart(m *Main)
}
type Action func(c Commander, w Writer, in *Input) (err error)
type Command struct {
// Command name separated by space; alias by commas.
Name string
ShortHelp, Help string
Action
}
func (c *Command) CliName() string { return c.Name }
func (c *Command) CliShortHelp() string { return c.ShortHelp }
func (c *Command) CliHelp() string { return c.Help }
func (c *Command) CliAction(w Writer, in *Input) (err error) { return c.Action(c, w, in) }
type command struct {
name string
names []string
}
type subCommand struct {
name string
cmds map[string]Commander
subs map[string]*subCommand
}
func (c *subCommand) Elts() int { return len(c.cmds) + len(c.subs) }
type File struct {
main *Main
ServerConfig
closeAfterTxFlush bool
poolIndex fileIndex
iomux.FileReadWriteCloser
}
type fileIndex uint
//go:generate gentemplate -d Package=cli -id file -d Data=Files -d PoolType=FilePool -d Type=File github.com/platinasystems/elib/pool.tmpl
type Main struct {
in Input
// Root of command tree.
rootCmd subCommand
allCmds map[string]Commander
Prompt string
RxReady func(c *File)
FilePool
servers []*Server
}
func normalizeName(n string) string { return strings.ToLower(n) }
func (m *Main) AddCommand(C Commander) {
ns := strings.Split(C.CliName(), ",")
for i := range ns {
m.addCommand(C, ns[i])
}
}
func (m *Main) addCommand(C Commander, name string) {
c := &command{name: name}
if m.allCmds == nil {
m.allCmds = make(map[string]Commander)
}
m.allCmds[c.name] = C
c.names = strings.Split(c.name, " ")
n := len(c.names)
if n == 0 {
panic(fmt.Errorf("name only whitespace: `%s'", c.name))
}
sub := &m.rootCmd
for i := 0; i < n; i++ {
// Normalize to lower case.
c.names[i] = normalizeName(c.names[i])
name := c.names[i]
if i+1 < n {
if sub.subs == nil {
sub.subs = make(map[string]*subCommand)
}
var (
x *subCommand
ok bool
)
if x, ok = sub.subs[name]; !ok {
x = &subCommand{}
sub.subs[name] = x
}
sub = x
} else {
if sub.cmds == nil {
sub.cmds = make(map[string]Commander)
}
sub.cmds[name] = C
}
}
}
func (sub *subCommand) uniqueCommand(matching string) (Commander, bool) {
n := 0
var c Commander
for k, v := range sub.cmds {
if strings.Index(k, matching) == 0 {
c = v
n++
}
}
ok := n == 1
if !ok {
c = nil
}
return c, ok
}
func (sub *subCommand) uniqueSubCommand(matching string) (*subCommand, bool) {
n := 0
var c *subCommand
for k, v := range sub.subs {
if strings.Index(k, matching) == 0 {
c = v
n++
}
}
ok := n == 1
if !ok {
c = nil
}
return c, ok
}
var (
ErrAmbiguous = errors.New("ambiguous")
ParseError = errors.New("parse error") // generic parse error
)
func (m *Main) lookup(in *Input) (Commander, error) {
sub := &m.rootCmd
for !in.End() {
text := in.Token()
name := normalizeName(text)
// Exact match for sub-command.
if x, ok := sub.subs[name]; ok {
sub = x
continue
}
// Unique match for sub-command.
if x, ok := sub.uniqueSubCommand(name); ok {
sub = x
continue
}
// Exact match.
if x, ok := sub.cmds[name]; ok {
return x, nil
}
// Unique match for command.
if x, ok := sub.uniqueCommand(name); ok {
return x, nil
}
// Not found
return nil, fmt.Errorf("unknown: %s", name)
}
return nil, ErrAmbiguous
}
func (m *Main) ExecInput(w io.Writer, in *Input) (err error) {
var c Commander
if c, err = m.lookup(in); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
if pe, ok := e.(*parse.Err); ok {
err = errors.New(c.CliName() + ": " + pe.Error())
} else if f, ok := e.(error); ok {
err = f
} else {
panic(e)
}
}
}()
// Potentially skip leading and trailing {} in input line.
var line Input
in.Parse("%l", &line.Input)
if elog.Enabled() {
elog.F("cli %s %s", c.CliName(), &line)
}
err = c.CliAction(w, &line)
return
}
func (m *Main) Exec(w io.Writer, r io.Reader) error {
in := &m.in
in.Init(r)
return m.ExecInput(w, in)
}
var Default = &Main{
Prompt: "# ",
}
func (c *Main) Add(name string, action Action) { c.AddCommand(&Command{Name: name, Action: action}) }
func AddCommand(c Commander) { Default.AddCommand(c) }
func ExecInput(w io.Writer, in *Input) error { return Default.ExecInput(w, in) }
func Exec(w io.Writer, r io.Reader) error { return Default.Exec(w, r) }