github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli-plugins/manager/cobra.go (about)

     1  package manager
     2  
     3  import (
     4  	"github.com/docker/cli/cli/command"
     5  	"github.com/spf13/cobra"
     6  )
     7  
     8  const (
     9  	// CommandAnnotationPlugin is added to every stub command added by
    10  	// AddPluginCommandStubs with the value "true" and so can be
    11  	// used to distinguish plugin stubs from regular commands.
    12  	CommandAnnotationPlugin = "com.docker.cli.plugin"
    13  
    14  	// CommandAnnotationPluginVendor is added to every stub command
    15  	// added by AddPluginCommandStubs and contains the vendor of
    16  	// that plugin.
    17  	CommandAnnotationPluginVendor = "com.docker.cli.plugin.vendor"
    18  
    19  	// CommandAnnotationPluginVersion is added to every stub command
    20  	// added by AddPluginCommandStubs and contains the version of
    21  	// that plugin.
    22  	CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
    23  
    24  	// CommandAnnotationPluginInvalid is added to any stub command
    25  	// added by AddPluginCommandStubs for an invalid command (that
    26  	// is, one which failed it's candidate test) and contains the
    27  	// reason for the failure.
    28  	CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
    29  )
    30  
    31  // AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
    32  // plugin. The command stubs will have several annotations added, see
    33  // `CommandAnnotationPlugin*`.
    34  func AddPluginCommandStubs(dockerCli command.Cli, cmd *cobra.Command) error {
    35  	plugins, err := ListPlugins(dockerCli, cmd)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	for _, p := range plugins {
    40  		vendor := p.Vendor
    41  		if vendor == "" {
    42  			vendor = "unknown"
    43  		}
    44  		annotations := map[string]string{
    45  			CommandAnnotationPlugin:        "true",
    46  			CommandAnnotationPluginVendor:  vendor,
    47  			CommandAnnotationPluginVersion: p.Version,
    48  		}
    49  		if p.Err != nil {
    50  			annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
    51  		}
    52  		cmd.AddCommand(&cobra.Command{
    53  			Use:         p.Name,
    54  			Short:       p.ShortDescription,
    55  			Run:         func(_ *cobra.Command, _ []string) {},
    56  			Annotations: annotations,
    57  		})
    58  	}
    59  	return nil
    60  }