forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_installer_with_repo.go
84 lines (68 loc) · 2.52 KB
/
plugin_installer_with_repo.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
package plugininstaller
import (
"errors"
"strings"
"code.cloudfoundry.org/cli/cf/actors/pluginrepo"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/util"
clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/web"
)
type pluginInstallerWithRepo struct {
UI terminal.UI
PluginDownloader *PluginDownloader
DownloadFromPath downloadFromPath
RepoName string
Checksummer util.Sha1Checksum
PluginRepo pluginrepo.PluginRepo
GetPluginRepos pluginReposFetcher
}
func (installer *pluginInstallerWithRepo) Install(inputSourceFilepath string) string {
var outputSourceFilepath string
targetPluginName := strings.ToLower(inputSourceFilepath)
installer.UI.Say(T("Looking up '{{.filePath}}' from repository '{{.repoName}}'", map[string]interface{}{"filePath": inputSourceFilepath, "repoName": installer.RepoName}))
repoModel, err := installer.getRepoFromConfig(installer.RepoName)
if err != nil {
installer.UI.Failed(err.Error() + "\n" + T("Tip: use 'add-plugin-repo' to register the repo"))
}
pluginList, repoAry := installer.PluginRepo.GetPlugins([]models.PluginRepo{repoModel})
if len(repoAry) != 0 {
installer.UI.Failed(T("Error getting plugin metadata from repo: ") + repoAry[0])
}
found := false
sha1 := ""
for _, plugin := range findRepoCaseInsensity(pluginList, installer.RepoName) {
if strings.ToLower(plugin.Name) == targetPluginName {
found = true
outputSourceFilepath, sha1 = installer.PluginDownloader.downloadFromPlugin(plugin)
installer.Checksummer.SetFilePath(outputSourceFilepath)
if !installer.Checksummer.CheckSha1(sha1) {
installer.UI.Failed(T("Downloaded plugin binary's checksum does not match repo metadata"))
}
}
}
if !found {
installer.UI.Failed(inputSourceFilepath + T(" is not available in repo '") + installer.RepoName + "'")
}
return outputSourceFilepath
}
func (installer *pluginInstallerWithRepo) getRepoFromConfig(repoName string) (models.PluginRepo, error) {
targetRepo := strings.ToLower(repoName)
list := installer.GetPluginRepos()
for i, repo := range list {
if strings.ToLower(repo.Name) == targetRepo {
return list[i], nil
}
}
return models.PluginRepo{}, errors.New(repoName + T(" not found"))
}
func findRepoCaseInsensity(repoList map[string][]clipr.Plugin, repoName string) []clipr.Plugin {
target := strings.ToLower(repoName)
for k, repo := range repoList {
if strings.ToLower(k) == target {
return repo
}
}
return nil
}