Skip to content

Instantly share code, notes, and snippets.

@kodekracker
Last active December 16, 2024 14:28
Show Gist options
  • Save kodekracker/95c4f3fb0348a2bb9b920ff3f1d2bd51 to your computer and use it in GitHub Desktop.
Save kodekracker/95c4f3fb0348a2bb9b920ff3f1d2bd51 to your computer and use it in GitHub Desktop.
Bash scripting utilities
set +e
set -o noglob
#
# Set Colors
#
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
#
# Headers and Logging
#
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
die() { error "$*" 1>&2; exit 1
}
set -e
# logging functions
log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}
note() {
log Note "$@"
}
warn() {
log Warn "$@" >&2
}
error() {
log ERROR "$@" >&2
exit 1
}
function check_python {
if ! python --version &> /dev/null
then
warn "No python installed in your environment. Need to install python(3.10+) first and run this script again."
return
fi
# python has been installed and check its version
if [[ $(python --version) =~ (([0-9]+)\.([0-9]+)([\.0-9]*)) ]]
then
python_version=${BASH_REMATCH[1]}
python_version_part1=${BASH_REMATCH[2]}
python_version_part2=${BASH_REMATCH[3]}
# the version of python does not meet the requirement
if [ "$python_version_part1" -lt 3 ] || ([ "$python_version_part1" -eq 3 ] && [ "$python_version_part2" -lt 10 ])
then
warn "Better to upgrade python to 3.10+."
return
else
note "python version: $python_version"
fi
else
warn "Failed to parse python version."
return
fi
}
function check_docker {
if ! docker --version &> /dev/null
then
error "Need to install docker(26.1.0+) first and run this script again."
exit 1
fi
# docker has been installed and check its version
if [[ $(docker --version) =~ (([0-9]+)\.([0-9]+)([\.0-9]*)) ]]
then
docker_version=${BASH_REMATCH[1]}
docker_version_part1=${BASH_REMATCH[2]}
docker_version_part2=${BASH_REMATCH[3]}
note "docker version: $docker_version"
# the version of docker does not meet the requirement
if [ "$docker_version_part1" -lt 26 ] || ([ "$docker_version_part1" -eq 26 ] && [ "$docker_version_part2" -lt 1 ])
then
error "Need to upgrade docker package to 26.1.0+."
exit 1
fi
else
error "Failed to parse docker version."
exit 1
fi
}
function check_dockercompose {
if [! docker compose version] &> /dev/null
then
error "Need to install a docker-compose-plugin (https://docs.docker.com/compose/)by yourself first and run this script again."
exit 1
fi
# either docker compose plugin has been installed
if docker compose version &> /dev/null
then
note "$(docker compose version)"
DOCKER_COMPOSE="docker compose"
else
error "Failed to parse docker compose version."
exit 1
fi
}
# Function to read configuration file of .cfg format
function decode_config_file {
local config_file="$1"
declare -A config
if [[ -f "$config_file" ]]; then
while IFS="=" read -r key value; do
# Ignore blank lines and comments
if [[ -z "$key" || "$key" =~ ^# ]]; then
continue
fi
key=$(echo "$key" | xargs) # Trim spaces
value=$(echo "$value" | xargs) # Trim spaces
if [[ ! -z "$key" ]]; then
config[$key]="$value"
fi
done < "$config_file"
else
echo "Error: Configuration file '$config_file' not found!"
exit 1
fi
declare -p config
}
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}
_main() {
exec "$@"
}
# If we are sourced from elsewhere, don't perform any further actions
if ! _is_sourced; then
_main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment