forked from platinasystems/goes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
65 lines (58 loc) · 1.41 KB
/
cache.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
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package goes
import (
"sort"
"sync"
)
type cache struct {
sync.Mutex
builtins map[string]func(...string) error
names []string
path []string
}
func (g *Goes) Builtins() map[string]func(...string) error {
if g.cache.builtins == nil || len(g.cache.builtins) == 0 {
g.cache.Lock()
defer g.cache.Unlock()
g.cache.builtins = map[string]func(...string) error{
"apropos": g.apropos,
"complete": g.complete,
"help": g.help,
"man": g.man,
"usage": g.usage,
}
}
return g.cache.builtins
}
func (g *Goes) Names() []string {
if len(g.cache.names) < len(g.ByName) {
g.cache.Lock()
defer g.cache.Unlock()
if got, want := len(g.cache.names), len(g.ByName); got < want {
if got == 0 {
g.cache.names = make([]string, 0, want)
} else {
g.cache.names = g.cache.names[:0]
}
for k := range g.ByName {
g.cache.names = append(g.cache.names, k)
}
sort.Strings(g.cache.names)
}
}
return g.cache.names
}
// set Path of sub-goes. e.g. "ip address"
func (g *Goes) Path() []string {
if g.parent != nil && len(g.cache.path) == 0 {
g.cache.Lock()
defer g.cache.Unlock()
for p := g; p != nil; p = p.parent {
g.cache.path = append([]string{p.String()},
g.cache.path...)
}
}
return g.cache.path
}