-
Notifications
You must be signed in to change notification settings - Fork 103
/
plugins.go
156 lines (125 loc) · 2.72 KB
/
plugins.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package plugins
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/choria-io/fisk"
)
var validNames = regexp.MustCompile(`^[a-z]+$`)
type plugin struct {
Cmd string `json:"cmd"`
Definition json.RawMessage `json:"def"`
}
func AddToApp(app *fisk.Application) error {
parent, err := pluginDir()
if err != nil {
return err
}
entries, err := os.ReadDir(parent)
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != ".json" {
continue
}
pb, err := os.ReadFile(filepath.Join(parent, entry.Name()))
if err != nil {
log.Printf("Could not read plugin %v: %v", entry.Name(), err)
continue
}
var p plugin
err = json.Unmarshal(pb, &p)
if err != nil {
log.Printf("Could not read plugin %v: %v", entry.Name(), err)
continue
}
_, err = app.ExternalPluginCommand(p.Cmd, p.Definition, strings.TrimSuffix(entry.Name(), ".json"), "")
if err != nil {
log.Printf("Invalid plugin %v: %v", entry.Name(), err)
continue
}
}
return nil
}
func Register(name string, command string, force bool) error {
if !validNames.MatchString(name) {
return fmt.Errorf("plugins names must match ^[a-z]$")
}
cmd, err := filepath.Abs(command)
if err != nil {
return err
}
store, err := pluginDir()
if err != nil {
return err
}
pluginPath := filepath.Join(store, fmt.Sprintf("%s.json", name))
if !force {
exist, _ := fileAccessible(pluginPath)
if exist {
return fmt.Errorf("plugins %s already registered, use --force to update", name)
}
}
intro := exec.Command(cmd, "--fisk-introspect")
out, err := intro.CombinedOutput()
if err != nil {
return err
}
pj, err := json.Marshal(plugin{cmd, out})
if err != nil {
return err
}
err = os.WriteFile(pluginPath, pj, 0600)
if err != nil {
return err
}
return nil
}
func fileAccessible(f string) (bool, error) {
stat, err := os.Stat(f)
if err != nil {
return false, err
}
if stat.IsDir() {
return false, fmt.Errorf("is a directory")
}
file, err := os.Open(f)
if err != nil {
return false, err
}
file.Close()
return true, nil
}
func pluginDir() (string, error) {
parent, err := xdgConfigHome()
if err != nil {
return "", err
}
dir := filepath.Join(parent, "nats", "cli", "plugins")
err = os.MkdirAll(dir, 0700)
if err != nil {
return "", err
}
return dir, nil
}
func xdgConfigHome() (string, error) {
parent := os.Getenv("XDG_CONFIG_HOME")
if parent != "" {
return parent, nil
}
u, err := user.Current()
if err != nil {
return "", err
}
if u.HomeDir == "" {
return "", fmt.Errorf("cannot determine home directory")
}
return filepath.Join(u.HomeDir, parent, ".config"), nil
}