/
registry.go
62 lines (48 loc) · 1.25 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cobraplugin
import (
"os/exec"
"path/filepath"
registry "github.com/nhatthm/plugin-registry"
"github.com/nhatthm/plugin-registry/plugin"
)
// Registry is a wrapper around registry.Registry.
type Registry struct {
registry.Registry
path string
}
// Plugins returns a list of plugins.
func (r *Registry) Plugins() (plugin.Plugins, error) {
cfg, err := r.Config()
if err != nil {
return nil, err
}
return cfg.Plugins, nil
}
// Command returns a new exec.Command from the plugin name.
func (r *Registry) Command(pluginName string, args ...string) (*exec.Cmd, error) {
plugins, err := r.Plugins()
if err != nil {
return nil, err
}
if !plugins.Has(pluginName) {
return nil, plugin.ErrPluginNotExist
}
cmd := filepath.Join(r.path, pluginName, pluginName)
return exec.Command(cmd, args...), nil // nolint: gosec
}
// PluginRegistry provides registry.Registry service.
func (r *Registry) PluginRegistry() registry.Registry {
return r
}
// NewRegistry initiates a new Registry.
func NewRegistry(path string, options ...registry.Option) (*Registry, error) {
upstream, err := registry.NewRegistry(path, options...)
if err != nil {
return nil, err
}
r := &Registry{
Registry: upstream,
path: filepath.Clean(path),
}
return r, nil
}