-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.go
59 lines (47 loc) · 1.32 KB
/
tools.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
package tools
import (
"fmt"
"sort"
)
var (
toolMap = make(map[string]*Tool)
toolList = sortableToolList{}
)
// Register registers a new Tool. This function may only be called in init() functions.
func Register(tool *Tool) {
// register in lists
toolMap[tool.Info.Name] = tool
toolList = append(toolList, tool)
sort.Sort(toolList)
}
// Get returns the Tool with the given name.
func Get(name string) (*Tool, error) {
tool, ok := toolMap[name]
if !ok {
return nil, fmt.Errorf("Tool %s %w", name, ErrNotFound)
}
return tool, nil
}
// New returns a new instance of a Tool's Logic with the given name.
func New(name string) (ToolLogic, error) {
tool, err := Get(name)
if err != nil {
return nil, err
}
return tool.Factory(), nil
}
// AsMap returns all Tools in a map. The returned map must not be modified.
func AsMap() map[string]*Tool {
return toolMap
}
// AsList returns all Tools in a slice. The returned slice must not be modified.
func AsList() []*Tool {
return toolList
}
type sortableToolList []*Tool
// Len implements sort.Interface.
func (l sortableToolList) Len() int { return len(l) }
// Swap implements sort.Interface.
func (l sortableToolList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// Less implements sort.Interface.
func (l sortableToolList) Less(i, j int) bool { return l[i].Info.Name < l[j].Info.Name }