forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_command.go
116 lines (97 loc) · 3.82 KB
/
plugins_command.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
package plugin
import (
"strings"
"code.cloudfoundry.org/cli/actor/pluginaction"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/plugin/shared"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/ui"
)
//go:generate counterfeiter . PluginsActor
type PluginsActor interface {
GetOutdatedPlugins() ([]pluginaction.OutdatedPlugin, error)
}
type PluginsCommand struct {
Checksum bool `long:"checksum" description:"Compute and show the sha1 value of the plugin binary file"`
Outdated bool `long:"outdated" description:"Search the plugin repositories for new versions of installed plugins"`
usage interface{} `usage:"CF_NAME plugins [--checksum | --outdated]"`
relatedCommands interface{} `related_commands:"install-plugin, repo-plugins, uninstall-plugin"`
SkipSSLValidation bool `short:"k" hidden:"true" description:"Skip SSL certificate validation"`
UI command.UI
Config command.Config
Actor PluginsActor
}
func (cmd *PluginsCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
pluginClient := shared.NewClient(config, ui, cmd.SkipSSLValidation)
cmd.Actor = pluginaction.NewActor(config, pluginClient)
return nil
}
func (cmd PluginsCommand) Execute([]string) error {
switch {
case cmd.Outdated:
return cmd.displayOutdatedPlugins()
case cmd.Checksum:
return cmd.displayPluginChecksums(cmd.Config.Plugins())
default:
return cmd.displayPluginCommands(cmd.Config.Plugins())
}
}
func (cmd PluginsCommand) displayPluginChecksums(plugins []configv3.Plugin) error {
cmd.UI.DisplayText("Computing sha1 for installed plugins, this may take a while...")
table := [][]string{{"plugin", "version", "sha1"}}
for _, plugin := range plugins {
table = append(table, []string{plugin.Name, plugin.Version.String(), plugin.CalculateSHA1()})
}
cmd.UI.DisplayNewline()
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
return nil
}
func (cmd PluginsCommand) displayOutdatedPlugins() error {
repos := cmd.Config.PluginRepositories()
if len(repos) == 0 {
return translatableerror.NoPluginRepositoriesError{}
}
repoNames := make([]string, len(repos))
for i := range repos {
repoNames[i] = repos[i].Name
}
cmd.UI.DisplayTextWithFlavor("Searching {{.RepoNames}} for newer versions of installed plugins...",
map[string]interface{}{
"RepoNames": strings.Join(repoNames, ", "),
})
outdatedPlugins, err := cmd.Actor.GetOutdatedPlugins()
if err != nil {
return err
}
table := [][]string{{"plugin", "version", "latest version"}}
for _, plugin := range outdatedPlugins {
table = append(table, []string{plugin.Name, plugin.CurrentVersion, plugin.LatestVersion})
}
cmd.UI.DisplayNewline()
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Use '{{.BinaryName}} install-plugin' to update a plugin to the latest version.", map[string]interface{}{
"BinaryName": cmd.Config.BinaryName(),
})
return nil
}
func (cmd PluginsCommand) displayPluginCommands(plugins []configv3.Plugin) error {
cmd.UI.DisplayText("Listing installed plugins...")
table := [][]string{{"plugin", "version", "command name", "command help"}}
for _, plugin := range plugins {
for _, command := range plugin.PluginCommands() {
table = append(table, []string{plugin.Name, plugin.Version.String(), command.CommandName(), command.HelpText})
}
}
cmd.UI.DisplayNewline()
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Use '{{.BinaryName}} repo-plugins' to list plugins in registered repos available to install.",
map[string]interface{}{
"BinaryName": cmd.Config.BinaryName(),
})
return nil
}