Repository navigation
Expand file tree
/
Copy pathscanner.go
More file actions
151 lines (136 loc) · 4.14 KB
/
Copy pathscanner.go
File metadata and controls
151 lines (136 loc) · 4.14 KB
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
package plugins
/*
some of the code here is borrowed from the krew codebse (kubernetes)
and the copyright belongs to the respective authors.
source: https://github.com/kubernetes-sigs/krew/tree/master/internal
*/
import (
stderrors "errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"github.com/goccy/go-yaml"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/hasura/graphql-engine/cli/v2/plugins/paths"
"github.com/spf13/afero"
)
func (c *Config) findPluginManifestFiles(indexDir string) ([]string, error) {
var op errors.Op = "plugins.Config.findPluginManifestFiles"
c.Logger.Debugf("finding plugin manifest files in directory %v", indexDir)
var out []string
fs := afero.Afero{
Fs: afero.NewOsFs(),
}
err := fs.Walk(indexDir, func(path string, info os.FileInfo, err error) error {
if info == nil {
if err != nil {
return errors.E(op, err)
}
return nil
}
if info.Mode().IsRegular() && filepath.Ext(info.Name()) == paths.ManifestExtension {
out = append(out, path)
}
return nil
})
if err != nil {
return nil, errors.E(op, err)
}
return out, nil
}
// LoadPluginListFromFS will parse and retrieve all plugin files.
func (c *Config) LoadPluginListFromFS(indexDir string) (Plugins, error) {
var op errors.Op = "plugins.Config.LoadPluginListFromFS"
indexDir, err := filepath.EvalSymlinks(indexDir)
if err != nil {
return nil, errors.E(op, err)
}
files, err := c.findPluginManifestFiles(indexDir)
if err != nil {
return nil, errors.E(op, fmt.Errorf("failed to scan plugins in index directory: %w", err))
}
return c.LoadPlugins(files), nil
}
// LoadPluginByName loads a plugins index file by its name. When plugin
// file not found, it returns an error that can be checked with stderrors.Is(err, fs.ErrNotExist)
func (c *Config) LoadPluginByName(pluginName string) (*PluginVersions, error) {
var op errors.Op = "plugins.Config.LoadPluginByName"
c.Logger.Debugf("loading plugin %s", pluginName)
if !IsSafePluginName(pluginName) {
return nil, errors.E(op, fmt.Errorf("plugin name %q not allowed", pluginName))
}
files, err := c.findPluginManifestFiles(c.Paths.IndexPluginsPath())
if err != nil {
return nil, errors.E(op, fmt.Errorf("failed to scan plugins in index directory: %w", err))
}
ps := c.LoadPlugins(files, pluginName)
if _, ok := ps[pluginName]; !ok {
return nil, errors.E(op, os.ErrNotExist)
}
return ps[pluginName], nil
}
func (c *Config) LoadPlugins(files []string, pluginName ...string) Plugins {
c.Logger.Debugf("loading plugins")
ps := Plugins{}
for _, file := range files {
p, err := c.ReadPluginFromFile(file)
if err != nil {
c.Logger.Debugf("failed to read or parse plugin manifest: %v", err)
continue
}
if p.ParsedVersion == nil {
c.Logger.Debugf("version of plugin %s cannot be nil", p.Name)
continue
}
if len(pluginName) != 0 {
var isFound bool
for _, name := range pluginName {
if name == p.Name {
isFound = true
break
}
}
if !isFound {
continue
}
}
if _, ok := ps[p.Name]; !ok {
ps[p.Name] = NewPluginVersions()
}
err = ps[p.Name].Append(p)
if err != nil {
c.Logger.Debugf("failed to append version %s for plugin %s: %v", p.Version, p.Name, err)
continue
}
}
return ps
}
// ReadPluginFromFile loads a file from the FS. When plugin file not found, it
// returns an error that can be checked with stderrors.Is(err, fs.ErrNotExist).
func (c *Config) ReadPluginFromFile(path string) (Plugin, error) {
var op errors.Op = "plugins.Config.ReadPluginFromFile"
f, err := os.Open(path)
if stderrors.Is(err, fs.ErrNotExist) {
return Plugin{}, errors.E(op, err)
} else if err != nil {
return Plugin{}, errors.E(op, fmt.Errorf("failed to open index file: %w", err))
}
defer f.Close()
var plugin Plugin
b, err := ioutil.ReadAll(f)
if err != nil {
return plugin, errors.E(op, err)
}
err = yaml.Unmarshal(b, &plugin)
if err != nil {
return plugin, errors.E(op, fmt.Errorf("failed to decode plugin manifest: %w", err))
}
plugin.ParseVersion()
err = plugin.ValidatePlugin(plugin.Name)
if err != nil {
return plugin, errors.E(op, fmt.Errorf("plugin manifest validation error: %w", err))
}
return plugin, nil
}