In the past, clk contained a command used to add hooks before and after other commands. This was highly advanced stuff and eventually was barely used. It was removed from clk. But, with the plugin mechanism, you actually can put this feature back into clk.
installing a plugin
To install a plugin in clk, you need to place the plugin file in the plugins directory of your clk configuration folder. By default, this is ~/.config/clk/plugins/.
For example, to install the trigger plugin shown below, you would:
- Create the plugins directory if it doesn’t exist:
mkdir -p ~/.config/clk/plugins
- Copy or create the plugin file:
cp myplugin.py ~/.config/clk/plugins/
The plugin will be automatically loaded the next time you run clk.
Note that plugins must have a load_plugin() function that will be called when clk loads them. This function is responsible for initializing the plugin, registering commands, and performing any necessary setup.
Let’s test this with the trigger plugin.
the trigger plugin code
The plugin is kept in clk repository.
To install it try something like:
mkdir -p ~/.config/clk/plugins
wget -O ~/.config/clk/plugins/trigger.py \
https://raw.githubusercontent.com/clk-project/clk/main/contrib/plugin/trigger.py
Once copied in the plugin directory, you should be able to see it listed in the list of plugins…
clk plugin show
trigger Trigger plugin - run commands before/after other commands.
…and use the injected command like any other.
clk trigger --help | head -15
Usage: clk trigger [OPTIONS] COMMAND [ARGS]...
Manipulate command triggers.
Triggers allow you to automatically run commands before or after other commands. This is useful for working around
issues in tools or adding consistent behaviors.
To run command B before command A:
clk trigger set pre A B
To run command B only after successful execution of A:
clk trigger set success A B
using triggers
Now that the plugin is installed, let’s explore how to use triggers effectively. Triggers allow you to automatically run commands before or after other commands, which is useful for adding consistent behaviors or working around issues in tools.
setting and showing a trigger
Let’s start by creating a simple command and setting a pre-trigger on it. First, we’ll create an alias that we can attach a trigger to:
clk alias set mycommand echo 'main command'
Now let’s set a pre-trigger that will run before mycommand:
clk trigger set pre mycommand echo hello
We can verify that the trigger was set by showing it:
clk trigger show --no-color pre mycommand
mycommand echo hello
pre-trigger execution
When we run mycommand, the pre-trigger executes first, followed by the main command:
clk mycommand
hello
main command
As you can see, “hello” appears before “main command”, demonstrating that the pre-trigger runs before the actual command.
success triggers
You can also set triggers that only run after a command succeeds. Let’s create a new command and add a success trigger:
clk alias set buildcmd echo 'build complete'
clk trigger set success buildcmd echo 'after success'
clk buildcmd
build complete
after success
unsetting a trigger
To remove a trigger, use the unset command:
clk trigger unset pre mycommand
After unsetting, only the main command runs:
clk mycommand
main command
listing triggers with –name-only
When you have many triggers, you can list just the command names that have triggers:
clk alias set cmd1 echo 'one'
clk alias set cmd2 echo 'two'
clk trigger set pre cmd1 echo 'trigger1'
clk trigger set pre cmd2 echo 'trigger2'
clk trigger show pre --name-only
buildcmd
cmd1
cmd2