2018-12-11 09:03:47 -05:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pluginNameRe = regexp.MustCompile("^[a-z][a-z0-9]*$")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin represents a potential plugin with all it's metadata.
|
|
|
|
type Plugin struct {
|
|
|
|
Metadata
|
|
|
|
|
2018-12-19 09:49:20 -05:00
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Path string `json:",omitempty"`
|
2018-12-11 09:03:47 -05:00
|
|
|
|
|
|
|
// Err is non-nil if the plugin failed one of the candidate tests.
|
|
|
|
Err error `json:",omitempty"`
|
|
|
|
|
|
|
|
// ShadowedPaths contains the paths of any other plugins which this plugin takes precedence over.
|
|
|
|
ShadowedPaths []string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// newPlugin determines if the given candidate is valid and returns a
|
|
|
|
// Plugin. If the candidate fails one of the tests then `Plugin.Err`
|
2018-12-19 09:49:20 -05:00
|
|
|
// is set, and is always a `pluginError`, but the `Plugin` is still
|
|
|
|
// returned with no error. An error is only returned due to a
|
|
|
|
// non-recoverable error.
|
2019-05-21 12:50:12 -04:00
|
|
|
//
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:gocyclo
|
2020-10-02 08:19:34 -04:00
|
|
|
func newPlugin(c Candidate, rootcmd *cobra.Command) (Plugin, error) {
|
2018-12-11 09:03:47 -05:00
|
|
|
path := c.Path()
|
|
|
|
if path == "" {
|
|
|
|
return Plugin{}, errors.New("plugin candidate path cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The candidate listing process should have skipped anything
|
|
|
|
// which would fail here, so there are all real errors.
|
|
|
|
fullname := filepath.Base(path)
|
|
|
|
if fullname == "." {
|
|
|
|
return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path)
|
|
|
|
}
|
2019-01-14 12:53:19 -05:00
|
|
|
var err error
|
|
|
|
if fullname, err = trimExeSuffix(fullname); err != nil {
|
|
|
|
return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path)
|
2018-12-11 09:03:47 -05:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(fullname, NamePrefix) {
|
2019-01-14 12:53:19 -05:00
|
|
|
return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix)
|
2018-12-11 09:03:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
p := Plugin{
|
|
|
|
Name: strings.TrimPrefix(fullname, NamePrefix),
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now apply the candidate tests, so these update p.Err.
|
|
|
|
if !pluginNameRe.MatchString(p.Name) {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = NewPluginError("plugin candidate %q did not match %q", p.Name, pluginNameRe.String())
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rootcmd != nil {
|
|
|
|
for _, cmd := range rootcmd.Commands() {
|
2018-12-11 09:50:04 -05:00
|
|
|
// Ignore conflicts with commands which are
|
|
|
|
// just plugin stubs (i.e. from a previous
|
|
|
|
// call to AddPluginCommandStubs).
|
|
|
|
if p := cmd.Annotations[CommandAnnotationPlugin]; p == "true" {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-11 09:03:47 -05:00
|
|
|
if cmd.Name() == p.Name {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = NewPluginError("plugin %q duplicates builtin command", p.Name)
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
if cmd.HasAlias(p.Name) {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = NewPluginError("plugin %q duplicates an alias of builtin command %q", p.Name, cmd.Name())
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are supposed to check for relevant execute permissions here. Instead we rely on an attempt to execute.
|
|
|
|
meta, err := c.Metadata()
|
|
|
|
if err != nil {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = wrapAsPluginError(err, "failed to fetch metadata")
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(meta, &p.Metadata); err != nil {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = wrapAsPluginError(err, "invalid metadata")
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
if p.Metadata.SchemaVersion != "0.1.0" {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = NewPluginError("plugin SchemaVersion %q is not valid, must be 0.1.0", p.Metadata.SchemaVersion)
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
if p.Metadata.Vendor == "" {
|
2018-12-19 09:49:20 -05:00
|
|
|
p.Err = NewPluginError("plugin metadata does not define a vendor")
|
2018-12-11 09:03:47 -05:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|