Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

My favorite command runner setup is just a simple bash script and .envrc

I can put my commands in a run file, which source a simple bash script, and use it like:

    run do-foo
    run build-bar
You can even `run help` to list all available commands.

The setup is explained here: https://olivernguyen.io/w/direnv.run/



Create a bash script `run`:

    #!/bin/bash
    set -eo pipefail

    run-hello() {
        echo "Hello, World!"
    }

    # -------- this is the magic ------- #
    source "scripts/_cli.sh"
    
And source a simple script `_cli.sh`:

    #!/bin/bash
    set -eo pipefail

    show-help(){
        items=()
        while IFS='' read -r line; do items+=("$line"); done < \
            <(compgen -A "function" | grep "run-" | sed "s/run-//")
        printf -v items "\t%s\n" "${items[@]}"

        usage="USAGE: $(basename "$0") CMD [ARGUMENTS]
      CMD:\n$items"
        printf "$usage"
    }

    name=$1
    case "$name" in
        "" | "-h" | "--help" | "help")
            show-help
            ;;
        *)
            shift
            if compgen -A "function" | grep "run-$name" >/dev/null ; then
                run-"${name}" "$@"
            else
                echo "ERROR: run-$name not found."
                exit 123
            fi
            ;;
    esac




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: