github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli-plugins/manager/plugin.go (about)

     1  package manager
     2  
     3  import (
     4  	"encoding/json"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var pluginNameRe = regexp.MustCompile("^[a-z][a-z0-9]*$")
    14  
    15  // Plugin represents a potential plugin with all it's metadata.
    16  type Plugin struct {
    17  	Metadata
    18  
    19  	Name string `json:",omitempty"`
    20  	Path string `json:",omitempty"`
    21  
    22  	// Err is non-nil if the plugin failed one of the candidate tests.
    23  	Err error `json:",omitempty"`
    24  
    25  	// ShadowedPaths contains the paths of any other plugins which this plugin takes precedence over.
    26  	ShadowedPaths []string `json:",omitempty"`
    27  }
    28  
    29  // newPlugin determines if the given candidate is valid and returns a
    30  // Plugin.  If the candidate fails one of the tests then `Plugin.Err`
    31  // is set, and is always a `pluginError`, but the `Plugin` is still
    32  // returned with no error. An error is only returned due to a
    33  // non-recoverable error.
    34  func newPlugin(c Candidate, rootcmd *cobra.Command) (Plugin, error) {
    35  	path := c.Path()
    36  	if path == "" {
    37  		return Plugin{}, errors.New("plugin candidate path cannot be empty")
    38  	}
    39  
    40  	// The candidate listing process should have skipped anything
    41  	// which would fail here, so there are all real errors.
    42  	fullname := filepath.Base(path)
    43  	if fullname == "." {
    44  		return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path)
    45  	}
    46  	var err error
    47  	if fullname, err = trimExeSuffix(fullname); err != nil {
    48  		return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path)
    49  	}
    50  	if !strings.HasPrefix(fullname, NamePrefix) {
    51  		return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix)
    52  	}
    53  
    54  	p := Plugin{
    55  		Name: strings.TrimPrefix(fullname, NamePrefix),
    56  		Path: path,
    57  	}
    58  
    59  	// Now apply the candidate tests, so these update p.Err.
    60  	if !pluginNameRe.MatchString(p.Name) {
    61  		p.Err = NewPluginError("plugin candidate %q did not match %q", p.Name, pluginNameRe.String())
    62  		return p, nil
    63  	}
    64  
    65  	if rootcmd != nil {
    66  		for _, cmd := range rootcmd.Commands() {
    67  			// Ignore conflicts with commands which are
    68  			// just plugin stubs (i.e. from a previous
    69  			// call to AddPluginCommandStubs).
    70  			if IsPluginCommand(cmd) {
    71  				continue
    72  			}
    73  			if cmd.Name() == p.Name {
    74  				p.Err = NewPluginError("plugin %q duplicates builtin command", p.Name)
    75  				return p, nil
    76  			}
    77  			if cmd.HasAlias(p.Name) {
    78  				p.Err = NewPluginError("plugin %q duplicates an alias of builtin command %q", p.Name, cmd.Name())
    79  				return p, nil
    80  			}
    81  		}
    82  	}
    83  
    84  	// We are supposed to check for relevant execute permissions here. Instead we rely on an attempt to execute.
    85  	meta, err := c.Metadata()
    86  	if err != nil {
    87  		p.Err = wrapAsPluginError(err, "failed to fetch metadata")
    88  		return p, nil
    89  	}
    90  
    91  	if err := json.Unmarshal(meta, &p.Metadata); err != nil {
    92  		p.Err = wrapAsPluginError(err, "invalid metadata")
    93  		return p, nil
    94  	}
    95  	if p.Metadata.SchemaVersion != "0.1.0" {
    96  		p.Err = NewPluginError("plugin SchemaVersion %q is not valid, must be 0.1.0", p.Metadata.SchemaVersion)
    97  		return p, nil
    98  	}
    99  	if p.Metadata.Vendor == "" {
   100  		p.Err = NewPluginError("plugin metadata does not define a vendor")
   101  		return p, nil
   102  	}
   103  	return p, nil
   104  }