Skip to content

πŸ‘’strap your bash project with this handy boilerplate

Notifications You must be signed in to change notification settings

andrei-pavel/bash-boilerplate

Repository files navigation

bash-boilerplate

SVG

Table of Contents

Libraries

All libraries are found inside lib.

Import any library with:

script_path="$(dirname "$(readlink -f "${0}")")"
. "${script_path}/lib/${library}"

They all have import guards to make sure no library is imported twice.

Make sure to import header first. It is a dependency to all other libraries.

header

  • β˜‘ set -euo pipefail
  • β˜‘ print-usage function to print all ${ARGUMENTS}
  • β˜‘ default parameters -c|--config $config_yaml reads the given YAML file and converts all YAML variables to shell variables
  • β˜‘ default parameters -d|--debug calls set -x for debug mode
  • β˜‘ default parameters -h|--help calls print-usage automatically
  • β˜‘ add-to-usage to add a custom message to the usage text
  • β˜‘ additional-arguments function to print arguments other than the default ones above that were given to the script
  • β˜‘ additional-options function to print arguments from the list above that were given to the script
  • β˜‘ ANSII colors ${BLACK}, ${RED}, ${GREEN}, ${YELLOW}, ${BLUE}, ${PURPLE}, ${CYAN}, ${WHITE} are exported
  • β˜‘ argument to define your argument and how it should be parsed
  • β˜‘ allow-extra-parameters to allow more parameters than defined to be used directly in the script
  • β˜‘ parse-parameters to parse the actual parameters
  • ☐ support multiple-word extra parameters

spinner

The best bash spinner out there

  • β˜‘ start-spinner ${text} function to start a spinner with specified ${text}
  • β˜‘ stop-spinner ${exit_code} function to stop the spinner with success if ${exit_code} is 0, with failure otherwise
  • β˜‘ disable-spinners function to ignore commands that start and stop spinners
  • β˜‘ enable-spinners function [default]
  • β˜‘ disable-verbose function to print output on stopped spinner only if exit code was non-null [default]
  • β˜‘ enable-verbose function to print output on stopped spinner all the time
  • β˜‘ configure-spinner-output [full-output|stdout-only|stderr-only|no-output] function
  • ☐ support nested spinners

traps

  • β˜‘ traps [--force|--return-code-only] ${custom_trap} function to set what runs after catching HUP, INT, QUIT, KILL, TERM, EXIT signals
    • usually captures return code, disables traps so that they don't run recursively, stops any spinners and then runs the ${custom_trap}
    • --force makes it so that only ${custom_trap} is run
    • --return-code-only skips over stopping the spinners which is needed in case you're not using spinners

utils

  • β˜‘ confirm [${custom_question}] function to ask the user a yes or no question
  • β˜‘ mandatory ${variable} function to check if ${variable} is set, exit 2 otherwise
  • β˜‘ mandatory-command ${command} function to check if ${command} is available, exit 3 otherwise

yaml-to-env

  • β˜‘ Simply importing it with
. "${script_path}/lib/yaml-to-env [${config_yaml}]"

will read ${config_yaml} or ./config.yaml if it is not provided and will convert all YAML variables to shell variables just like -c|--config $config_yaml, but programatically instead of user-requested.

  • β˜‘ support for a second -c config-yaml in a child script

Example

See the hello-world example which showcases how these features should be used.

Usage: hello-world {{options}} {{arguments}}
Options:
  [-c|--config $config_yaml]   Reads from a YAML configuration and converts all
                               key-value pairs with literal values to
                               environment variables.
  [-d|--debug]                 Enables debug mode, showing every executed
                               statement.
  [-h|--help]                  Prints usage (this text).
Arguments:
  [-a|--approval]              whether to ask user for approval
  -n $count                    print $count times
  [-y|--yaml $yaml]            specify path to the configuration file
  [$string]                    something other than "Hello, world!" to print

About

πŸ‘’strap your bash project with this handy boilerplate

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages