-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommands.go
56 lines (49 loc) · 1.36 KB
/
commands.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
package cookoo
// LogMessage prints a message to the log.
//
// Params
//
// - msg: The message to print
// - level: The log level (default: "info")
func LogMessage(cxt Context, params *Params) (interface{}, Interrupt) {
msg := params.Get("msg", "tick")
level := params.Get("level", "info").(string)
cxt.Log(level, msg)
return nil, nil
}
// AddToContext adds all of the param name/value pairs into the context.
//
// Params
//
// - Any params will be added into the context.
func AddToContext(cxt Context, params *Params) (interface{}, Interrupt) {
p := params.AsMap()
for k, v := range p {
cxt.Put(k, v)
}
return true, nil
}
// ForwardTo forwards to the given route name.
//
// To prevent possible loops or problematic re-routes, use ignoreRoutes.
//
// Params
//
// - route: The route to forward to. This is required.
// - ignoreRoutes: Route names that should be ignored (generate recoverable errors).
func ForwardTo(cxt Context, params *Params) (interface{}, Interrupt) {
ok, _ := params.Requires("route")
if !ok {
return nil, &FatalError{"Expected a 'route'"}
}
route := params.Get("route", "default").(string)
stoplist := params.Get("ignoreRoutes", []string{}).([]string)
if len(stoplist) > 0 {
for _, stop := range stoplist {
if stop == route {
return nil, &RecoverableError{"Ignored route " + route}
}
}
}
return nil, &Reroute{route}
}