forked from rclone/rclone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
78 lines (67 loc) · 2.02 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
// Define the registry
package rc
import (
"context"
"sort"
"strings"
"sync"
)
// Func defines a type for a remote control function
type Func func(ctx context.Context, in Params) (out Params, err error)
// Call defines info about a remote control function and is used in
// the Add function to create new entry points.
type Call struct {
Path string // path to activate this RC
Fn Func `json:"-"` // function to call
Title string // help for the function
AuthRequired bool // if set then this call requires authorisation to be set
Help string // multi-line markdown formatted help
NeedsRequest bool // if set then this call will be passed the original request object as _request
NeedsResponse bool // if set then this call will be passed the original response object as _response
}
// Registry holds the list of all the registered remote control functions
type Registry struct {
mu sync.RWMutex
call map[string]*Call
}
// NewRegistry makes a new registry for remote control functions
func NewRegistry() *Registry {
return &Registry{
call: make(map[string]*Call),
}
}
// Add a call to the registry
func (r *Registry) Add(call Call) {
r.mu.Lock()
defer r.mu.Unlock()
call.Path = strings.Trim(call.Path, "/")
call.Help = strings.TrimSpace(call.Help)
// fs.Debugf(nil, "Adding path %q to remote control registry", call.Path) // disabled to make initialization less verbose
r.call[call.Path] = &call
}
// Get a Call from a path or nil
func (r *Registry) Get(path string) *Call {
r.mu.RLock()
defer r.mu.RUnlock()
return r.call[path]
}
// List of all calls in alphabetical order
func (r *Registry) List() (out []*Call) {
r.mu.RLock()
defer r.mu.RUnlock()
var keys []string
for key := range r.call {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
out = append(out, r.call[key])
}
return out
}
// Calls is the global registry of Call objects
var Calls = NewRegistry()
// Add a function to the global registry
func Add(call Call) {
Calls.Add(call)
}