Pet - A Command Line Snippet Manager
Posted on April 27, 2022 by Adrian Wyssmann ‐ 3 min read
I always look to improve the way I work, often also by the help of tools. Github is always a good place to look what's new, and I eventually always find interesting tools which I did not knew before. For example pet, a command line snippet manager.
In my post Cheatsheets and KB" I talked about cool tools already, which help to organize your knowledge and to help with command line. My latest discovery is called pet, a bit similar to kb, but still different. It’s purpose is to store commands you rarely use, in an easy and searchable manner. It offers nice features, like
- store and search commands
- use of variables (
<param>
or<param=default_value>
) in snippets - execute snippets from your collection
- sync to Gist or Gitlab Snippets
- tag snippets
pet - Simple command-line snippet manager.
Usage:
pet [command]
Available Commands:
configure Edit config file
edit Edit snippet file
exec Run the selected commands
help Help about any command
list Show all snippets
new Create a new snippet
search Search snippets
sync Sync snippets
version Print the version number
Flags:
--config string config file (default is $HOME/.config/pet/config.toml)
--debug debug mode
-h, --help help for pet
Use "pet [command] --help" for more information about a command.
You can easily add a new command by using pet new
, which then offers you the option to specify a command and a description:
pet new
Command> echo <output>
Description> Example command
As you can see I parametrized the command, so when I do pet exec
, this will be render output
as an input parameter, before executing the command. So when you run pet exec
![selection dialog for pet exec](https://wyssmann.com/blog/2022/04/pet-a-command-line-snippet-manager/pet-exec-select_hudb858d2ea32f8952885969fcfbd68b60_9078_20x0_resize_box_3.png)
Once you select the command we just added and hit enter, you see a dialog with the command - which you can adjust - and the parameters - in our case output
![pre-execution dialog for pet exec](https://wyssmann.com/blog/2022/04/pet-a-command-line-snippet-manager/pet-exec-dialog_hu5e30fd73063f936a6688b5a5101ba297_8428_20x0_resize_box_3.png)
So this will then use the parameter for your command
![result of the command execution](https://wyssmann.com/blog/2022/04/pet-a-command-line-snippet-manager/pet-exec-result_hu292779d470f2181e95bbc69c6fca74eb_182_20x0_resize_box_3.png)
Sure, there are far much better examples, for instance this command
echo | openssl s_client -connect <domain>:<port=443> 2>/dev/null |openssl x509 -dates -noout
As you can see, we have 2 parameters, one with a default value <port=443>
. That said, you see now why this is very helpful to store (rarely used) commands in such a way.
Another cool thing is, that you can synchronize your snippets to either Gist or Gitlab Snippets. The latter, was not so well documented, hence made created a PR to make it a bit more clear. Once properly configured you can run a pet sync
which synchronizes the snippets - you can also configure to automatically synchronize if you make changes.
![gitlab snippet](https://wyssmann.com/blog/2022/04/pet-a-command-line-snippet-manager/gitlab-snippets_hudf2b809814686236070db8b35feaf69a_38517_20x0_resize_box_3.png)
Tagging is also a nice feature, it basically allows you to add keywords to your commands an later search commands based on the keyword. If you are adding a new command, just use -t
i.e. pet new -t
. For existing commands, you can add them by using pet edit
. So I change my config as follows
[[snippets]]
description = "Show expiration date of SSL certificates"
command = "echo | openssl s_client -connect <domain>:<port=443> 2>/dev/null |openssl x509 -dates -noout"
output = ""
tag = ["ssl"]
[[snippets]]
description = "Example command"
command = "echo <output>"
output = ""
tag = ["example"]
An then run pet exec -t example
, I see the “Example command”
![example command tagged](https://wyssmann.com/blog/2022/04/pet-a-command-line-snippet-manager/pet-tagged-example-command_hu84683f72f4b3ce1398b44da311aef86d_4409_20x0_resize_box_3.png)
The website also describe some functions you can add to your shell, to easily add commands you just used, so have a look and try it out.