github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/plugins/plugdeps/plugdeps.go (about) 1 package plugdeps 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/gobuffalo/meta" 10 "github.com/karrick/godirwalk" 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 depeneds 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 if app.WithPop { 22 plugs.Add(pop) 23 } 24 25 lp, err := listLocal(app) 26 if err != nil { 27 return plugs, err 28 } 29 plugs.Add(lp.List()...) 30 31 if !On(app) { 32 return plugs, ErrMissingConfig 33 } 34 35 p := ConfigPath(app) 36 tf, err := os.Open(p) 37 if err != nil { 38 return plugs, err 39 } 40 if err := plugs.Decode(tf); err != nil { 41 return plugs, err 42 } 43 44 return plugs, nil 45 } 46 47 func listLocal(app meta.App) (*Plugins, error) { 48 plugs := New() 49 proot := filepath.Join(app.Root, "plugins") 50 if _, err := os.Stat(proot); err != nil { 51 return plugs, nil 52 } 53 err := godirwalk.Walk(proot, &godirwalk.Options{ 54 FollowSymbolicLinks: true, 55 Callback: func(path string, info *godirwalk.Dirent) error { 56 if info.IsDir() { 57 return nil 58 } 59 base := filepath.Base(path) 60 if strings.HasPrefix(base, "buffalo-") { 61 plugs.Add(Plugin{ 62 Binary: base, 63 Local: "." + strings.TrimPrefix(path, app.Root), 64 }) 65 } 66 return nil 67 }, 68 }) 69 if err != nil { 70 return plugs, err 71 } 72 73 return plugs, nil 74 } 75 76 // ConfigPath returns the path to the config/buffalo-plugins.toml file 77 // relative to the app 78 func ConfigPath(app meta.App) string { 79 return filepath.Join(app.Root, "config", "buffalo-plugins.toml") 80 } 81 82 // On checks for the existence of config/buffalo-plugins.toml if this 83 // file exists its contents will be used to list plugins. If the file is not 84 // found, then the BUFFALO_PLUGIN_PATH and ./plugins folders are consulted. 85 func On(app meta.App) bool { 86 _, err := os.Stat(ConfigPath(app)) 87 return err == nil 88 }