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