Clibook: Save and Automate Your Terminal Commands

Working with the terminal can be fun but remembering a long list of repetitive commands isn't. That's where Clibook comes in—a handy CLI tool designed to save your frequently used commands in a centralized location. Simply create, run, or update workflows of commands by name without needing to write separate bash scripts. Let’s dive into how you can install Clibook and explore its functionality in detail.

Why Clibook?

Clibook is your personal terminal assistant that makes the repetitive tasks of working with the command line simpler and more efficient. Here are the main features:

  • Automate repetitive tasks with ease by creating named workflows.

  • No need to remember long command sequences—just write them once and use them by their name.

  • Centralized storage for all your command sequences, making them easily accessible and reusable.

Installation Guide

To install Clibook, follow these steps:

  1. Download the script using curl:
curl -O https://raw.githubusercontent.com/AryanMankame/bashscripts/refs/heads/master/clibook.sh
  1. Make the script executable:
    chmod +x clibook.sh
  1. Move the script to your /usr/local/bin directory to make it globally accessible:

     sudo mv clibook.sh /usr/local/bin/clibook
     # Now, you can run Clibook from anywhere in your terminal by using the command:
     clibook
    

How Clibook Works

Clibook is designed to manage your workflows of commands. It has several commands that you can use:

  • clibook create [workflow-name]: Create a new workflow with a list of commands.

  • clibook update [workflow-name]: Update an existing workflow.

  • clibook run [workflow-name]: Run the specified workflow of commands.

  • clibook delete [workflow-name]: Delete a workflow that you no longer need.

Understanding the Code

1. File Setup

The first part of the script sets up the environment and creates necessary files:

FILE_PATH="$HOME" 
# If the program abruptly stops, remove the temp.json
if [ -f "$FILE_PATH/temp.json" ]; then
    rm "$FILE_PATH/temp.json"
fi

# Create commands.json file if it doesn't exist
if [ ! -f "$FILE_PATH/commands.json" ]; then
    echo "{}" > "$FILE_PATH/commands.json"
fi

This ensures that a file named commands.json exists to store your workflows in JSON format.

2. Creating a Workflow

To create a workflow, Clibook uses the create_workflow function, where you can input your list of commands:

function create_workflow() {
        jq --arg name "$2" '.[$name]=[]' "$FILE_PATH/commands.json" > "$FILE_PATH/temp.json" && mv "$FILE_PATH/temp.json" "$FILE_PATH/commands.json"
        echo "Enter the commands for the workflow:"
        truncate -s 0 "$FILE_PATH/workflow_commands.txt"
        vi "$FILE_PATH/workflow_commands.txt"
        while IFS= read -r line; do
            jq --arg cmd "$line" --arg name "$2" '.[$name] += [$cmd]' "$FILE_PATH/commands.json" > "$FILE_PATH/temp.json" && mv "$FILE_PATH/temp.json" "$FILE_PATH/commands.json"
        done < "$FILE_PATH/workflow_commands.txt"
        rm "$FILE_PATH/workflow_commands.txt"
        echo "Workflow '$2' created successfully!"
    }

The above function prompts the user to enter a series of commands for the workflow and stores them in the JSON file.

3. Running a Workflow

Executing a saved workflow is simple using the run_workflow function:

function run_workflow() {
        if jq -e --arg key "$2" 'has($key) and (.[ $key ] | type == "array")' "$FILE_PATH/commands.json" > /dev/null; then
            truncate -s 0 "$FILE_PATH/run_workflow_commands.sh"
            jq -r --arg key "$2" '.[$key][]' "$FILE_PATH/commands.json" | while IFS= read -r command; do
                echo "$command" >> "$FILE_PATH/run_workflow_commands.sh"
            done
            chmod +x "$FILE_PATH/run_workflow_commands.sh"
            "$FILE_PATH/run_workflow_commands.sh"
            rm "$FILE_PATH/run_workflow_commands.sh"
        else
            echo "Workflow $2 Not Found!"
        fi
}

This function creates a temporary shell script containing the commands of the specified workflow and then runs it.

4. Updating and Deleting Workflows

The functions for updating (update_workflow) and deleting (delete_workflow) workflows allow you to modify or remove workflows with ease, keeping your command library up to date.

Conclusion

Clibook simplifies the way you handle repetitive command sequences in your terminal, making your workflow faster and more efficient. It’s a must-have tool for anyone who regularly works with the command line. Install it, try it out, and watch how it transforms your terminal experience!

Learn More

Check out the Clibook installation script and start automating your terminal commands today!

Did you find this article valuable?

Support We Think Big by becoming a sponsor. Any amount is appreciated!