Path: blob/main/docs/versioned_docs/version-v0.26/plugins/02-dev-plugins.md
1690 views
------Developing Plugins
It's easy to create a plugin and use it immediately in your project. First choose a directory outside your project and run :
This will create a new directory my-plugin that contains the plugin's code, and will output some instructions about how to use your plugin with the ignite command. Indeed, a plugin path can be a local directory, which has several benefits:
you don't need to use a git repository during the development of your plugin.
the plugin is recompiled each time you run the
ignitebinary in your project, if the source files are older than the plugin binary.
Thus, the plugin development workflow is as simple as :
scaffold a plugin with
ignite plugin scaffold my-pluginadd it to your config via
ignite plugin add -g /path/to/my-pluginupdate plugin code
run
ignite my-pluginbinary to compile and run the plugin.go back to 3.
Once your plugin is ready, you can publish it to a git repository, and the community can use it by calling ignite plugin add github.com/foo/my-plugin.
Now let's detail how to update your plugin's code.
The plugin interface
The ignite plugin system uses github.com/hashicorp/go-plugin under the hood, which implies to implement a predefined interface:
The code scaffolded already implements this interface, you just need to update the methods' body.
Defining plugin's manifest
Here is the Manifest struct :
In your plugin's code, the Manifest method already returns a predefined Manifest struct as an example. Adapt it according to your need.
If your plugin adds one or more new commands to ignite, feeds the Commands field.
If your plugin adds features to existing commands, feeds the Hooks field.
Of course a plugin can declare Commands and Hooks.
A plugin may also share a host process by setting SharedHost to true. SharedHost is desirable if a plugin hooks into, or declares long running commands. Commands executed from the same plugin context interact with the same plugin server. Allowing all executing commands to share the same server instance, giving shared execution context.
Adding new command
Plugin commands are custom commands added to the ignite cli by a registered plugin. Commands can be of any path not defined already by ignite. All plugin commands will extend of the command root ignite.
For instance, let's say your plugin adds a new oracle command to ignite scaffold, the Manifest() method will look like :
To update the plugin execution, you have to change the plugin Execute command, for instance :
Then, run ignite scaffold oracle to execute the plugin.
Adding hooks
Plugin Hooks allow existing ignite commands to be extended with new functionality. Hooks are useful when you want to streamline functionality without needing to run custom scripts after or before a command has been run. this can streamline processes that where once error prone or forgotten all together.
The following are hooks defined which will run on a registered ignite commands
| Name | Description |
|---|---|
| Pre | Runs before a commands main functionality is invoked in the PreRun scope |
| Post | Runs after a commands main functionality is invoked in the PostRun scope |
| Clean Up | Runs after a commands main functionality is invoked. if the command returns an error it will run before the error is returned to guarantee execution. |
Note: If a hook causes an error in the pre step the command will not run resulting in post and clean up not executing.
The following is an example of a hook definition.
Above we can see a similar definition to Command where a hook has a Name and a PlaceHookOn. You'll notice that the Execute* methods map directly to each life cycle of the hook. All hooks defined within the plugin will invoke these methods.