This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
68 lines (63 loc) · 1.78 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
package pink
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
)
// GetPluginsDir returns the default root of the plugins tree below a given root.
func GetPluginsDir(root string) string {
return filepath.Join(root, "plugins")
}
// findPlugins returns the paths of all manifest.json files of plugins installed below the given root.
func findPlugins(root string) ([]string, error) {
plugins := []string{}
nodes, err := ioutil.ReadDir(root)
if err != nil {
return nil, err
}
for _, node := range nodes {
if node.IsDir() {
plugins = append(plugins, node.Name())
}
}
return plugins, nil
}
func isInvokable(path string) (bool, error) {
nodes, err := ioutil.ReadDir(path)
if err != nil {
return false, err
}
foundManifest := false
for _, n := range nodes {
if !n.IsDir() && n.Name() == "manifest.json" {
foundManifest = true
break
}
}
return foundManifest, nil
}
// DispatchCommand finds the manifest for the correct command, or instructs us to run "help". If neither case is appropriate, an error is returned.
func DispatchCommand(args []string, path []string, root string) (string, []string, bool, error) {
subpath := filepath.Join(path...)
fullPath := filepath.Join(root, subpath)
inv, err := isInvokable(fullPath)
if err != nil {
pErr, ok := err.(*os.PathError)
if ok && pErr.Err == syscall.ENOENT {
return "", nil, false, fmt.Errorf("No plug-in called %q is installed", strings.Join(path, " "))
}
return "", nil, false, err
}
if inv {
return filepath.Join(fullPath, "manifest.json"), args, false, nil
}
// We've hit the end of the args without finding a manifest, we should run help!
if len(args) == 0 || args[0] == "-h" {
return fullPath, args, true, nil
}
path = append(path, args[0])
return DispatchCommand(args[1:], path, root)
}