-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
storage.go
205 lines (174 loc) · 4.52 KB
/
storage.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package carapace
import (
"fmt"
"strings"
"sync"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// TODO storage needs better naming and structure
type entry struct {
flag ActionMap
flagMutex sync.RWMutex
positional []Action
positionalAny *Action
dash []Action
dashAny *Action
preinvoke func(cmd *cobra.Command, flag *pflag.Flag, action Action) Action
prerun func(cmd *cobra.Command, args []string)
bridged bool
initialized bool
}
type _storage map[*cobra.Command]*entry
var storageMutex sync.RWMutex
func (s _storage) get(cmd *cobra.Command) *entry {
storageMutex.RLock()
e, ok := s[cmd]
storageMutex.RUnlock()
if !ok {
storageMutex.Lock()
defer storageMutex.Unlock()
if e, ok = s[cmd]; !ok {
e = &entry{}
s[cmd] = e
}
}
return e
}
var bridgeMutex sync.Mutex
func (s _storage) bridge(cmd *cobra.Command) {
if entry := storage.get(cmd); !entry.bridged {
bridgeMutex.Lock()
defer bridgeMutex.Unlock()
if entry := storage.get(cmd); !entry.bridged {
cobra.OnInitialize(func() {
if !entry.initialized {
bridgeMutex.Lock()
defer bridgeMutex.Unlock()
if !entry.initialized {
registerValidArgsFunction(cmd)
registerFlagCompletion(cmd)
entry.initialized = true
}
}
})
entry.bridged = true
}
}
}
func (s _storage) hasFlag(cmd *cobra.Command, name string) bool {
if flag := cmd.LocalFlags().Lookup(name); flag == nil && cmd.HasParent() {
return s.hasFlag(cmd.Parent(), name)
} else {
entry := s.get(cmd)
entry.flagMutex.RLock()
defer entry.flagMutex.RUnlock()
_, ok := entry.flag[name]
return ok
}
}
func (s _storage) getFlag(cmd *cobra.Command, name string) Action {
if flag := cmd.LocalFlags().Lookup(name); flag == nil && cmd.HasParent() {
return s.getFlag(cmd.Parent(), name)
} else {
entry := s.get(cmd)
entry.flagMutex.RLock()
defer entry.flagMutex.RUnlock()
flagAction, ok := entry.flag[name]
if !ok {
if f, ok := cmd.GetFlagCompletionFunc(name); ok {
flagAction = ActionCobra(f)
}
}
a := s.preinvoke(cmd, flag, flagAction)
return ActionCallback(func(c Context) Action { // TODO verify order of execution is correct
invoked := a.Invoke(c)
if invoked.action.meta.Usage == "" {
invoked.action.meta.Usage = flag.Usage
}
return invoked.ToA()
})
}
}
func (s _storage) preRun(cmd *cobra.Command, args []string) {
if entry := s.get(cmd); entry.prerun != nil {
LOG.Printf("executing PreRun for %#v with args %#v", cmd.Name(), args)
entry.prerun(cmd, args)
}
}
func (s _storage) preinvoke(cmd *cobra.Command, flag *pflag.Flag, action Action) Action {
a := action
if entry := s.get(cmd); entry.preinvoke != nil {
a = ActionCallback(func(c Context) Action {
return entry.preinvoke(cmd, flag, action)
})
}
if cmd.HasParent() {
return s.preinvoke(cmd.Parent(), flag, a)
}
return a
}
func (s _storage) hasPositional(cmd *cobra.Command, index int) bool {
entry := s.get(cmd)
isDash := common.IsDash(cmd)
// TODO fallback to cobra defined completion if exists
switch {
case !isDash && len(entry.positional) > index:
return true
case !isDash:
return entry.positionalAny != nil
case len(entry.dash) > index:
return true
default:
return entry.dashAny != nil
}
}
func (s _storage) getPositional(cmd *cobra.Command, index int) Action {
entry := s.get(cmd)
isDash := common.IsDash(cmd)
var a Action
switch {
case !isDash && len(entry.positional) > index:
a = entry.positional[index]
case !isDash:
if entry.positionalAny != nil {
a = *entry.positionalAny
} else {
a = ActionCobra(cmd.ValidArgsFunction)
}
case len(entry.dash) > index:
a = entry.dash[index]
default:
if entry.dashAny != nil {
a = *entry.dashAny
} else {
a = ActionCobra(cmd.ValidArgsFunction)
}
}
a = s.preinvoke(cmd, nil, a)
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
if invoked.action.meta.Usage == "" && len(strings.Fields(cmd.Use)) > 1 {
invoked.action.meta.Usage = cmd.Use
}
return invoked.ToA()
})
}
func (s _storage) check() []string {
errors := make([]string, 0)
for cmd, entry := range s {
func() {
entry.flagMutex.RLock()
defer entry.flagMutex.RUnlock()
for name := range entry.flag {
if flag := cmd.LocalFlags().Lookup(name); flag == nil {
errors = append(errors, fmt.Sprintf("unknown flag for %s: %s\n", uid.Command(cmd), name))
}
}
}()
}
return errors
}
var storage = make(_storage)