github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/pluginrepo/repo_plugins.go (about)

     1  package pluginrepo
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/actors/pluginrepo"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  
    15  	clipr "github.com/cloudfoundry/cli-plugin-repo/web"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/i18n"
    18  )
    19  
    20  type RepoPlugins struct {
    21  	ui         terminal.UI
    22  	config     coreconfig.Reader
    23  	pluginRepo pluginrepo.PluginRepo
    24  }
    25  
    26  func init() {
    27  	commandregistry.Register(&RepoPlugins{})
    28  }
    29  
    30  func (cmd *RepoPlugins) MetaData() commandregistry.CommandMetadata {
    31  	fs := make(map[string]flags.FlagSet)
    32  	fs["r"] = &flags.StringFlag{ShortName: "r", Usage: T("Name of a registered repository")}
    33  
    34  	return commandregistry.CommandMetadata{
    35  		Name:        T("repo-plugins"),
    36  		Description: T("List all available plugins in specified repository or in all added repositories"),
    37  		Usage: []string{
    38  			T(`CF_NAME repo-plugins [-r REPO_NAME]`),
    39  		},
    40  		Examples: []string{
    41  			"CF_NAME repo-plugins -r PrivateRepo",
    42  		},
    43  		Flags: fs,
    44  	}
    45  }
    46  
    47  func (cmd *RepoPlugins) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    48  	reqs := []requirements.Requirement{}
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *RepoPlugins) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.pluginRepo = deps.PluginRepo
    56  	return cmd
    57  }
    58  
    59  func (cmd *RepoPlugins) Execute(c flags.FlagContext) error {
    60  	var repos []models.PluginRepo
    61  	repoName := c.String("r")
    62  
    63  	repos = cmd.config.PluginRepos()
    64  	for i := range repos {
    65  		if repos[i].URL == "http://plugins.cloudfoundry.org" {
    66  			repos[i].URL = "https://plugins.cloudfoundry.org"
    67  		}
    68  	}
    69  
    70  	if repoName == "" {
    71  		cmd.ui.Say(T("Getting plugins from all repositories ... "))
    72  	} else {
    73  		index := cmd.findRepoIndex(repoName)
    74  		if index != -1 {
    75  			cmd.ui.Say(T("Getting plugins from repository '") + repoName + "' ...")
    76  			repos = []models.PluginRepo{repos[index]}
    77  		} else {
    78  			return errors.New(repoName + T(" does not exist as an available plugin repo."+"\nTip: use `add-plugin-repo` command to add repos."))
    79  		}
    80  	}
    81  
    82  	cmd.ui.Say("")
    83  
    84  	repoPlugins, repoError := cmd.pluginRepo.GetPlugins(repos)
    85  
    86  	err := cmd.printTable(repoPlugins)
    87  
    88  	cmd.printErrors(repoError)
    89  
    90  	if err != nil {
    91  		return err
    92  	}
    93  	return nil
    94  }
    95  
    96  func (cmd RepoPlugins) printTable(repoPlugins map[string][]clipr.Plugin) error {
    97  	for k, plugins := range repoPlugins {
    98  		cmd.ui.Say(terminal.ColorizeBold(T("Repository: ")+k, 33))
    99  		table := cmd.ui.Table([]string{T("name"), T("version"), T("description")})
   100  		for _, p := range plugins {
   101  			table.Add(p.Name, p.Version, p.Description)
   102  		}
   103  		err := table.Print()
   104  		if err != nil {
   105  			return err
   106  		}
   107  		cmd.ui.Say("")
   108  	}
   109  	return nil
   110  }
   111  
   112  func (cmd RepoPlugins) printErrors(repoError []string) {
   113  	if len(repoError) > 0 {
   114  		cmd.ui.Say(terminal.ColorizeBold(T("Logged errors:"), 31))
   115  		for _, e := range repoError {
   116  			cmd.ui.Say(terminal.Colorize(e, 31))
   117  		}
   118  		cmd.ui.Say("")
   119  	}
   120  }
   121  
   122  func (cmd RepoPlugins) findRepoIndex(repoName string) int {
   123  	repos := cmd.config.PluginRepos()
   124  	for i, repo := range repos {
   125  		if strings.ToLower(repo.Name) == strings.ToLower(repoName) {
   126  			return i
   127  		}
   128  	}
   129  	return -1
   130  }