- installing a plugin
- the trigger plugin code
- using triggers
- writing one of your own
- the day one stops working
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. -------------- Legend: global
…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 pre mycommand
mycommand echo hello -------------- Legend: global
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 :Context:
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
writing one of your own
Copying a file into the plugins directory works, but clk can lay it out for you, in the profile you name.
clk plugin create global greet --description "Say hello on load"
clk plugin which global greet
./clk-root/plugins/greet.py
Ask for it again and clk will not write over what you have put in it since.
clk plugin create global greet --description "Say hello on load"
Usage: clk plugin create [OPTIONS] PROFILE_SOURCE NEW_NAME
error: Won't overwrite ./clk-root/plugins/greet.py unless explicitly asked so with --force
Their names complete, so you never have to remember them.
clk plugin which global g<TAB>
greet
the day one stops working
A plugin is python like any other, and it leans on what is installed. Remove the library it wanted and it no longer loads.
clk plugin create global notifier --description "Say things out loud"
cat<<'EOF' > "$(clk plugin which global notifier)"
#!/usr/bin/env python3
import notify2
def load_plugin():
"Nothing here, it never gets this far."
EOF
clk names it, says where you would remove it, and gets on with what you asked.
clk echo hello
warning: Error when loading plugin notifier (if the plugin is no more useful, consider uninstalling the plugins notifier): No module named 'notify2'
hello