-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system_registry.go
89 lines (78 loc) · 2.5 KB
/
file_system_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
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
package plugin
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path"
)
// FileSystemRegistry implements plugin.Registry using a local directory of plugins.
type FileSystemRegistry struct {
PluginsDirPath string
isInitialized bool
}
func (r *FileSystemRegistry) Initialize() error {
if r.PluginsDirPath == "" {
return ErrNoPluginsDirPath
}
if r.isInitialized {
return ErrAlreadyInitialized
}
r.isInitialized = true
return nil
}
func (r *FileSystemRegistry) IsInitialized() bool {
return r.isInitialized
}
// ValidatePlugin validates that the prerequisites of a plugin in the registry are given for the plugin's manifest. Prerequisites may be defined as dependencies, versions, and licensing.
//
// Not to be confused with validating the prerequisites of a plugin in the project during plugin installation.
func (r *FileSystemRegistry) ValidatePlugin(pluginID ID) (Manifest, error) {
if !r.IsInitialized() {
return Manifest{}, errWrapID(pluginID, ErrNotInitialized)
}
manifest, manifestErr := r.getPluginManifest(pluginID)
if manifestErr != nil {
return Manifest{}, errWrapID(pluginID, manifestErr)
}
return manifest, nil
}
// LoadPlugin loads a plugin from the registry into the local environment and memory via the passed plugin ID.
func (r *FileSystemRegistry) LoadPlugin(manifest Manifest) (Plugin, error) {
return Plugin{}, nil
}
func (r *FileSystemRegistry) getPluginManifest(pluginID ID) (Manifest, error) {
manifestPath, pathErr := r.getPluginManifestPath(pluginID)
if pathErr != nil {
return Manifest{}, pathErr
}
manifestContents, readErr := os.ReadFile(manifestPath)
if readErr != nil && errors.Is(readErr, fs.ErrNotExist) {
return Manifest{}, fmt.Errorf("%w: %v", ErrManifestNotFound, readErr)
}
if readErr != nil {
return Manifest{}, readErr
}
manifest := Manifest{}
unmarshalErr := json.Unmarshal(manifestContents, &manifest)
if unmarshalErr != nil {
return Manifest{}, unmarshalErr
}
return manifest, nil
}
func (r *FileSystemRegistry) getPluginSubdirectoryPath(pluginID ID) (string, error) {
if validErr := pluginID.Validate(); validErr != nil {
return "", validErr
}
subDirPath := path.Join(r.PluginsDirPath, string(pluginID.Type), pluginID.Name, pluginID.Version)
return subDirPath, nil
}
func (r *FileSystemRegistry) getPluginManifestPath(pluginID ID) (string, error) {
pluginPath, subDirErr := r.getPluginSubdirectoryPath(pluginID)
if subDirErr != nil {
return "", subDirErr
}
manifestPath := path.Join(pluginPath, "MANIFEST.json")
return manifestPath, nil
}