Skip to content

Commit

Permalink
Refactor and reorder plugin.go
Browse files Browse the repository at this point in the history
Rename collectPlugins -> fetchPlugins, keep names consistent

Add some doc comments
  • Loading branch information
gastrodon committed Aug 17, 2024
1 parent 1d5cec8 commit f1dbe14
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
100 changes: 54 additions & 46 deletions configure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,7 @@ func fetchPlugin(cachePath, binPath string, descriptor pluginBlock) (string, err
}
}

func loadPlugin(pluginPath string, descriptor pluginBlock) (*sdk.Plugin, error) {
plugin, err := plugin.Open(pluginPath)
if err != nil {
return nil, fmt.Errorf("failed loading the library providing %s ( %s @ %s ):\n%s",
descriptor.Name, descriptor.Source, pluginPath, err)
}

makePluginSym, err := plugin.Lookup("Plugin")
if err != nil {
return nil, fmt.Errorf("failed loading the provider func for %s:\n%s", descriptor.Name, err)
}

if makePluginSym == nil {
return nil, fmt.Errorf("failed loading the provider func for %s:\nLookup \"Plugin\" is nil", descriptor.Name)
}

makePlugin, ok := makePluginSym.(func() *sdk.Plugin)
if !ok {
return nil, fmt.Errorf("failed loading the provider func for %s:\nnot OK: %+v", descriptor.Name, makePluginSym)
}

return makePlugin(), nil
}

func collectPlugins(cachePath, binPath string, blocks []pluginBlock) (map[string]string, error) {
func fetchPlugins(cachePath, binPath string, blocks []pluginBlock) (map[string]string, error) {
collected := make(map[string]string, len(blocks))
for _, desc := range blocks {
loc, err := fetchPlugin(cachePath, binPath, desc)
Expand All @@ -138,26 +114,13 @@ func collectPlugins(cachePath, binPath string, blocks []pluginBlock) (map[string
return collected, nil
}

func loadPlugins(binPaths map[string]string, blocks []pluginBlock) ([]*sdk.Plugin, error) {
plugins := make([]*sdk.Plugin, len(blocks))
for i, descriptor := range blocks {
binPath, ok := binPaths[descriptor.Name]
if !ok {
return nil, fmt.Errorf("binary not found for plugin %s", descriptor.Name)
}

plugin, err := loadPlugin(binPath, descriptor)
if err != nil {
return nil, fmt.Errorf("unable to load plugin %s: %s", descriptor.Name, err)
}

plugins[i] = plugin
}

return plugins, nil
}
/*
TODO: de-wrap loadPlugins and put housekeeping somewhere else
func CollectPlugins(initPath string, blocks []pluginBlock) (map[string]string, error) {
Fetch plugins, cloning and building them if necessary, producing a map plugin-name -> bin-path.
Make sure cache and build paths are made, cache path is cleaned, before wraping fetchPlugins
*/
func FetchPlugins(initPath string, blocks []pluginBlock) (map[string]string, error) {
cachePath, err := os.MkdirTemp("", "psyduck-plugin-*")
if err != nil {
return nil, fmt.Errorf("failed to cache dir: %s", err)
Expand All @@ -168,7 +131,7 @@ func CollectPlugins(initPath string, blocks []pluginBlock) (map[string]string, e
return nil, fmt.Errorf("failed to create binpath: %s", err)
}

collected, err := collectPlugins(cachePath, binPath, blocks)
collected, err := fetchPlugins(cachePath, binPath, blocks)
if err != nil {
return nil, fmt.Errorf("failed to collect: %s", err)
}
Expand All @@ -180,6 +143,51 @@ func CollectPlugins(initPath string, blocks []pluginBlock) (map[string]string, e
return collected, nil
}

func loadPlugin(name, binPath string) (*sdk.Plugin, error) {
plugin, err := plugin.Open(binPath)
if err != nil {
return nil, fmt.Errorf("failed loading the library providing %s (@ %s):\n%s", name, binPath, err)
}

makePluginSym, err := plugin.Lookup("Plugin")
if err != nil {
return nil, fmt.Errorf("failed loading the provider func for %s:\n%s", name, err)
}

if makePluginSym == nil {
return nil, fmt.Errorf("failed loading the provider func for %s:\nLookup \"Plugin\" is nil", name)
}

makePlugin, ok := makePluginSym.(func() *sdk.Plugin)
if !ok {
return nil, fmt.Errorf("failed loading the provider func for %s:\nnot OK: %+v", name, makePluginSym)
}

return makePlugin(), nil
}

func loadPlugins(binPaths map[string]string) ([]*sdk.Plugin, error) {
i := 0
plugins := make([]*sdk.Plugin, len(binPaths))
for name, binPath := range binPaths {
plugin, err := loadPlugin(name, binPath)
if err != nil {
return nil, fmt.Errorf("unable to load plugin %s: %s", name, err)
}

plugins[i] = plugin
i++
}

return plugins, nil
}

/*
TODO: de-wrap loadPlugins and put housekeeping somewhere else
Load all of the plugins described by plugin blocks from binaries in the init path
This assumes that `init` was called on the directory being worked in, and relies on the existance of a `plugin.json`
*/
func LoadPlugins(initPath string, blocks []pluginBlock) ([]*sdk.Plugin, error) {
b, err := os.ReadFile(path.Join(initPath, "plugin.json"))
if err != nil {
Expand All @@ -191,7 +199,7 @@ func LoadPlugins(initPath string, blocks []pluginBlock) ([]*sdk.Plugin, error) {
return nil, fmt.Errorf("failed to decode binPaths: %s", err)
}

loaded, err := loadPlugins(binPaths, blocks)
loaded, err := loadPlugins(binPaths)
if err != nil {
return nil, fmt.Errorf("failed to load plugins from json: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func cmdinit(ctx *cli.Context) error { // init is a different thing in go
return diags
}

pluginPaths, err := configure.CollectPlugins(initPath, pluginBlocks)
pluginPaths, err := configure.FetchPlugins(initPath, pluginBlocks)
if err != nil {
return err
}
Expand Down

0 comments on commit f1dbe14

Please sign in to comment.