-
Notifications
You must be signed in to change notification settings - Fork 20
/
commandMap.go
131 lines (107 loc) · 2.71 KB
/
commandMap.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"errors"
"os"
"strings"
"sync"
"time"
"github.com/dreadl0ck/readline"
"github.com/mgutz/ansi"
)
type commandMap struct {
items map[string]*command
sync.RWMutex
}
func newCommandMap() *commandMap {
return &commandMap{
items: make(map[string]*command, 0),
}
}
// flush commandMap
// and remove all command completions
func (cm *commandMap) flush() {
cm.Lock()
defer cm.Unlock()
var (
names []string
indices []int
newChildren []readline.PrefixCompleterInterface
)
for name := range cm.items {
names = append(names, name)
}
// reset cmdMap
cmdMap.items = make(map[string]*command, 0)
// remove all command completions
completer.Lock()
for i, comp := range completer.Children {
for _, n := range names {
if strings.TrimSpace(string(comp.GetName())) == n {
indices = append(indices, i)
}
}
}
var addChild = true
for i, comp := range completer.Children {
for _, index := range indices {
if i == index {
addChild = false
}
}
if addChild {
newChildren = append(newChildren, comp)
}
// reset
addChild = true
}
completer.Children = newChildren
completer.Unlock()
}
func (cm *commandMap) length() int {
cm.Lock()
defer cm.Unlock()
return len(cm.items)
}
func (cm *commandMap) init(start time.Time) {
cLog := Log.WithField("prefix", "cmdMap.init")
conf.Lock()
if conf.fields.Debug {
// only print info when using the interactive shell
if len(os.Args) == 1 {
if len(cm.items) == 1 {
l.Println(cp.Text+"initialized "+cp.Prompt, "1", cp.Text+" command in: "+cp.Prompt, time.Now().Sub(start), cp.Reset+"\n")
} else {
l.Println(cp.Text+"initialized "+cp.Prompt, len(cmdMap.items), cp.Text+" commands in: "+cp.Prompt, time.Now().Sub(start), cp.Reset+"\n")
}
}
}
conf.Unlock()
cm.Lock()
defer cm.Unlock()
// check if custom command conflicts with builtin name
for _, name := range builtins {
if _, ok := cm.items[name]; ok {
cLog.Error("command ", name, " conflicts with a builtin command. Please choose a different name.")
}
}
var commandCompletions []readline.PrefixCompleterInterface
for _, c := range cm.items {
commandCompletions = append(commandCompletions, readline.PcItem(c.name))
}
// add all commands to the completer for the help page
for _, c := range completer.Children {
if string(c.GetName()) == "help " {
c.SetChildren(commandCompletions)
}
}
}
// retrieve a command instance by passing a command string
func (cm *commandMap) getCommand(name string) (*command, error) {
cmdMap.Lock()
defer cmdMap.Unlock()
if cmd, ok := cmdMap.items[name]; ok {
// return command instance
return cmd, nil
}
return nil, errors.New(ErrUnknownCommand.Error() + ": " + ansi.Red + name + cp.Text)
}