github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/plugins/plugdeps/plugdeps.go (about) 1 package plugdeps 2 3 import ( 4 "fmt" 5 "io/fs" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/gobuffalo/meta" 11 ) 12 13 // ErrMissingConfig is if config/buffalo-plugins.toml file is not found. Use plugdeps#On(app) to test if plugdeps are being used 14 var ErrMissingConfig = fmt.Errorf("could not find a buffalo-plugins config file at %s", ConfigPath(meta.New("."))) 15 16 // List all of the plugins the application depends on. Will return ErrMissingConfig 17 // if the app is not using config/buffalo-plugins.toml to manage their plugins. 18 // Use plugdeps#On(app) to test if plugdeps are being used. 19 func List(app meta.App) (*Plugins, error) { 20 plugs := New() 21 22 lp, err := listLocal(app) 23 if err != nil { 24 return plugs, err 25 } 26 plugs.Add(lp.List()...) 27 28 if !On(app) { 29 return plugs, ErrMissingConfig 30 } 31 32 p := ConfigPath(app) 33 tf, err := os.Open(p) 34 if err != nil { 35 return plugs, err 36 } 37 if err := plugs.Decode(tf); err != nil { 38 return plugs, err 39 } 40 41 return plugs, nil 42 } 43 44 func listLocal(app meta.App) (*Plugins, error) { 45 plugs := New() 46 proot := filepath.Join(app.Root, "plugins") 47 if _, err := os.Stat(proot); err != nil { 48 return plugs, nil 49 } 50 51 err := fs.WalkDir(os.DirFS(proot), ".", func(path string, d fs.DirEntry, err error) error { 52 if err != nil { 53 return err 54 } 55 56 if d.IsDir() { 57 return nil 58 } 59 60 base := filepath.Base(path) 61 if strings.HasPrefix(base, "buffalo-") { 62 plugs.Add(Plugin{ 63 Binary: base, 64 Local: "." + strings.TrimPrefix(path, app.Root), 65 }) 66 } 67 return nil 68 }) 69 return plugs, err 70 } 71 72 // ConfigPath returns the path to the config/buffalo-plugins.toml file 73 // relative to the app 74 func ConfigPath(app meta.App) string { 75 return filepath.Join(app.Root, "config", "buffalo-plugins.toml") 76 } 77 78 // On checks for the existence of config/buffalo-plugins.toml if this 79 // file exists its contents will be used to list plugins. If the file is not 80 // found, then the BUFFALO_PLUGIN_PATH and ./plugins folders are consulted. 81 func On(app meta.App) bool { 82 _, err := os.Stat(ConfigPath(app)) 83 return err == nil 84 }