-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
83 lines (68 loc) · 1.47 KB
/
cmd.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
package route
import (
"sort"
"sync"
)
// CmdStore is a concurrent-safe store of Cmds.
type CmdStore struct {
ma map[string]Cmd
mu sync.RWMutex
}
// NewCmdStore creates a usable CmdStore.
func NewCmdStore() *CmdStore {
return &CmdStore{
ma: map[string]Cmd{},
mu: sync.RWMutex{},
}
}
// Add stores a Cmd, using its defined name.
func (c *CmdStore) Add(cmd Cmd) {
c.mu.Lock()
c.ma[cmd.Name] = cmd
c.mu.Unlock()
}
// Get fetches a Cmd with the given name.
func (c *CmdStore) Get(name string) (Cmd, bool) {
c.mu.RLock()
cmd, ok := c.ma[name]
c.mu.RUnlock()
return cmd, ok
}
// Del removes a Cmd with the given name.
func (c *CmdStore) Del(name string) {
c.mu.Lock()
delete(c.ma, name)
c.mu.Unlock()
}
// Cmd is a command.
type Cmd struct {
Name string
Desc string
Cat string
Func Func
Hide bool
Flags interface{}
}
// ByCat creates a map of sorted Cmd categories, and a sorted list of category names.
//
// If hidden is true, Cmds with the Hide flag will be included.
func (c *CmdStore) ByCat(hidden bool) (map[string][]Cmd, []string) {
cats := make(map[string][]Cmd)
c.mu.RLock()
for _, cmd := range c.ma {
if !cmd.Hide || hidden {
cats[cmd.Cat] = append(cats[cmd.Cat], cmd)
}
}
c.mu.RUnlock()
catNames := make([]string, 0, len(cats))
for name := range cats {
catNames = append(catNames, name)
cat := cats[name]
sort.Slice(cat, func(i, j int) bool {
return cat[i].Name < cat[j].Name
})
}
sort.Strings(catNames)
return cats, catNames
}