github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/actors/plugininstaller/plugin_installer_with_repo.go (about)

     1  package plugininstaller
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	clipr "code.cloudfoundry.org/cli-plugin-repo/web"
     8  	"code.cloudfoundry.org/cli/cf/actors/pluginrepo"
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  	"code.cloudfoundry.org/cli/util"
    13  )
    14  
    15  type pluginInstallerWithRepo struct {
    16  	UI               terminal.UI
    17  	PluginDownloader *PluginDownloader
    18  	DownloadFromPath downloadFromPath
    19  	RepoName         string
    20  	Checksummer      util.Sha1Checksum
    21  	PluginRepo       pluginrepo.PluginRepo
    22  	GetPluginRepos   pluginReposFetcher
    23  }
    24  
    25  func (installer *pluginInstallerWithRepo) Install(inputSourceFilepath string) string {
    26  	var outputSourceFilepath string
    27  
    28  	targetPluginName := strings.ToLower(inputSourceFilepath)
    29  
    30  	installer.UI.Say(T("Looking up '{{.filePath}}' from repository '{{.repoName}}'", map[string]interface{}{"filePath": inputSourceFilepath, "repoName": installer.RepoName}))
    31  
    32  	repoModel, err := installer.getRepoFromConfig(installer.RepoName)
    33  	if err != nil {
    34  		installer.UI.Failed(err.Error() + "\n" + T("Tip: use 'add-plugin-repo' to register the repo"))
    35  	}
    36  
    37  	pluginList, repoAry := installer.PluginRepo.GetPlugins([]models.PluginRepo{repoModel})
    38  	if len(repoAry) != 0 {
    39  		installer.UI.Failed(T("Error getting plugin metadata from repo: ") + repoAry[0])
    40  	}
    41  
    42  	found := false
    43  	sha1 := ""
    44  	for _, plugin := range findRepoCaseInsensity(pluginList, installer.RepoName) {
    45  		if strings.ToLower(plugin.Name) == targetPluginName {
    46  			found = true
    47  			outputSourceFilepath, sha1 = installer.PluginDownloader.downloadFromPlugin(plugin)
    48  
    49  			installer.Checksummer.SetFilePath(outputSourceFilepath)
    50  			if !installer.Checksummer.CheckSha1(sha1) {
    51  				installer.UI.Failed(T("Downloaded plugin binary's checksum does not match repo metadata"))
    52  			}
    53  		}
    54  
    55  	}
    56  	if !found {
    57  		installer.UI.Failed(inputSourceFilepath + T(" is not available in repo '") + installer.RepoName + "'")
    58  	}
    59  
    60  	return outputSourceFilepath
    61  }
    62  
    63  func (installer *pluginInstallerWithRepo) getRepoFromConfig(repoName string) (models.PluginRepo, error) {
    64  	targetRepo := strings.ToLower(repoName)
    65  	list := installer.GetPluginRepos()
    66  
    67  	for i, repo := range list {
    68  		if strings.ToLower(repo.Name) == targetRepo {
    69  			return list[i], nil
    70  		}
    71  	}
    72  
    73  	return models.PluginRepo{}, errors.New(repoName + T(" not found"))
    74  }
    75  
    76  func findRepoCaseInsensity(repoList map[string][]clipr.Plugin, repoName string) []clipr.Plugin {
    77  	target := strings.ToLower(repoName)
    78  	for k, repo := range repoList {
    79  		if strings.ToLower(k) == target {
    80  			return repo
    81  		}
    82  	}
    83  	return nil
    84  }