This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 573
/
plugdeps.go
88 lines (76 loc) · 2.11 KB
/
plugdeps.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
package plugdeps
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/meta"
"github.com/karrick/godirwalk"
)
// ErrMissingConfig is if config/buffalo-plugins.toml file is not found. Use plugdeps#On(app) to test if plugdeps are being used
var ErrMissingConfig = fmt.Errorf("could not find a buffalo-plugins config file at %s", ConfigPath(meta.New(".")))
// List all of the plugins the application depeneds on. Will return ErrMissingConfig
// if the app is not using config/buffalo-plugins.toml to manage their plugins.
// Use plugdeps#On(app) to test if plugdeps are being used.
func List(app meta.App) (*Plugins, error) {
plugs := New()
if app.WithPop {
plugs.Add(pop)
}
lp, err := listLocal(app)
if err != nil {
return plugs, err
}
plugs.Add(lp.List()...)
if !On(app) {
return plugs, ErrMissingConfig
}
p := ConfigPath(app)
tf, err := os.Open(p)
if err != nil {
return plugs, err
}
if err := plugs.Decode(tf); err != nil {
return plugs, err
}
return plugs, nil
}
func listLocal(app meta.App) (*Plugins, error) {
plugs := New()
proot := filepath.Join(app.Root, "plugins")
if _, err := os.Stat(proot); err != nil {
return plugs, nil
}
err := godirwalk.Walk(proot, &godirwalk.Options{
FollowSymbolicLinks: true,
Callback: func(path string, info *godirwalk.Dirent) error {
if info.IsDir() {
return nil
}
base := filepath.Base(path)
if strings.HasPrefix(base, "buffalo-") {
plugs.Add(Plugin{
Binary: base,
Local: "." + strings.TrimPrefix(path, app.Root),
})
}
return nil
},
})
if err != nil {
return plugs, err
}
return plugs, nil
}
// ConfigPath returns the path to the config/buffalo-plugins.toml file
// relative to the app
func ConfigPath(app meta.App) string {
return filepath.Join(app.Root, "config", "buffalo-plugins.toml")
}
// On checks for the existence of config/buffalo-plugins.toml if this
// file exists its contents will be used to list plugins. If the file is not
// found, then the BUFFALO_PLUGIN_PATH and ./plugins folders are consulted.
func On(app meta.App) bool {
_, err := os.Stat(ConfigPath(app))
return err == nil
}