Skip to content

Some suggestions #4

@tvalladon

Description

@tvalladon

Hello, I have been using this for quite a while and have added some of my own flare to it. I figured I would drop it off and see if you are interested.

Here is an example gist I made for controlling a docker compose infrastructure: https://gist.github.com/tvalladon/e316bee4b58ca082d2190be023565949

The updated help command will check for ## Some Description on the function line and display those descriptions inline (taken and modified from a self documenting Makefile).

./Taskfile <task> <args>
control_example                Test different control pathways <start | stop | restart>
control_example_with_optional  Test different control pathways <start | stop | restart> [service name]
debug_example                  This is an example of using the debug command
help                           Display usage for this application
inline_debug_example           While working on a command debug is useful
no_arg                         No args required
one_arg                        One arg required
two_arg                        Two args required
Task completed in 0m0.002s

The debug can help when coming up with the proper settings or commands to complete the task, for example piping to tr, grep, xarg or checking bash regex in commands like FILENAME=src/test1.txt;cat ${FILENAME} > ${FILENAME/.txt/.bak}.

Let me know if there is anything I can do to help. I tried to toss in some general examples of how I am using the system right now.

I also set the commands to return an exit code if the required args are missing so you can properly chain on success, such as run one arg && run two && run three arg, the run three would not execute if run two required an arg that was missing.

#!/bin/bash
PATH=./node_modules/.bin:$PATH

function debug {
    echo "Stopped in REPL. Press ^D to resume, or ^C to abort."
    local line
    while read -r -p "> " line; do
        eval "$line"
    done
    echo
}

function no_arg { ## No args required
    echo $*
}

function one_arg { ## One arg required
    if [ $# -ne 1 ]; then echo 1>&2 "Usage: $0 $FUNCNAME <arg>";exit 3;fi
    echo $*
}

function two_args { ## Two args required
    if [ $# -ne 2 ]; then echo 1>&2 "Usage: $0 $FUNCNAME <arg> <arg>";exit 3;fi
    echo $*
}

function debug_example { ## This is an example of using the debug command
    debug
}

function control_example { ## Test different control pathways <start | stop | restart>
    if [ $# -ne 1 ]; then echo 1>&2 "Usage: $0 $FUNCNAME <start | stop | restart>";exit 3;fi

    if [ $1 = "start" ]; then
        echo "Starting..."
        echo $*
    fi

    if [ $1 = "stop" ]; then
        echo "Stoping...."
        echo $*
    fi

    if [ $1 = "restart" ]; then
        echo "Restarting..."
        echo $*
        control_example stop && control_example start
    fi
}

function control_example_with_optional { ## Test different control pathways <start | stop | restart> [service name]
    if [ $# -lt 1 ]; then echo 1>&2 "Usage: $0 $FUNCNAME <start | stop | restart> [service name]";exit 3;fi

    if [ $1 = "start" ]; then
        echo "Starting ${2}..."
        echo $*
    fi

    if [ $1 = "stop" ]; then
        echo "Stoping ${2}..."
        echo $*
    fi

    if [ $1 = "restart" ]; then
        echo "Restarting ${2}..."
        echo $*
        control_example_with_optional stop $2 && control_example_with_optional start $2
    fi
}

function inline_debug_example { ## While working on a command debug is useful
    VALUE="testing"
    cd src
    ls
    ls -alth
    echo "if you type \`echo \$VALUE\` in the following REPL you will see the set value of ${VALUE}"
    debug
}

function default {
    help
}

function help { ## Display usage for this application
    echo "$0 <task> <args>"
    grep -E '^function [a-zA-Z_-]+ {.*?## .*$$' $0 | sed -e 's/function //' | sort | awk 'BEGIN {FS = "{.*?## "}; {printf "\033[93m%-30s\033[92m %s\033[0m\n", $1, $2}'
}

TIMEFORMAT="Task completed in %3lR"
time ${@:-default}```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions