-
-
Save Ryllaz/07447319d5a5c429ec8eb077a28cfced to your computer and use it in GitHub Desktop.
Template for bootstrap script for any project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE}")")" | |
# ----------------- START OF THE USER FUNCTIONS SECTION ----------------- | |
my_first_command_help() { | |
echo "Build project" | |
} | |
my_first_command() { ## It is my first command | |
echo "doing something important" | |
} | |
# ----------------- END OF THE USER FUNCTIONS SECTION ----------------- | |
# Render help message according to comments for functions | |
__help() { | |
echo "Usage: $BASH_SOURCE [COMMAND] [arg1 arg2 ...]" | |
echo "Available commands:" | |
grep -Eo "^(.*)\(\)(.*)\#\#(.*)$" $BASH_SOURCE | sort | awk 'BEGIN { FS = "\\\(\\\).*## " } { printf "\033[36m%-30s\033[0m %s\n", $1, $2 }' | |
} | |
# Check function existing | |
__function_exists() { | |
declare -f -F "$1" > /dev/null | |
return $? | |
} | |
# Check if function exists and run it | |
if __function_exists "$1"; then | |
if [ "--help" = "${@:2}" ]; then | |
help_func=$1"_help" | |
if __function_exists $help_func; then | |
$help_func | |
else | |
echo "Function '$1' does not have help message" | |
fi | |
else | |
$1 "${@:2}" | |
fi | |
else | |
__help | |
fi |
For new commands you need add this data (carefully check all spaces and # symbols):
my_second_command_help() {
echo "Build project"
}
my_second_command() { ## It is my second command
echo "doing something VERY important"
}
Function with _help
suffix not required, you can skip it if you do not need extra documenting for command.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cd path-to-script-dir/
chmod +x ./run
- allow execute the script./run help
- get help message./run my_first_command
- execute command sample