-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathregistry.go
366 lines (311 loc) · 8.88 KB
/
registry.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cookoo
// Copyright 2013 Masterminds.
import (
"fmt"
"strings"
)
// A Registry contains the the callback routes and the commands each
// route executes.
type Registry struct {
routes map[string]*routeSpec
orderedRouteNames []string
currentRoute *routeSpec
}
// NewRegistry returns a new initialized registry.
func NewRegistry() *Registry {
r := new(Registry)
r.Init()
return r
}
// Init initializes a registry. If a Registry is created through a means other
// than NewRegistry Init should be called on it.
func (r *Registry) Init() *Registry {
// Why 8?
r.routes = make(map[string]*routeSpec, 8)
r.orderedRouteNames = make([]string, 0, 8)
return r
}
// Route specifies a new route to add to the registry.
func (r *Registry) Route(name, description string) *Registry {
// Create the route spec.
route := new(routeSpec)
route.name = name
route.description = description
route.commands = make([]*commandSpec, 0, 4)
// Add the route spec.
r.currentRoute = route
r.routes[name] = route
r.orderedRouteNames = append(r.orderedRouteNames, name)
return r
}
func (r *Registry) DoesCmdDef(cd CommandDefinition, name string) *Registry {
// Configure command spec.
spec := new(commandSpec)
spec.name = name
spec.command = func(c Context, p *Params) (interface{}, Interrupt) {
// We don't have to clone cmd.Def because Map builds
// a new copy.
o, err := Map(c, p, cd)
if err != nil {
return nil, err
}
return o.Run(c)
}
// Add command spec.
r.currentRoute.commands = append(r.currentRoute.commands, spec)
return r
}
// Does adds a command to the end of the chain of commands for the current
// (most recently specified) route.
func (r *Registry) Does(cmd Command, commandName string) *Registry {
// Configure command spec.
spec := new(commandSpec)
spec.name = commandName
spec.command = cmd
// Add command spec.
r.currentRoute.commands = append(r.currentRoute.commands, spec)
return r
}
// Using specifies a paramater to use for the most recently specified command
// as set by Does.
func (r *Registry) Using(name string) *Registry {
// Look up the last command added.
lastCommand := r.lastCommandAdded()
// Create a new spec.
spec := new(paramSpec)
spec.name = name
// Add it to the list.
lastCommand.parameters = append(lastCommand.parameters, spec)
return r
}
// WithDefault specifies the default value for the most recently specified
// parameter as set by Using.
func (r *Registry) WithDefault(value interface{}) *Registry {
param := r.lastParamAdded()
param.defaultValue = value
return r
}
// From sepcifies where to get the value from for the most recently specified
// paramater as set by Using.
func (r *Registry) From(fromVal ...string) *Registry {
param := r.lastParamAdded()
// This is sort of a hack. Really, we should make params.from a []string.
param.from = strings.Join(fromVal, " ")
return r
}
// Get the last parameter for the last command added.
func (r *Registry) lastParamAdded() *paramSpec {
cspec := r.lastCommandAdded()
last := len(cspec.parameters) - 1
return cspec.parameters[last]
}
// Includes makes the commands from another route avaiable on this route.
func (r *Registry) Includes(route string) *Registry {
// Not that we don't clone commands; we just add the pointer to the current
// route.
spec := r.routes[route]
if spec == nil {
panicString := fmt.Sprintf("Could not find route %s. Skipping include.", route)
panic(panicString)
}
for _, cmd := range spec.commands {
r.currentRoute.commands = append(r.currentRoute.commands, cmd)
}
return r
}
// RouteSpec gets a ruote cased on its name.
func (r *Registry) RouteSpec(routeName string) (spec *routeSpec, ok bool) {
spec, ok = r.routes[routeName]
return
}
// Routes gets an unordered map of routes names to route specs.
//
// If order is important, use RouteNames to get the names (in order).
func (r *Registry) Routes() map[string]*routeSpec {
return r.routes
}
// RouteNames gets a slice containing the names of every registered route.
//
// The route names are returned in the order they were added to the
// registry. This is useful to some resolvers, which apply rules in order.
func (r *Registry) RouteNames() []string {
return r.orderedRouteNames
/*
names := make([]string, len(r.routes))
i := 0
for k := range r.routes {
names[i] = k
i++
}
return names
*/
}
// Look up the last command.
func (r *Registry) lastCommandAdded() *commandSpec {
lastIndex := len(r.currentRoute.commands) - 1
return r.currentRoute.commands[lastIndex]
}
type RouteDetails interface {
Name() string
Description() string
}
type routeSpec struct {
name, description string
commands []*commandSpec
}
func (r *routeSpec) Name() string {
return r.name
}
func (r *routeSpec) Description() string {
return r.description
}
type commandSpec struct {
name string
command Command
parameters []*paramSpec
}
type paramSpec struct {
name string
defaultValue interface{}
from string
}
// New public API
// AddRoute adds a single route to the registry.
func (r *Registry) AddRoute(route Route) error {
return r.AddRoutes(route)
}
func extractParams(cmd Task) []*paramSpec {
var paramspecs []*paramSpec
// FIXME: This is a horrible way to do this.
paramspecs = make([]*paramSpec, len(cmd.getParams()))
for j, prm := range cmd.getParams() {
pspec := ¶mSpec{
name: prm.Name,
defaultValue: prm.DefaultValue,
from: prm.From,
}
paramspecs[j] = pspec
}
return paramspecs
}
// AddRoutes adds one or more routes to the registry.
func (r *Registry) AddRoutes(routes ...Route) error {
for _, route := range routes {
cmdspecs := make([]*commandSpec, 0, len(route.Does))
for _, cmd := range route.Does {
switch cmd := cmd.(type) {
case CmdDef:
// This wraps the CmdDef inside of a command.
paramspecs := extractParams(cmd)
cmdspec := &commandSpec{
name: cmd.Name,
command: func(c Context, p *Params) (interface{}, Interrupt) {
// We don't have to clone cmd.Def because Map builds
// a new copy.
o, err := Map(c, p, cmd.Def)
if err != nil {
return nil, err
}
return o.Run(c)
},
parameters: paramspecs,
}
cmdspecs = append(cmdspecs, cmdspec)
case Cmd:
paramspecs := extractParams(cmd)
cmdspec := &commandSpec{
name: cmd.Name,
command: cmd.Fn,
parameters: paramspecs,
}
cmdspecs = append(cmdspecs, cmdspec)
case Include:
other, ok := r.RouteSpec(cmd.Path)
if !ok {
// Route not found.
return fmt.Errorf("Route '%s' not found.", cmd.Path)
}
cmdspecs = append(cmdspecs, other.commands...)
}
}
rspec := &routeSpec{
name: route.Name,
description: route.Help,
commands: cmdspecs,
}
// Add the route spec.
r.currentRoute = rspec
r.routes[rspec.name] = rspec
r.orderedRouteNames = append(r.orderedRouteNames, rspec.name)
}
return nil
}
// Route declares a new Cookoo route.
//
// A Route has a name, which is used to identify and call it, and Help. The
// Help can be used by other tools to generate help text or information about
// an application's structure.
//
// Routes are composed of a series of Tasks, each of which is executed in
// order.
type Route struct {
Name, Help string
Does Tasks
}
// Tasks represents a list of discrete tasks that are run on a Route.
//
// There are two kinds of Tasks: Cmd (a command) and Include, which imports a
// Tasks list from another route.
type Tasks []Task
// Cmd associates a cookoo.Command to a Route.
//
// The Name is the direct reference to a command. When a Command returns output,
// that output is inserted into the Context with the key Name.
//
// Fn specifies which cookoo.Command should be executed during this step.
//
// Using contains a list of Parameters that Cookoo can pass into the Command
// at execution time.
type Cmd struct {
Name string
Fn Command
Using Parameters
}
// Include imports all of the Tasks on another route into the present Route.
type Include struct {
Path string
}
type CmdDef struct {
Name string
Def CommandDefinition
Using Parameters
}
// A Task can be either an Include or a Cmd. This is a very lame way of
// making this behavior private.
type Task interface {
getParams() Parameters
}
func (i Include) getParams() Parameters {
return Parameters{}
}
func (c Cmd) getParams() Parameters {
return c.Using
}
func (c CmdDef) getParams() Parameters {
return c.Using
}
type Parameters []Param
// Param describes an individual parameter which will be passed to a Command.
//
// The Name is the name of the parameter. The Command itself dictates which
// Names it uses.
//
// The DefaultValue is the value of the Parameter if nothing else is specified.
//
// From indicates where the Param value may come from. Examples: `From("cxt:foo")`
// gets the value from the value of the key 'foo' in the Context.
type Param struct {
Name string
DefaultValue interface{}
From string
}