/
completions.go
354 lines (281 loc) · 10.7 KB
/
completions.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"strings"
)
import (
"encoding/json"
"github.com/oracle/coherence-cli/pkg/config"
)
// contains functions for doing custom command completion.
var emptySlice = make([]string, 0)
// completionAllClusters provides a completion function to return all clusters.
func completionAllClusters(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return getAllClusters(false), cobra.ShellCompDirectiveNoFileComp
}
// completionAllManualClusters provides a completion function to return all clusters that were manually created.
func completionAllManualClusters(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return getAllClusters(true), cobra.ShellCompDirectiveNoFileComp
}
// completionAllProfiles provides a completion function to return all profiles.
func completionAllProfiles(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
profiles := make([]string, 0)
for _, p := range Config.Profiles {
profiles = append(profiles, p.Name)
}
return profiles, cobra.ShellCompDirectiveNoFileComp
}
// completionCaches provides a completion function to return all cache names.
func completionCaches(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var (
cacheSummaries config.CacheSummaries
caches = make([]string, 0)
)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
data, err := dataFetcher.GetCachesSummaryJSONAllServices()
if err != nil || len(data) == 0 || string(data) == "{}" {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
err = json.Unmarshal(data, &cacheSummaries)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range cacheSummaries.Caches {
caches = append(caches, v.CacheName)
}
return escapeValues(caches, cobra.ShellCompDirectiveNoFileComp)
}
// completionViewCaches provides a completion function to return all cache names.
func completionViewCaches(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var (
cacheSummaries config.ViewCacheSummaries
caches = make([]string, 0)
)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
data, err := dataFetcher.GetViewsSummaryJSONAllServices()
if err != nil || len(data) == 0 || string(data) == "{}" {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
err = json.Unmarshal(data, &cacheSummaries)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range cacheSummaries.ViewCaches {
caches = append(caches, v.ViewName)
}
return escapeValues(caches, cobra.ShellCompDirectiveNoFileComp)
}
// completionTopics provides a completion function to return all topic names.
func completionTopics(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var topics = make([]string, 0)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
// get the topics and services
topicsDetails, err := getTopics(dataFetcher, serviceName)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range topicsDetails.Details {
topics = append(topics, v.TopicName)
}
return escapeValues(topics, cobra.ShellCompDirectiveNoFileComp)
}
// completionService provides a completion function to return all services in a cluster.
func completionService(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var results = make([]string, 0)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
var servicesSummary = config.ServicesSummaries{}
servicesResult, err := dataFetcher.GetServiceDetailsJSON()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
err = json.Unmarshal(servicesResult, &servicesSummary)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range servicesSummary.Services {
results = append(results, v.ServiceName)
}
return escapeValues(results, cobra.ShellCompDirectiveNoFileComp)
}
// completionPersistenceService provides a completion function to return all persistence services in a cluster.
func completionPersistenceService(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
servicesResult, err := GetPersistenceServices(dataFetcher)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
return escapeValues(servicesResult, cobra.ShellCompDirectiveNoFileComp)
}
// completionDistributedService provides a completion function to return all distributed services in a cluster.
func completionDistributedService(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
servicesResult, err := GetDistributedServices(dataFetcher)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
return escapeValues(servicesResult, cobra.ShellCompDirectiveNoFileComp)
}
// completionFederatedService provides a completion function to return all federated services in a cluster.
func completionFederatedService(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
federatedServices, err := GetFederatedServices(dataFetcher)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
return escapeValues(federatedServices, cobra.ShellCompDirectiveNoFileComp)
}
// completionNodeID provides a completion function to return all node ids in a cluster.
func completionNodeID(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
nodeIDArray, err := GetNodeIds(dataFetcher)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
return nodeIDArray, cobra.ShellCompDirectiveNoFileComp
}
// completionExecutors provides a completion function to return all executor names in a cluster.
func completionExecutors(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var results = make([]string, 0)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
executors, err := getExecutorDetails(dataFetcher, true)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range executors.Executors {
results = append(results, v.Name)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// completionExecutors provides a completion function to return all machines in a cluster.
func completionMachines(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var results = make([]string, 0)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
machinesMap, err := GetMachineList(dataFetcher)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for k := range machinesMap {
results = append(results, k)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// completionHTTPSessions provides a completion function to return all http sessions in a cluster.
func completionHTTPSessions(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
var (
results = make([]string, 0)
httpSessions = config.HTTPSessionSummaries{}
)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
sessions, err := dataFetcher.GetHTTPSessionDetailsJSON()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
if len(sessions) > 0 {
err = json.Unmarshal(sessions, &httpSessions)
}
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range httpSessions.HTTPSessions {
results = append(results, v.AppID)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// completionHTTPServers provides a completion function to return all HTTP servers in a cluster.
func completionHTTPServers(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return getProxies(httpString)
}
// completionProxies provides a completion function to return all proxies in a cluster.
func completionProxies(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return getProxies(tcpString)
}
func getProxies(protocol string) ([]string, cobra.ShellCompDirective) {
var (
proxiesSummary = config.ProxiesSummary{}
proxies = make([]string, 0)
)
_, dataFetcher, err := GetConnectionAndDataFetcher()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
proxyResults, err := dataFetcher.GetProxySummaryJSON()
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
err = json.Unmarshal(proxyResults, &proxiesSummary)
if err != nil {
return emptySlice, cobra.ShellCompDirectiveNoFileComp
}
for _, v := range proxiesSummary.Proxies {
if v.Protocol == protocol {
proxies = append(proxies, v.ServiceName)
}
}
return proxies, cobra.ShellCompDirectiveNoFileComp
}
// getAllClusters
func getAllClusters(manualOnly bool) []string {
var clusters = make([]string, 0)
for _, v := range Config.Clusters {
if !manualOnly || (manualOnly && v.ManuallyCreated) {
clusters = append(clusters, v.Name)
}
}
return clusters
}
// escapeValues escapes any values so that if we have values with $ they are not
// interpreted by the shell.
func escapeValues(values []string, directive cobra.ShellCompDirective) ([]string, cobra.ShellCompDirective) {
if isWindows() {
return values, directive
}
for i, v := range values {
if strings.Contains(v, ":") {
// must be a service name so enclose in double quotes and escape the $
values[i] = fmt.Sprintf("\\\"%s\\\"", strings.ReplaceAll(v, "$", "\\$"))
} else if strings.Contains(v, "$") {
// must just be a topic or service name so enclose in single quotes
values[i] = fmt.Sprintf("\\'%s\\'", strings.ReplaceAll(v, "$", "\\$"))
}
}
return values, directive
}