github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/actors/plugin_installer/plugin_installer_with_repo.go (about)

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