2018-03-05 12:44:16 +01:00
|
|
|
// Define the registry
|
|
|
|
|
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-04-23 21:44:44 +02:00
|
|
|
"sort"
|
2018-03-05 12:44:16 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Func defines a type for a remote control function
|
2019-06-17 10:34:30 +02:00
|
|
|
type Func func(ctx context.Context, in Params) (out Params, err error)
|
2018-03-05 12:44:16 +01:00
|
|
|
|
|
|
|
// Call defines info about a remote control function and is used in
|
|
|
|
// the Add function to create new entry points.
|
|
|
|
type Call struct {
|
2020-07-27 20:01:35 +02:00
|
|
|
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
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-10-27 19:29:20 +02:00
|
|
|
func (r *Registry) Add(call Call) {
|
2018-03-05 12:44:16 +01:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
call.Path = strings.Trim(call.Path, "/")
|
|
|
|
call.Help = strings.TrimSpace(call.Help)
|
2021-05-20 14:08:01 +02:00
|
|
|
// fs.Debugf(nil, "Adding path %q to remote control registry", call.Path) // disabled to make initialization less verbose
|
2018-03-05 12:44:16 +01:00
|
|
|
r.call[call.Path] = &call
|
|
|
|
}
|
|
|
|
|
2018-10-27 19:29:20 +02:00
|
|
|
// Get a Call from a path or nil
|
|
|
|
func (r *Registry) Get(path string) *Call {
|
2018-03-05 12:44:16 +01:00
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
return r.call[path]
|
|
|
|
}
|
|
|
|
|
2018-10-27 19:29:20 +02:00
|
|
|
// List of all calls in alphabetical order
|
|
|
|
func (r *Registry) List() (out []*Call) {
|
2018-03-05 12:44:16 +01:00
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
2018-04-23 21:44:44 +02:00
|
|
|
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])
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-10-27 19:29:20 +02:00
|
|
|
// Calls is the global registry of Call objects
|
|
|
|
var Calls = NewRegistry()
|
2018-03-05 12:44:16 +01:00
|
|
|
|
|
|
|
// Add a function to the global registry
|
|
|
|
func Add(call Call) {
|
2018-10-27 19:29:20 +02:00
|
|
|
Calls.Add(call)
|
2018-03-05 12:44:16 +01:00
|
|
|
}
|