TermFunk: Bridging the gap between Python and the terminal

picture

If you have been a systems engineer or sysadmin for some time you have probably written and gathered a collection of shell scripts to solve your day to day chores. Shell is a powerful language but (arguably) it's syntax is arcane and scripts can become hard to read and maintain once they grow in size and logic.

TermFunk is a lightweight framework to organize, access and execute Python code from the terminal. Its aim is to bridge the gap between Python and your terminal by removing all the necessary boilerplate to access functions and their arguments using auto-completion.

In this article we will create a CLI tool to validate JSON and YAML files using Python and TermFunk.

Prerequisites

  • TermFunk is fully supported when running Bash. You can use TermFunk with other shells but you will probably miss out on most of the auto-completion functionality. (help welcome here!)
  • Basic Python knowledge.

Installation

To install TermFunk:

$ pip install TermFunk

Writing your code

Installing the TermFunk library won't do much on its own. You will have to create a class containing your custom code, which bases the TermFunk library:

  • Each class method starting with function_ is considered to be a sub-command of your script. (line 12, 18)
  • Parameters are automatically mapped to CLI parameters with auto-completion. TermFunk comes with multiple types see https://github.com/smetj/termfunk for more types.

Preparing you bash environment

For auto-completion to work add a variation of the below code to your .bashrc file:

alias validate=/usr/bin/python3 /home/user/python/scripts/validate.py
eval "$(validate complete)"

CLI usage

Each part of the command can be auto-completed:

$ validate json --file some_file.json

A help page containing all valid subcommands (methods) including can be generated:

$ validate --help
usage: validate [-h] {complete,json,yaml} ...

A collection of validation functions.

positional arguments:
  {complete,json,yaml}

optional arguments:
  -h, --help            show this help message and exit

Each sub command has its own help page showing all valid parameters and their type:

$ validate json --help
usage: validate json [-h] [--file FILE]

optional arguments:
  -h, --help   show this help message and exit
  --file FILE  : <File>

Final words

TermFunk can be useful to expose your Python code to the terminal and making it accessible without having to write much boilerplate code. If you find TermFunk useful or have any questions or suggestions don't hesite to get in touch.