Skip to content

Commit

Permalink
feat: add "app" extension to plugin binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeronimoalbi committed Sep 9, 2023
1 parent a920774 commit a21791f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 90 deletions.
42 changes: 22 additions & 20 deletions ignite/services/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ type Plugin struct {
// If any error occurred during the plugin load, it's stored here
Error error

repoPath string
cloneURL string
cloneDir string
reference string
srcPath string
binaryName string
name string
repoPath string
cloneURL string
cloneDir string
reference string
srcPath string

client *hplugin.Client

Expand All @@ -74,15 +74,13 @@ func CollectEvents(ev events.Bus) Option {
// Load loads the plugins found in the chain config.
//
// There's 2 kinds of plugins, local or remote.
// Local plugins have their path starting with a `/`, while remote plugins
// don't.
// Local plugins have their path starting with a `/`, while remote plugins don't.
// Local plugins are useful for development purpose.
// Remote plugins require to be fetched first, in $HOME/.ignite/plugins
// folder, then they are loaded from there.
// Remote plugins require to be fetched first, in $HOME/.ignite/plugins folder, then they are loaded
// from there.
//
// If an error occurs during a plugin load, it's not returned but rather stored
// in the Plugin.Error field. This prevents the loading of other plugins to be
// interrupted.
// If an error occurs during a plugin load, it's not returned but rather stored in the `Plugin.Error`
// field. This prevents the loading of other plugins to be interrupted.
func Load(ctx context.Context, plugins []pluginsconfig.Plugin, options ...Option) ([]*Plugin, error) {
pluginsDir, err := PluginsPath()
if err != nil {
Expand Down Expand Up @@ -140,7 +138,7 @@ func newPlugin(pluginsDir string, cp pluginsconfig.Plugin, options ...Option) *P
return p
}
p.srcPath = pluginPath
p.binaryName = path.Base(pluginPath)
p.name = path.Base(pluginPath)
return p
}
// This is a remote plugin, parse the URL
Expand Down Expand Up @@ -170,7 +168,7 @@ func newPlugin(pluginsDir string, cp pluginsconfig.Plugin, options ...Option) *P
repoSubPath := path.Join(parts[3:]...)

p.srcPath = path.Join(p.cloneDir, repoSubPath)
p.binaryName = path.Base(pluginPath)
p.name = path.Base(pluginPath)

return p
}
Expand All @@ -193,8 +191,12 @@ func (p *Plugin) KillClient() {
}
}

func (p *Plugin) binaryPath() string {
return path.Join(p.srcPath, p.binaryName)
func (p Plugin) binaryName() string {
return fmt.Sprintf("%s.app", p.name)
}

func (p Plugin) binaryPath() string {
return path.Join(p.srcPath, p.binaryName())
}

// load tries to fill p.Interface, ensuring the plugin is usable.
Expand Down Expand Up @@ -229,7 +231,7 @@ func (p *Plugin) load(ctx context.Context) {
}
// pluginMap is the map of plugins we can dispense.
pluginMap := map[string]hplugin.Plugin{
p.binaryName: &InterfacePlugin{},
p.name: &InterfacePlugin{},
}
// Create an hclog.Logger
logLevel := hclog.Error
Expand Down Expand Up @@ -279,7 +281,7 @@ func (p *Plugin) load(ctx context.Context) {
}

// Request the plugin
raw, err := rpcClient.Dispense(p.binaryName)
raw, err := rpcClient.Dispense(p.name)
if err != nil {
p.Error = errors.Wrapf(err, "dispensing")
return
Expand Down Expand Up @@ -341,7 +343,7 @@ func (p *Plugin) build(ctx context.Context) {
p.Error = errors.Wrapf(err, "go mod tidy")
return
}
if err := gocmd.Build(ctx, p.binaryName, p.srcPath, nil); err != nil {
if err := gocmd.Build(ctx, p.binaryName(), p.srcPath, nil); err != nil {
p.Error = errors.Wrapf(err, "go build")
return
}
Expand Down
140 changes: 70 additions & 70 deletions ignite/services/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestNewPlugin(t *testing.T) {
name: "ok: local plugin",
pluginCfg: pluginsconfig.Plugin{Path: path.Join(wd, "testdata")},
expectedPlugin: Plugin{
srcPath: path.Join(wd, "testdata"),
binaryName: "testdata",
srcPath: path.Join(wd, "testdata"),
name: "testdata",
},
},
{
Expand All @@ -77,72 +77,72 @@ func TestNewPlugin(t *testing.T) {
name: "ok: remote plugin",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin",
reference: "",
srcPath: ".ignite/plugins/github.com/ignite/plugin",
binaryName: "plugin",
repoPath: "github.com/ignite/plugin",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin",
reference: "",
srcPath: ".ignite/plugins/github.com/ignite/plugin",
name: "plugin",
},
},
{
name: "ok: remote plugin with @ref",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin@develop"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin@develop",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-develop",
reference: "develop",
srcPath: ".ignite/plugins/github.com/ignite/plugin-develop",
binaryName: "plugin",
repoPath: "github.com/ignite/plugin@develop",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-develop",
reference: "develop",
srcPath: ".ignite/plugins/github.com/ignite/plugin-develop",
name: "plugin",
},
},
{
name: "ok: remote plugin with @ref containing slash",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin@package/v1.0.0"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin@package/v1.0.0",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
reference: "package/v1.0.0",
srcPath: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
binaryName: "plugin",
repoPath: "github.com/ignite/plugin@package/v1.0.0",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
reference: "package/v1.0.0",
srcPath: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
name: "plugin",
},
},
{
name: "ok: remote plugin with subpath",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin/plugin1"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin",
reference: "",
srcPath: ".ignite/plugins/github.com/ignite/plugin/plugin1",
binaryName: "plugin1",
repoPath: "github.com/ignite/plugin",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin",
reference: "",
srcPath: ".ignite/plugins/github.com/ignite/plugin/plugin1",
name: "plugin1",
},
},
{
name: "ok: remote plugin with subpath and @ref",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin/plugin1@develop"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin@develop",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-develop",
reference: "develop",
srcPath: ".ignite/plugins/github.com/ignite/plugin-develop/plugin1",
binaryName: "plugin1",
repoPath: "github.com/ignite/plugin@develop",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-develop",
reference: "develop",
srcPath: ".ignite/plugins/github.com/ignite/plugin-develop/plugin1",
name: "plugin1",
},
},
{
name: "ok: remote plugin with subpath and @ref containing slash",
pluginCfg: pluginsconfig.Plugin{Path: "github.com/ignite/plugin/plugin1@package/v1.0.0"},
expectedPlugin: Plugin{
repoPath: "github.com/ignite/plugin@package/v1.0.0",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
reference: "package/v1.0.0",
srcPath: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0/plugin1",
binaryName: "plugin1",
repoPath: "github.com/ignite/plugin@package/v1.0.0",
cloneURL: "https://github.com/ignite/plugin",
cloneDir: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0",
reference: "package/v1.0.0",
srcPath: ".ignite/plugins/github.com/ignite/plugin-package-v1.0.0/plugin1",
name: "plugin1",
},
},
}
Expand Down Expand Up @@ -202,8 +202,8 @@ func TestPluginLoad(t *testing.T) {
name: "fail: no go files in srcPath",
buildPlugin: func(t *testing.T) Plugin {
return Plugin{
srcPath: path.Join(wd, "testdata"),
binaryName: "testdata",
srcPath: path.Join(wd, "testdata"),
name: "testdata",
}
},
expectedError: `no Go files in`,
Expand All @@ -213,8 +213,8 @@ func TestPluginLoad(t *testing.T) {
buildPlugin: func(t *testing.T) Plugin {
path := scaffoldPlugin(t, t.TempDir(), "github.com/foo/bar", false)
return Plugin{
srcPath: path,
binaryName: "bar",
srcPath: path,
name: "bar",
}
},
},
Expand All @@ -225,10 +225,10 @@ func TestPluginLoad(t *testing.T) {
cloneDir := t.TempDir()

return Plugin{
cloneURL: repoDir,
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote"),
binaryName: "remote",
cloneURL: repoDir,
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote"),
name: "remote",
}
},
},
Expand Down Expand Up @@ -261,11 +261,11 @@ func TestPluginLoad(t *testing.T) {
cloneDir := t.TempDir()

return Plugin{
cloneURL: repoDir,
reference: "v1",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-tag"),
binaryName: "remote-tag",
cloneURL: repoDir,
reference: "v1",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-tag"),
name: "remote-tag",
}
},
},
Expand All @@ -284,11 +284,11 @@ func TestPluginLoad(t *testing.T) {
cloneDir := t.TempDir()

return Plugin{
cloneURL: repoDir,
reference: "branch1",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-branch"),
binaryName: "remote-branch",
cloneURL: repoDir,
reference: "branch1",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-branch"),
name: "remote-branch",
}
},
},
Expand All @@ -302,11 +302,11 @@ func TestPluginLoad(t *testing.T) {
cloneDir := t.TempDir()

return Plugin{
cloneURL: repoDir,
reference: h.Hash().String(),
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-hash"),
binaryName: "remote-hash",
cloneURL: repoDir,
reference: h.Hash().String(),
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-hash"),
name: "remote-hash",
}
},
},
Expand All @@ -318,11 +318,11 @@ func TestPluginLoad(t *testing.T) {
cloneDir := t.TempDir()

return Plugin{
cloneURL: repoDir,
reference: "doesnt_exists",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-no-ref"),
binaryName: "remote-no-ref",
cloneURL: repoDir,
reference: "doesnt_exists",
cloneDir: cloneDir,
srcPath: path.Join(cloneDir, "remote-no-ref"),
name: "remote-no-ref",
}
},
expectedError: `cloning ".*": reference not found`,
Expand All @@ -347,7 +347,7 @@ func TestPluginLoad(t *testing.T) {
require.NotNil(p.Interface)
manifest, err := p.Interface.Manifest()
require.NoError(err)
assert.Equal(p.binaryName, manifest.Name)
assert.Equal(p.name, manifest.Name)
assert.NoError(p.Interface.Execute(ExecutedCommand{}))
assert.NoError(p.Interface.ExecuteHookPre(ExecutedHook{}))
assert.NoError(p.Interface.ExecuteHookPost(ExecutedHook{}))
Expand Down Expand Up @@ -397,9 +397,9 @@ func TestPluginLoadSharedHost(t *testing.T) {
// Load one plugin per instance
for i := 0; i < tt.instances; i++ {
p := Plugin{
Plugin: pluginsconfig.Plugin{Path: path},
srcPath: path,
binaryName: filepath.Base(path),
Plugin: pluginsconfig.Plugin{Path: path},
srcPath: path,
name: filepath.Base(path),
}
p.load(context.Background())
require.NoError(p.Error)
Expand Down

0 comments on commit a21791f

Please sign in to comment.