Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): execute from function #3899

Merged
merged 10 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ignite/services/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@
}
}

// Execute starts and executes a plugin, then shutdowns it.
func Execute(ctx context.Context, path string, args ...string) error {
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
plugins, err := Load(ctx, []pluginsconfig.Plugin{{Path: path}})
if err != nil {
return err
}

Check warning on line 84 in ignite/services/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/plugin.go#L83-L84

Added lines #L83 - L84 were not covered by tests
if plugins[0].Error != nil {
return plugins[0].Error
}
defer plugins[0].KillClient()
return plugins[0].Interface.Execute(ctx, &ExecutedCommand{Args: args}, NewClientAPI())
}

// Load loads the plugins found in the chain config.
//
// There's 2 kinds of plugins, local or remote.
Expand Down
37 changes: 37 additions & 0 deletions ignite/services/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,43 @@ func (TestClientAPI) GetChainInfo(context.Context) (*ChainInfo, error) {
return &ChainInfo{}, nil
}

func TestPluginExecute(t *testing.T) {
tests := []struct {
name string
scaffoldPlugin func(t *testing.T) string
expectedError string
}{
{
name: "fail: plugin doesnt exist",
scaffoldPlugin: func(t *testing.T) string {
return "/not/exists"
},
expectedError: "local app path \"/not/exists\" not found: stat /not/exists: no such file or directory",
},
{
name: "ok: plugin exists",
scaffoldPlugin: func(t *testing.T) string {
path, err := Scaffold(context.Background(), t.TempDir(), "foo", false)
require.NoError(t, err)
return path
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.scaffoldPlugin(t)

err := Execute(context.Background(), path, "args")

if tt.expectedError != "" {
require.EqualError(t, err, tt.expectedError)
return
}
require.NoError(t, err)
})
}
}

func TestPluginLoad(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
Expand Down
Loading