-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
84 lines (74 loc) · 1.39 KB
/
root.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
package mackerelfs
import (
"errors"
"io/fs"
"strings"
"github.com/mackerelio/mackerel-client-go"
"github.com/rmatsuoka/mackerelfs/internal/muxfs"
)
type root map[string]orgs
type orgs struct {
api string
fsys fs.FS
}
func FS() fs.FS {
r := root{}
m := muxfs.NewFS()
m.File("ctl", muxfs.CtlFile(r.ctlFile))
m.VarFS(r)
return m
}
func (r root) ctlFile(s string) error {
f := strings.Fields(s)
if len(f) < 1 {
return nil
}
switch f[0] {
case "new":
if len(f) == 1 {
return errors.New("missing arguments")
}
name, fsys, err := orgFS(newClient(f[1]))
if err != nil {
return err
}
r[name] = orgs{api: f[1], fsys: fsys}
case "delete":
if len(f) == 1 {
return errors.New("missing arguments")
}
delete(r, f[1])
}
return nil
}
func (r root) All() (muxfs.Seq[string], error) {
return func(yield func(string) bool) {
for k := range r {
if !yield(k) {
return
}
}
}, nil
}
func (r root) FS(name string) (fs.FS, bool) {
f, ok := r[name]
return f.fsys, ok
}
func orgFS(c *mackerel.Client) (name string, fsys fs.FS, err error) {
org, err := c.GetOrg()
if err != nil {
return "", nil, err
}
m := muxfs.NewFS()
m.FS("hosts", hostsFS(c))
m.FS("service", servicesFS(c))
return org.Name, m, nil
}
func newClient(apikey string) *mackerel.Client {
client, _ := mackerel.NewClientWithOptions(
apikey,
"https://api.mackerelio.com/",
true,
)
return client
}