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
list of commands shown for pet exec

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
pre-execution dialog for the selected command

So this will then use the parameter for your command

result of the command execution
result of the selected command execution

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
pet commands synchronized to Gitlab Snippets

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
Showing all commands with tag 'example'

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.