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

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