github.com/arunkumar7540/cli@v6.45.0+incompatible/command/plugin/plugins_command.go (about)

     1  package plugin
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/actor/pluginaction"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/plugin/shared"
     9  	"code.cloudfoundry.org/cli/command/translatableerror"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  )
    13  
    14  //go:generate counterfeiter . PluginsActor
    15  
    16  type PluginsActor interface {
    17  	GetOutdatedPlugins() ([]pluginaction.OutdatedPlugin, error)
    18  }
    19  
    20  type PluginsCommand struct {
    21  	Checksum          bool        `long:"checksum" description:"Compute and show the sha1 value of the plugin binary file"`
    22  	Outdated          bool        `long:"outdated" description:"Search the plugin repositories for new versions of installed plugins"`
    23  	usage             interface{} `usage:"CF_NAME plugins [--checksum | --outdated]"`
    24  	relatedCommands   interface{} `related_commands:"install-plugin, repo-plugins, uninstall-plugin"`
    25  	SkipSSLValidation bool        `short:"k" hidden:"true" description:"Skip SSL certificate validation"`
    26  	UI                command.UI
    27  	Config            command.Config
    28  	Actor             PluginsActor
    29  }
    30  
    31  func (cmd *PluginsCommand) Setup(config command.Config, ui command.UI) error {
    32  	cmd.UI = ui
    33  	cmd.Config = config
    34  	pluginClient := shared.NewClient(config, ui, cmd.SkipSSLValidation)
    35  	cmd.Actor = pluginaction.NewActor(config, pluginClient)
    36  	return nil
    37  }
    38  
    39  func (cmd PluginsCommand) Execute([]string) error {
    40  	switch {
    41  	case cmd.Outdated:
    42  		return cmd.displayOutdatedPlugins()
    43  	case cmd.Checksum:
    44  		return cmd.displayPluginChecksums(cmd.Config.Plugins())
    45  	default:
    46  		return cmd.displayPluginCommands(cmd.Config.Plugins())
    47  	}
    48  }
    49  
    50  func (cmd PluginsCommand) displayPluginChecksums(plugins []configv3.Plugin) error {
    51  	cmd.UI.DisplayText("Computing sha1 for installed plugins, this may take a while...")
    52  	table := [][]string{{"plugin", "version", "sha1"}}
    53  	for _, plugin := range plugins {
    54  		table = append(table, []string{plugin.Name, plugin.Version.String(), plugin.CalculateSHA1()})
    55  	}
    56  
    57  	cmd.UI.DisplayNewline()
    58  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
    59  	return nil
    60  }
    61  
    62  func (cmd PluginsCommand) displayOutdatedPlugins() error {
    63  	repos := cmd.Config.PluginRepositories()
    64  	if len(repos) == 0 {
    65  		return translatableerror.NoPluginRepositoriesError{}
    66  	}
    67  	repoNames := make([]string, len(repos))
    68  	for i := range repos {
    69  		repoNames[i] = repos[i].Name
    70  	}
    71  	cmd.UI.DisplayTextWithFlavor("Searching {{.RepoNames}} for newer versions of installed plugins...",
    72  		map[string]interface{}{
    73  			"RepoNames": strings.Join(repoNames, ", "),
    74  		})
    75  
    76  	outdatedPlugins, err := cmd.Actor.GetOutdatedPlugins()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	table := [][]string{{"plugin", "version", "latest version"}}
    82  
    83  	for _, plugin := range outdatedPlugins {
    84  		table = append(table, []string{plugin.Name, plugin.CurrentVersion, plugin.LatestVersion})
    85  	}
    86  
    87  	cmd.UI.DisplayNewline()
    88  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
    89  
    90  	cmd.UI.DisplayNewline()
    91  	cmd.UI.DisplayText("Use '{{.BinaryName}} install-plugin' to update a plugin to the latest version.", map[string]interface{}{
    92  		"BinaryName": cmd.Config.BinaryName(),
    93  	})
    94  
    95  	return nil
    96  }
    97  
    98  func (cmd PluginsCommand) displayPluginCommands(plugins []configv3.Plugin) error {
    99  	cmd.UI.DisplayText("Listing installed plugins...")
   100  	table := [][]string{{"plugin", "version", "command name", "command help"}}
   101  	for _, plugin := range plugins {
   102  		for _, command := range plugin.PluginCommands() {
   103  			table = append(table, []string{plugin.Name, plugin.Version.String(), command.CommandName(), command.HelpText})
   104  		}
   105  	}
   106  	cmd.UI.DisplayNewline()
   107  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   108  
   109  	cmd.UI.DisplayNewline()
   110  	cmd.UI.DisplayText("Use '{{.BinaryName}} repo-plugins' to list plugins in registered repos available to install.",
   111  		map[string]interface{}{
   112  			"BinaryName": cmd.Config.BinaryName(),
   113  		})
   114  
   115  	return nil
   116  }