github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli-plugins/manager/plugin.go (about) 1 package manager 2 3 import ( 4 "encoding/json" 5 "os/exec" 6 "path/filepath" 7 "regexp" 8 "strings" 9 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 ) 13 14 var pluginNameRe = regexp.MustCompile("^[a-z][a-z0-9]*$") 15 16 // Plugin represents a potential plugin with all it's metadata. 17 type Plugin struct { 18 Metadata 19 20 Name string `json:",omitempty"` 21 Path string `json:",omitempty"` 22 23 // Err is non-nil if the plugin failed one of the candidate tests. 24 Err error `json:",omitempty"` 25 26 // ShadowedPaths contains the paths of any other plugins which this plugin takes precedence over. 27 ShadowedPaths []string `json:",omitempty"` 28 } 29 30 // newPlugin determines if the given candidate is valid and returns a 31 // Plugin. If the candidate fails one of the tests then `Plugin.Err` 32 // is set, and is always a `pluginError`, but the `Plugin` is still 33 // returned with no error. An error is only returned due to a 34 // non-recoverable error. 35 func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) { 36 path := c.Path() 37 if path == "" { 38 return Plugin{}, errors.New("plugin candidate path cannot be empty") 39 } 40 41 // The candidate listing process should have skipped anything 42 // which would fail here, so there are all real errors. 43 fullname := filepath.Base(path) 44 if fullname == "." { 45 return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path) 46 } 47 var err error 48 if fullname, err = trimExeSuffix(fullname); err != nil { 49 return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path) 50 } 51 if !strings.HasPrefix(fullname, NamePrefix) { 52 return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix) 53 } 54 55 p := Plugin{ 56 Name: strings.TrimPrefix(fullname, NamePrefix), 57 Path: path, 58 } 59 60 // Now apply the candidate tests, so these update p.Err. 61 if !pluginNameRe.MatchString(p.Name) { 62 p.Err = NewPluginError("plugin candidate %q did not match %q", p.Name, pluginNameRe.String()) 63 return p, nil 64 } 65 66 for _, cmd := range cmds { 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 // We are supposed to check for relevant execute permissions here. Instead we rely on an attempt to execute. 84 meta, err := c.Metadata() 85 if err != nil { 86 p.Err = wrapAsPluginError(err, "failed to fetch metadata") 87 return p, nil 88 } 89 90 if err := json.Unmarshal(meta, &p.Metadata); err != nil { 91 p.Err = wrapAsPluginError(err, "invalid metadata") 92 return p, nil 93 } 94 if p.Metadata.SchemaVersion != "0.1.0" { 95 p.Err = NewPluginError("plugin SchemaVersion %q is not valid, must be 0.1.0", p.Metadata.SchemaVersion) 96 return p, nil 97 } 98 if p.Metadata.Vendor == "" { 99 p.Err = NewPluginError("plugin metadata does not define a vendor") 100 return p, nil 101 } 102 return p, nil 103 } 104 105 // RunHook executes the plugin's hooks command 106 // and returns its unprocessed output. 107 func (p *Plugin) RunHook(cmdName string, flags map[string]string) ([]byte, error) { 108 hDataBytes, err := json.Marshal(HookPluginData{ 109 RootCmd: cmdName, 110 Flags: flags, 111 }) 112 if err != nil { 113 return nil, wrapAsPluginError(err, "failed to marshall hook data") 114 } 115 116 hookCmdOutput, err := exec.Command(p.Path, p.Name, HookSubcommandName, string(hDataBytes)).Output() 117 if err != nil { 118 return nil, wrapAsPluginError(err, "failed to execute plugin hook subcommand") 119 } 120 121 return hookCmdOutput, nil 122 }