github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/plugin/plugins.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/cloudfoundry/cli/flags"
    12  	"github.com/cloudfoundry/cli/flags/flag"
    13  	"github.com/cloudfoundry/cli/utils"
    14  )
    15  
    16  type Plugins struct {
    17  	ui     terminal.UI
    18  	config plugin_config.PluginConfiguration
    19  }
    20  
    21  func init() {
    22  	command_registry.Register(&Plugins{})
    23  }
    24  
    25  func (cmd *Plugins) MetaData() command_registry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	fs["checksum"] = &cliFlags.BoolFlag{Name: "checksum", Usage: T("Compute and show the sha1 value of the plugin binary file")}
    28  
    29  	return command_registry.CommandMetadata{
    30  		Name:        "plugins",
    31  		Description: T("list all available plugin commands"),
    32  		Usage:       T("CF_NAME plugins"),
    33  		Flags:       fs,
    34  	}
    35  }
    36  
    37  func (cmd *Plugins) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    38  	if len(fc.Args()) != 0 {
    39  		cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("plugins"))
    40  	}
    41  
    42  	return
    43  }
    44  
    45  func (cmd *Plugins) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    46  	cmd.ui = deps.Ui
    47  	cmd.config = deps.PluginConfig
    48  	return cmd
    49  }
    50  
    51  func (cmd *Plugins) Execute(c flags.FlagContext) {
    52  	var version string
    53  
    54  	cmd.ui.Say(T("Listing Installed Plugins..."))
    55  
    56  	plugins := cmd.config.Plugins()
    57  
    58  	var table terminal.Table
    59  	if c.Bool("checksum") {
    60  		cmd.ui.Say(T("Computing sha1 for installed plugins, this may take a while ..."))
    61  		table = terminal.NewTable(cmd.ui, []string{T("Plugin Name"), T("Version"), T("Command Name"), "sha1", T("Command Help")})
    62  	} else {
    63  		table = terminal.NewTable(cmd.ui, []string{T("Plugin Name"), T("Version"), T("Command Name"), T("Command Help")})
    64  	}
    65  
    66  	for pluginName, metadata := range plugins {
    67  		if metadata.Version.Major == 0 && metadata.Version.Minor == 0 && metadata.Version.Build == 0 {
    68  			version = "N/A"
    69  		} else {
    70  			version = fmt.Sprintf("%d.%d.%d", metadata.Version.Major, metadata.Version.Minor, metadata.Version.Build)
    71  		}
    72  
    73  		for _, command := range metadata.Commands {
    74  			args := []string{pluginName, version}
    75  
    76  			if command.Alias != "" {
    77  				args = append(args, command.Name+", "+command.Alias)
    78  			} else {
    79  				args = append(args, command.Name)
    80  			}
    81  
    82  			if c.Bool("checksum") {
    83  				checksum := utils.NewSha1Checksum(metadata.Location)
    84  				sha1, err := checksum.ComputeFileSha1()
    85  				if err != nil {
    86  					args = append(args, "n/a")
    87  				} else {
    88  					args = append(args, fmt.Sprintf("%x", sha1))
    89  				}
    90  			}
    91  
    92  			args = append(args, command.HelpText)
    93  			table.Add(args...)
    94  		}
    95  	}
    96  
    97  	cmd.ui.Ok()
    98  	cmd.ui.Say("")
    99  
   100  	table.Print()
   101  }