/
list.go
252 lines (230 loc) · 7.91 KB
/
list.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
package list
import (
"fmt"
"os"
"reflect"
"strings"
transform2 "github.com/konveyor/crane-lib/transform"
"github.com/konveyor/crane/internal/flags"
"github.com/konveyor/crane/internal/plugin"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Options struct {
// Two GlobalFlags struct fields are needed
// 1. cobraGlobalFlags for explicit CLI args parsed by cobra
// 2. globalFlags for the args merged with values from the viper config file
cobraGlobalFlags *flags.GlobalFlags
globalFlags *flags.GlobalFlags
// Two Flags struct fields are needed
// 1. cobraFlags for explicit CLI args parsed by cobra
// 2. Flags for the args merged with values from the viper config file
cobraFlags Flags
Flags
}
type Flags struct {
Repo string `mapstructure:"repo"`
Installed bool `mapstructure:"installed"`
PluginDir string `mapstructure:"plugin-dir"`
Params bool `mapstructure:"params"`
Name string `mapstructure:"name"`
Versions bool `mapstructure:"versions"`
}
type AvailablePlugins struct {
Name string
ShortDescription string
Description string
Versions []string
}
func (o *Options) Complete(c *cobra.Command, args []string) error {
// TODO: @jgabani
return nil
}
func (o *Options) Validate() error {
// TODO: @jgabani
return nil
}
func (o *Options) Run() error {
return o.run()
}
func NewListCommand(f *flags.GlobalFlags) *cobra.Command {
o := &Options{
globalFlags: f,
}
cmd := &cobra.Command{
Use: "list",
Short: "Lists all the available plugins",
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(c, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}
return nil
},
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
viper.Unmarshal(&o.Flags)
viper.Unmarshal(&o.globalFlags)
},
}
addFlagsForOptions(&o.cobraFlags, cmd)
return cmd
}
func addFlagsForOptions(o *Flags, cmd *cobra.Command) {
// TODO: display installed plugin information
cmd.Flags().BoolVarP(&o.Installed, "installed", "", false, "Flag to list installed plugins.")
cmd.Flags().BoolVarP(&o.Params, "params", "", false, "If passed, returns with metadata information for all the version of specific plugin. This flag is to be used with \"--name\" flag. Takes precedence over \"--versions\" if both passed.")
cmd.Flags().StringVarP(&o.Name, "name", "", "", "To be used with \"--params\" or \"--versions\" flag to specify the plugin for which additional information is needed. In case of conflict, command fails and asks for a specific repository information.")
cmd.Flags().BoolVarP(&o.Versions, "versions", "", false, "If passed, returns with all the versions available for a plugin. This flag is to be used with \"--name\" flag.")
}
func (o *Options) run() error {
log := o.globalFlags.GetLogger()
if o.Installed {
// retrieve list of all the plugins that are installed within plugin dir
// TODO: differentiate between multiple repos
plugins, err := plugin.GetFilteredPlugins(o.PluginDir, []string{}, log)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Listing plugins from path - %s, along with default plugin", o.PluginDir))
printInstalledInformation(plugins)
return nil
}
// if the name flag is used then either get all the information of the plugin or get all the version of the plugin
if o.Name != "" && (o.Params || o.Versions) {
manifestMap, err := plugin.BuildManifestMap(log, o.Name, o.Repo)
if err != nil {
return nil
}
if len(manifestMap) == 0 {
log.Errorf("The plugin %s is not found", o.Name)
return nil
}
if o.Params {
// retrieve all the information for all the versions available for a specific plugin
for repo, pluginsMap := range manifestMap {
fmt.Printf("Listing from the repo %s\n", repo)
printParamsInformation(pluginsMap)
}
} else if o.Versions {
// retrieve all the available version of the plugin
for repo, pluginsMap := range manifestMap {
for name, pluginVersions := range pluginsMap {
fmt.Printf("Listing versions of plugin %s from the repo %s\n", name, repo)
for _, pluginVersion := range pluginVersions {
fmt.Printf("Version: %s\n", pluginVersion.Version)
}
}
}
} else {
log.Errorf(fmt.Sprintf("name flag must be used with wither params or versions"))
}
} else {
manifestMap, err := plugin.BuildManifestMap(log, "", o.Repo)
if err != nil {
return nil
}
if o.Name != "" {
log.Info(fmt.Sprintf("\"--name\" flag should be used with either \"--versions\" or \"--params\" flag to get more information about the plugin, example: \"crane plugin-manager --name %s --versions\" or \"crane plugin-manager --name %s --params\"\n", o.Name, o.Name))
} else if o.Params {
// retrieve all the information for all the versions available for a specific plugin
for repo, pluginsMap := range manifestMap {
fmt.Printf("Listing from the repo %s\n", repo)
printParamsInformation(pluginsMap)
}
} else {
for repo, pluginsMap := range manifestMap {
// output information
fmt.Printf("Listing from the repo %s\n", repo)
groupInformationForPlugins(pluginsMap)
}
}
}
return nil
}
//TODO: this can be merged with printParamsInformation
func printInstalledInformation(plugins []transform2.Plugin) {
for _, thisPlugin := range plugins {
printTable([][]string{
{"Name", thisPlugin.Metadata().Name},
{"Version", thisPlugin.Metadata().Version},
{"OptionalFields", getOptionalFields(thisPlugin.Metadata().OptionalFields)},
})
}
}
func groupInformationForPlugins(pluginsMap map[string][]plugin.PluginVersion) {
availablePlugin := map[string]AvailablePlugins{}
for _, pluginVersions := range pluginsMap {
for _, pluginVersion := range pluginVersions {
if _, ok := availablePlugin[pluginVersion.Name]; ok {
availablePlugin[pluginVersion.Name] = AvailablePlugins{Name: pluginVersion.Name, ShortDescription: pluginVersion.ShortDescription, Versions: append(availablePlugin[pluginVersion.Name].Versions, string(pluginVersion.Version))}
} else {
availablePlugin[pluginVersion.Name] = AvailablePlugins{
Name: pluginVersion.Name,
ShortDescription: pluginVersion.ShortDescription,
Versions: []string{string(pluginVersion.Version)},
}
}
}
}
printInformation(availablePlugin)
}
func printInformation(availablePlugins map[string]AvailablePlugins) {
for _, availablePlugin := range availablePlugins {
if availablePlugin.Name != "" {
printTable([][]string{
{"Name", availablePlugin.Name},
{"ShortDescription", availablePlugin.ShortDescription},
{"AvailableVersions", strings.Join(availablePlugin.Versions, ", ")},
})
}
}
}
func printParamsInformation(pluginsMap map[string][]plugin.PluginVersion) {
for _, pluginVersions := range pluginsMap {
for _, pluginVersion := range pluginVersions {
printTable([][]string{
{"Name", pluginVersion.Name},
{"ShortDescription", pluginVersion.ShortDescription},
{"Description", pluginVersion.Description},
{"AvailableVersions", string(pluginVersion.Version)},
{"OptionalFields", getOptionalFields(pluginVersion.OptionalFields)},
})
}
}
}
func getOptionalFields(fields []transform2.OptionalFields) string {
var retstr string
if len(fields) > 0 {
var strs []string
for _, field := range fields {
optionalField := reflect.ValueOf(&field).Elem()
typeOfT := optionalField.Type()
for i := 0; i < optionalField.NumField(); i++ {
f := optionalField.Field(i)
var prefix string
if i == 0 {
prefix = "- "
} else {
prefix = " "
}
strs = append(strs, fmt.Sprintf("%s%s: %v", prefix,
typeOfT.Field(i).Name, f.Interface()))
}
}
retstr = strings.Join(strs, "\n")
}
return retstr
}
func printTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.AppendBulk(data)
table.Render()
}