/
uiPlugin.go
221 lines (197 loc) · 7.1 KB
/
uiPlugin.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
package completion
import (
"context"
"fmt"
"strings"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ydata"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
// WithUIPlugin UI extension completion
func WithUIPlugin(flagName string, clientFunc func() (*c8y.Client, error)) Option {
return func(cmd *cobra.Command) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := clientFunc()
if err != nil {
return []string{err.Error()}, cobra.ShellCompDirectiveDefault
}
hasVersions := true
items, _, err := client.Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.ApplicationOptions{
Type: c8y.ApplicationTypeHosted,
HasVersions: &hasVersions,
PaginationOptions: *c8y.NewPaginationOptions(2000),
},
)
if err != nil {
values := []string{fmt.Sprintf("error. %s", err)}
return values, cobra.ShellCompDirectiveError
}
values := []string{}
pattern := "*" + toComplete + "*"
for _, item := range items.Applications {
if toComplete == "" || MatchString(pattern, item.Name) || MatchString(pattern, item.ID) {
values = append(values, fmt.Sprintf("%s\t%s | id: %s", item.Name, item.Type, item.ID))
}
}
// Lookup shared extensions
// Shared extensions
items, _, err = client.Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.ApplicationOptions{
Type: c8y.ApplicationTypeHosted,
HasVersions: &hasVersions,
Availability: c8y.ApplicationAvailabilityShared,
PaginationOptions: *c8y.NewPaginationOptions(2000),
},
)
if err != nil {
values := []string{fmt.Sprintf("error. %s", err)}
return values, cobra.ShellCompDirectiveError
}
for _, item := range items.Applications {
if toComplete == "" || MatchString(pattern, item.Name) || MatchString(pattern, item.ID) {
values = append(values, fmt.Sprintf("%s\t%s | id: %s", item.Name, item.Type, item.ID))
}
}
return values, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
}
func formatUIPluginValues(items *c8y.ApplicationCollection, toComplete string) []string {
values := []string{}
pattern := "*" + toComplete + "*"
withVersions := strings.Contains(toComplete, "@")
for i, app := range items.Applications {
if withVersions {
// Show name@version
for _, appVersion := range app.ApplicationVersions {
value := fmt.Sprintf("%s@%s", app.Name, appVersion.Version)
if toComplete == "" || MatchString(pattern, value) || MatchString(pattern, app.ID) {
tags := strings.Join(appVersion.Tags, ",")
if tags == "" {
tags = "-"
}
values = append(values, fmt.Sprintf("%s\t%s | id: %s, version: %s, tags: [%s]", value, app.Type, app.ID, appVersion.Version, tags))
}
}
} else {
// Show just names
if toComplete == "" || MatchString(pattern, app.Name) || MatchString(pattern, app.ID) {
// tags := strings.Join(appVersion.Tags, ",")
// if tags == "" {
// tags = "-"
// }
description := items.Items[i].Get("manifest.description").String()
if description == "" {
description = "<no description>"
}
totalVersions := len(app.ApplicationVersions)
values = append(values, fmt.Sprintf("%s\t%s | id: %s, versions: %d, desc: %s", app.Name, app.Type, app.ID, totalVersions, description))
}
}
}
return values
}
// WithUIPluginWithVersions UI plugin with version completion
// Values are returned in the format of name@version
func WithUIPluginWithVersions(flagName string, clientFunc func() (*c8y.Client, error)) Option {
return func(cmd *cobra.Command) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := clientFunc()
if err != nil {
return []string{err.Error()}, cobra.ShellCompDirectiveDefault
}
values := []string{}
hasVersions := true
// Extensions in current tenant
items, _, err := client.Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.ApplicationOptions{
Type: c8y.ApplicationTypeHosted,
HasVersions: &hasVersions,
PaginationOptions: *c8y.NewPaginationOptions(2000),
},
)
if err != nil {
values := []string{fmt.Sprintf("error. %s", err)}
return values, cobra.ShellCompDirectiveError
}
values = append(values, formatUIPluginValues(items, toComplete)...)
// Shared extensions
items, _, err = client.Application.GetApplications(
c8y.WithDisabledDryRunContext(context.Background()),
&c8y.ApplicationOptions{
Type: c8y.ApplicationTypeHosted,
HasVersions: &hasVersions,
Availability: c8y.ApplicationAvailabilityShared,
PaginationOptions: *c8y.NewPaginationOptions(2000),
},
)
if err != nil {
values := []string{fmt.Sprintf("error. %s", err)}
return values, cobra.ShellCompDirectiveError
}
values = append(values, formatUIPluginValues(items, toComplete)...)
return values, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
}
// Complete UI extension versions
func WithUIPluginVersion(flagVersion string, flagExtension string, clientFunc func() (*c8y.Client, error)) Option {
return func(cmd *cobra.Command) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc(flagVersion, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := clientFunc()
if err != nil {
return []string{err.Error()}, cobra.ShellCompDirectiveDefault
}
values := []string{}
// Lookup by name
names, err := GetFlagStringValues(cmd, flagExtension)
name := ""
if len(names) > 0 {
name = names[0]
}
extensionID := ""
if err == nil && name != "" {
if c8ydata.IsID(name) {
extensionID = name
} else {
// Lookup by name
col, _, err := client.UIExtension.GetExtensions(context.Background(), &c8y.ExtensionOptions{
Name: name,
HasVersions: true,
PaginationOptions: *c8y.NewPaginationOptions(100),
})
if err != nil {
return values, cobra.ShellCompDirectiveNoFileComp
}
if len(col.Applications) > 0 {
extensionID = col.Applications[0].ID
}
}
}
if extensionID == "" {
return values, cobra.ShellCompDirectiveNoFileComp
}
// Get versions
col, _, err := client.ApplicationVersions.GetVersions(context.Background(), extensionID, &c8y.ApplicationVersionsOptions{
PaginationOptions: *c8y.NewPaginationOptions(100),
})
if err != nil {
return values, cobra.ShellCompDirectiveNoFileComp
}
pattern := "*" + toComplete + "*"
for _, item := range col.Versions {
if toComplete == "" || MatchString(pattern, item.Version) || MatchString(pattern, item.BinaryID) {
values = append(values, fmt.Sprintf("%s\t%s | id: %s", item.Version, strings.Join(item.Tags, ","), item.BinaryID))
}
}
return values, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
}