-
Notifications
You must be signed in to change notification settings - Fork 929
/
repo_plugins.go
122 lines (102 loc) · 3.22 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
118
119
120
121
122
package pluginrepo
import (
"errors"
"strings"
"github.com/cloudfoundry/cli/cf/actors/pluginrepo"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/flags"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/web"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type RepoPlugins struct {
ui terminal.UI
config coreconfig.Reader
pluginRepo pluginrepo.PluginRepo
}
func init() {
commandregistry.Register(&RepoPlugins{})
}
func (cmd *RepoPlugins) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["r"] = &flags.StringFlag{ShortName: "r", Usage: T("Name of a registered repository")}
return commandregistry.CommandMetadata{
Name: T("repo-plugins"),
Description: T("List all available plugins in specified repository or in all added repositories"),
Usage: []string{
T(`CF_NAME repo-plugins [-r REPO_NAME]`),
},
Examples: []string{
"CF_NAME repo-plugins -r PrivateRepo",
},
Flags: fs,
}
}
func (cmd *RepoPlugins) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
reqs := []requirements.Requirement{}
return reqs
}
func (cmd *RepoPlugins) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.pluginRepo = deps.PluginRepo
return cmd
}
func (cmd *RepoPlugins) Execute(c flags.FlagContext) error {
var repos []models.PluginRepo
repoName := c.String("r")
repos = cmd.config.PluginRepos()
for i := range repos {
if repos[i].URL == "http://plugins.cloudfoundry.org" {
repos[i].URL = "https://plugins.cloudfoundry.org"
}
}
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 {
return errors.New(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)
return nil
}
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 (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
}