forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_plugins.go
117 lines (97 loc) · 3 KB
/
repo_plugins.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
package plugin_repo
import (
"strings"
"github.com/cloudfoundry/cli/cf/actors/plugin_repo"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/flag_helpers"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/models"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type RepoPlugins struct {
ui terminal.UI
config core_config.Reader
pluginRepo plugin_repo.PluginRepo
}
func NewRepoPlugins(ui terminal.UI, config core_config.Reader, pluginRepo plugin_repo.PluginRepo) RepoPlugins {
return RepoPlugins{
ui: ui,
config: config,
pluginRepo: pluginRepo,
}
}
func (cmd RepoPlugins) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: T("repo-plugins"),
Description: T("List all available plugins in all added repositories"),
Usage: T(`CF_NAME repo-plugins
EXAMPLE:
cf repo-plugins [-r REPO_NAME]
`),
Flags: []cli.Flag{
flag_helpers.NewStringFlag("r", T("Repo Name - List plugins from just this repository")),
},
}
}
func (cmd RepoPlugins) GetRequirements(_ requirements.Factory, c *cli.Context) (req []requirements.Requirement, err error) {
return
}
func (cmd RepoPlugins) Run(c *cli.Context) {
var repos []models.PluginRepo
repoName := c.String("r")
repos = cmd.config.PluginRepos()
if repoName == "" {
cmd.ui.Say(T("Getting plugins from all repositories ... "))
} else {
index := cmd.findRepoIndex(repoName)
if index != -1 {
cmd.ui.Say(T("Getting plugins from repository '") + repoName + "' ...")
repos = []models.PluginRepo{repos[index]}
} else {
cmd.ui.Failed(repoName + T(" does not exist as an available plugin repo."+"\nTip: use `add-plugin-repo` command to add repos."))
}
}
cmd.ui.Say("")
repoPlugins, repoError := cmd.pluginRepo.GetPlugins(repos)
cmd.printTable(repoPlugins)
cmd.printErrors(repoError)
}
func (cmd RepoPlugins) printTable(repoPlugins map[string][]clipr.Plugin) {
for k, plugins := range repoPlugins {
cmd.ui.Say(terminal.ColorizeBold(T("Repository: ")+k, 33))
table := cmd.ui.Table([]string{T("name"), T("version"), T("description")})
for _, p := range plugins {
table.Add(p.Name, p.Version, p.Description)
}
table.Print()
cmd.ui.Say("")
}
}
func (cmd RepoPlugins) printErrors(repoError []string) {
if len(repoError) > 0 {
cmd.ui.Say(terminal.ColorizeBold(T("Logged errors:"), 31))
for _, e := range repoError {
cmd.ui.Say(terminal.Colorize(e, 31))
}
cmd.ui.Say("")
}
}
func getListEndpoint(url string) string {
if strings.HasSuffix(url, "/") {
return url + "list"
}
return url + "/list"
}
func (cmd RepoPlugins) findRepoIndex(repoName string) int {
repos := cmd.config.PluginRepos()
for i, repo := range repos {
if strings.ToLower(repo.Name) == strings.ToLower(repoName) {
return i
}
}
return -1
}