From be5deafe8eb7df68d05629d0456f601c00ffd126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B3nimo=20Albi?= Date: Wed, 20 Sep 2023 09:15:46 +0200 Subject: [PATCH] feat: add "app" extension to plugin binaries (#3653) * feat: add "app" extension to plugin binaries * chore: update changelog --------- Co-authored-by: Danilo Pantani --- changelog.md | 1 + ignite/services/plugin/plugin.go | 42 ++++---- ignite/services/plugin/plugin_test.go | 140 +++++++++++++------------- 3 files changed, 93 insertions(+), 90 deletions(-) diff --git a/changelog.md b/changelog.md index 53c1c3ba6d..3d4c238c88 100644 --- a/changelog.md +++ b/changelog.md @@ -14,6 +14,7 @@ - [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1` - [#3522](https://github.com/ignite/cli/pull/3522) Remove indentation from `chain serve` output - [#3601](https://github.com/ignite/cli/pull/3601) Update ts-relayer version to `0.10.0` +- [#3653](https://github.com/ignite/cli/pull/3653) Add "app" extension to plugin binaries - [#3656](https://github.com/ignite/cli/pull/3656) Disable Go toolchain download ### Fixes diff --git a/ignite/services/plugin/plugin.go b/ignite/services/plugin/plugin.go index dfb9d8f27a..d1b66d2dbb 100644 --- a/ignite/services/plugin/plugin.go +++ b/ignite/services/plugin/plugin.go @@ -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 @@ -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 { @@ -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 @@ -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 } @@ -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. @@ -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 @@ -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 @@ -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 } diff --git a/ignite/services/plugin/plugin_test.go b/ignite/services/plugin/plugin_test.go index b59cd33442..36583953a3 100644 --- a/ignite/services/plugin/plugin_test.go +++ b/ignite/services/plugin/plugin_test.go @@ -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", }, }, { @@ -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", }, }, } @@ -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`, @@ -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", } }, }, @@ -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", } }, }, @@ -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", } }, }, @@ -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", } }, }, @@ -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", } }, }, @@ -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`, @@ -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{})) @@ -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)