Skip to content

Instantly share code, notes, and snippets.

@cernoel
Last active May 31, 2023 13:54
Show Gist options
  • Save cernoel/46292b97aeacef721d76a90780f532dd to your computer and use it in GitHub Desktop.
Save cernoel/46292b97aeacef721d76a90780f532dd to your computer and use it in GitHub Desktop.
(unsecure, easy) Makefile alternative for sh
#!/bin/sh
set -e
DEFAULT=help
#########################
## your functions here ##
# #
help() {
echo "=================== help ========================"
echo "help - run this help"
echo "build - builds something via Makefile, requires make"
echo "checkout - checkout repo and submodules"
echo "deploy_dev - deploy something (dummy)"
echo "================================================="
}
checkout() {
git checkout develop
git submodule update --recursive --remote
}
build () {
# this requires make, check if installed
check_command_exists make
# also check if Makefile exists
check_file_exists ./Makefile
# then build
make build
}
deploy_dev() {
DEV_HOSTS="host1.my.domain, host2.my.domain"
iterate_over_and_run debug $DEV_HOSTS
}
# #
## end of your functions ##
###########################
################################
## your helper functions here ##
# #
iterate_over_and_run() {
#param1: method
#param2: array
echo "function: ${1}"
echo "array: ${@:2}"
array_to_iterate_over=${@:2}
if [ "x${1}" == "x" ]; then
echo "missing method and array"
return
fi;
for i in ${array_to_iterate_over//,/ }
do
$1 $i
done
}
check_command_exists() {
if ! command -v $1 &> /dev/null
then
echo "$1 could not be found"
exit
fi
}
check_file_exists() {
if [ ! -f "$1" ]; then
echo "$1 does not exist."
exit 1
fi
}
debug() {
echo "debug: $1"
}
# #
## end of your helper functions ##
##################################
run() {
if [ -e .env ]; then
echo "found .env, sourcing it"
. ./.env
fi;
# source run file
. ./run.sh --source-only
# run default function or run defined one
if [ "x${1}" == "x" ]; then
echo "run default: $DEFAULT"
$DEFAULT
else
$@ "${@:2}"
fi;
}
# this prevents running when sourcing
if [ "${1}" != "--source-only" ]; then
run "${@}"
fi
# References:
# https://stackoverflow.com/questions/12815774/importing-functions-from-a-shell-script
# https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs
# https://stackoverflow.com/questions/27702452/loop-through-a-comma-separated-shell-variable
# https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment