-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
241 lines (187 loc) · 4.93 KB
/
plugin.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package plugin
import (
"context"
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"github.com/bool64/ctxd"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)
const (
// MetadataFile is the plugin metadata file.
MetadataFile = ".plugin.registry.yaml"
defaultFile = "${name}-${version}-${os}-${arch}.tar.gz"
)
// ErrPluginNotExist indicates that the plugin does not exist.
var ErrPluginNotExist = errors.New("plugin does not exist")
// Plugins is a map of plugins.
type Plugins map[string]Plugin
// FilterByTag filter the plugins by tags.
func (p Plugins) FilterByTag(tag string) Plugins {
result := make(Plugins, len(p))
for k, v := range p {
if v.Tags.Contains(tag) {
result[k] = v
}
}
return result
}
// Has checks whether the plugin is in the map or not.
func (p Plugins) Has(name string) bool {
_, ok := p[name]
return ok
}
// Plugin represents metadata of a plugin.
type Plugin struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Version string `yaml:"version"`
Description string `yaml:"description"`
Enabled bool `yaml:"enabled"`
Hidden bool `yaml:"hidden"`
Artifacts Artifacts `yaml:"artifacts"`
Tags Tags `yaml:"tags"`
}
// RuntimeArtifact returns the artifact of current arch.
func (p *Plugin) RuntimeArtifact() Artifact {
if a, ok := p.Artifacts[RuntimeArtifactIdentifier()]; ok {
return a
}
if a, ok := p.Artifacts[RuntimeArtifactIdentifierWithoutArch()]; ok {
return a
}
return Artifact{File: defaultFile}
}
// ResolveArtifact replaces all placeholders in artifact definition by real values.
func (p *Plugin) ResolveArtifact(a Artifact) Artifact {
r := strings.NewReplacer(
"${name}", p.Name,
"${name}", p.Name,
"${version}", p.Version,
"${os}", runtime.GOOS,
"${arch}", runtime.GOARCH,
)
a.File = r.Replace(a.File)
return a
}
// UnmarshalYAML satisfies yaml.Unmarshaler.
func (p *Plugin) UnmarshalYAML(value *yaml.Node) error {
type rawPlugin Plugin
raw := rawPlugin(defaultPluginConfig())
if err := value.Decode(&raw); err != nil {
return err
}
if !raw.Artifacts.Has(RuntimeArtifactIdentifier()) &&
!raw.Artifacts.Has(RuntimeArtifactIdentifierWithoutArch()) {
raw.Artifacts[RuntimeArtifactIdentifier()] = Artifact{File: defaultFile}
}
*p = Plugin(raw)
return nil
}
// Tags is a list of string tag.
type Tags []string
// Contains checks whether a tag is in the list or not.
func (t Tags) Contains(tag string) bool {
for _, v := range t {
if v == tag {
return true
}
}
return false
}
// Artifacts is a map of Artifact, identified by os and arch.
type Artifacts map[ArtifactIdentifier]Artifact
// Has checks whether the artifact is in the list.
func (a *Artifacts) Has(id ArtifactIdentifier) bool {
_, ok := (*a)[id]
return ok
}
// MarshalYAML satisfies yaml.Marshaler.
func (a Artifacts) MarshalYAML() (interface{}, error) { //nolint: unparam
raw := make(map[string]Artifact, len(a))
for k, v := range a {
raw[k.String()] = v
}
return raw, nil
}
// ArtifactIdentifier represents information to identify an artifact.
type ArtifactIdentifier struct {
OS string
Arch string
}
// String satisfies fmt.Stringer.
func (a ArtifactIdentifier) String() string {
var s string
if a.Arch == "" {
s = a.OS
} else {
s = fmt.Sprintf("%s/%s", a.OS, a.Arch)
}
return s
}
// UnmarshalYAML satisfies yaml.Unmarshaler.
func (a *ArtifactIdentifier) UnmarshalYAML(value *yaml.Node) error {
var raw string
if err := value.Decode(&raw); err != nil {
return err
}
parts := strings.SplitN(raw, "/", 2)
*a = ArtifactIdentifier{
OS: parts[0],
Arch: "",
}
if len(parts) > 1 {
a.Arch = parts[1]
}
return nil
}
// Artifact represents all information about an artifact of a plugin.
type Artifact struct {
File string `yaml:"file"`
}
func defaultPluginConfig() Plugin {
return Plugin{
Enabled: true,
Artifacts: map[ArtifactIdentifier]Artifact{},
}
}
// RuntimeArtifactIdentifier returns the system's identifier.
func RuntimeArtifactIdentifier() ArtifactIdentifier {
return ArtifactIdentifier{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}
}
// RuntimeArtifactIdentifierWithoutArch returns the system's identifier without arch.
func RuntimeArtifactIdentifierWithoutArch() ArtifactIdentifier {
return ArtifactIdentifier{
OS: runtime.GOOS,
Arch: "",
}
}
// NewArtifactIdentifier creates a new ArtifactIdentifier.
func NewArtifactIdentifier(os, arch string) ArtifactIdentifier {
return ArtifactIdentifier{
OS: os,
Arch: arch,
}
}
// Load loads plugin metadata.
func Load(fs afero.Fs, path string) (*Plugin, error) {
r, err := fs.Open(filepath.Join(path, MetadataFile))
if err != nil {
return nil, loadError(err, path)
}
var p Plugin
dec := yaml.NewDecoder(r)
if err := dec.Decode(&p); err != nil {
return nil, loadError(err, path)
}
return &p, nil
}
func loadError(err error, path string) error {
return ctxd.WrapError(context.Background(), err, "could not read metadata", "path", path)
}