This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
plugins.go
91 lines (80 loc) · 1.76 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
package plugdeps
import (
"io"
"sort"
"sync"
"github.com/BurntSushi/toml"
)
// Plugins manages the config/buffalo-plugins.toml file
// as well as the plugins available from the file.
type Plugins struct {
plugins map[string]Plugin
moot *sync.RWMutex
}
// Encode the list of plugins, in TOML format, to the reader
func (plugs *Plugins) Encode(w io.Writer) error {
tp := tomlPlugins{
Plugins: plugs.List(),
}
if err := toml.NewEncoder(w).Encode(tp); err != nil {
return err
}
return nil
}
// Decode the list of plugins, in TOML format, from the reader
func (plugs *Plugins) Decode(r io.Reader) error {
tp := &tomlPlugins{
Plugins: []Plugin{},
}
if _, err := toml.DecodeReader(r, tp); err != nil {
return err
}
for _, p := range tp.Plugins {
plugs.Add(p)
}
return nil
}
// List of dependent plugins listed in order of Plugin.String()
func (plugs *Plugins) List() []Plugin {
m := map[string]Plugin{}
plugs.moot.RLock()
for _, p := range plugs.plugins {
m[p.key()] = p
}
plugs.moot.RUnlock()
var pp []Plugin
for _, v := range m {
pp = append(pp, v)
}
sort.Slice(pp, func(a, b int) bool {
return pp[a].Binary < pp[b].Binary
})
return pp
}
// Add plugin(s) to the list of dependencies
func (plugs *Plugins) Add(pp ...Plugin) {
plugs.moot.Lock()
for _, p := range pp {
plugs.plugins[p.key()] = p
}
plugs.moot.Unlock()
}
// Remove plugin(s) from the list of dependencies
func (plugs *Plugins) Remove(pp ...Plugin) {
plugs.moot.Lock()
for _, p := range pp {
delete(plugs.plugins, p.key())
}
plugs.moot.Unlock()
}
// New returns a configured *Plugins value
func New() *Plugins {
plugs := &Plugins{
plugins: map[string]Plugin{},
moot: &sync.RWMutex{},
}
return plugs
}
type tomlPlugins struct {
Plugins []Plugin `toml:"plugin"`
}