Week3&4 @SoB'23

Week3&4 @SoB'23

In this blog, I would like to walk you through an exciting phase of development where I managed to fine-tune an integral component of our project, the mempool-cli. This was a productive phase, and it played a pivotal role in solidifying my foundation in CLI development.

Enhancements to mempool-cli:

1. Multi-Command Support & Unit Testing:

The first upgrade was to augment the existing command line interface with support for multiple commands. This was achieved by integrating subcommands through the kotlinx-cli library. The integration was seamless, and it opened up a new spectrum of options for the tool.

In addition, I was tasked by my mentor to implement an interface called IMempoolClient. A mempoolClient class is needed to implement this interface. The salient point here was that the subcommands should be instantiated with an IMempoolClient instead of a concrete MempoolClient. This promotes the Dependency Inversion principle, a core tenet of SOLID principles in object-oriented design, and makes the code more testable and maintainable.

For unit testing these subcommands, I leveraged mock implementations of the IMempoolClient. This ensured that the tests were isolated from the actual mempool.space API calls and purely checked the integrity and interaction of the commands with the IMempoolClient.

2. Autocompletion Integration:

The second task, which was more challenging for me since it was uncharted territory, involved adding autocompletion capabilities to the CLI tool. This enhances user experience as they can use the Tab key for command suggestions.

For this, I employed the built-in autocompletion functionalities provided by popular shells like bash and zsh. For bash, I penned a script that programmatically specified how the autocompletion should function for specific commands.

Here’s a more technical explanation: Bash autocompletion scripts typically utilize the compgen and complete built-in commands. compgen generates completion matches, while complete is employed to define the autocompletion behavior for distinct commands.

Below is a simplified example:

_kotlin_cli_completion() {
    local cur prev commands opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    commands="fetchBlockId fetchTxIds"

    # If the user is typing the command (not the options)
    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
        return 0
    fi

    # If the user is typing the options for one of the commands
    case "${prev}" in
        fetchBlockId|fetchTxIds)
            opts="--since --limit"
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            ;;
        *)
            ;;
    esac
}
complete -F _kotlin_cli_completion mempool-cli

I sourced this script into my .bashrc file, and the CLI tool now splendidly auto-completed the commands and options.

It was truly satisfying to witness my commands being automatically completed by simply pressing the Tab key.

Here's an example of using the mempool-cli with auto-completion in the terminal:

claddy@claddy:~$ mempool-cli fetchBlockId -s 730000
0000000000000000000384f28cb3b9cf4377a39cfd6c29ae9466951de38c0529

This progress can be reviewed in this Pull Request: github.com/claddyk/MempoolCLI/pull/5.

What’s next? The Real Deal – Eclair-CLI:

Having completed the improvised version of mempool-cli, my attention now turns to the development of eclair-cli. This is where the true challenge lies, as this project has the potential to leave an impact on the lives of thousands(if not millions). The gravity of this undertaking is not lost on me, and I'm keen to apply the knowledge gained from mempool-cli to this larger venture.

For those interested in contributing or following the development, the repository can be found here: github.com/ACINQ/eclair-cli.

In conclusion, the learning curve has been steep but rewarding. Through understanding interface abstractions, unit testing, and scripting for autocompletion, I have gathered valuable insights into CLI development and object-oriented programming principles. This will prove invaluable as I progress into the development of eclair-cli.

Thanks for reading!
Have a good day/night :)