Skip to content

Instantly share code, notes, and snippets.

@laidback
Last active September 4, 2019 22:00
Show Gist options
  • Select an option

  • Save laidback/7fbb93d62fe7bf6683d9e7e1744bdc08 to your computer and use it in GitHub Desktop.

Select an option

Save laidback/7fbb93d62fe7bf6683d9e7e1744bdc08 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Filename: ${filename}
# Description: ${filedesc}
# Maintainer: Lukas Ciszewski <lukas.ciszewski@gmail.com>
# set shell options
# * fail immediately on the first error occuring
# * allow clean trapping for ERR
# * fail also if the call to nested functions fails
# * fail if unset variables are used
set -o errexit
set -o errtrace
set -o pipefail
set -o nounset
#set -o allexport
#set -o noglob
[ "${TRACE:-}" ] && set -o xtrace
[ "${VERBOSE:-}" ] && set -o verbose
# global exports and path settings
export PATH=$PATH
# set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename "${__file}" .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)"
# check if we are already loaded
MAIN_LOADED=${MAIN_LOADED:-0}
if [[ $MAIN_LOADED -ne 0 ]]; then echo "main already loaded"; return 0; fi
MAIN_LOADED=1
# check bash version
if [ -z "${BASH_VERSINFO}" ] ||
[ -z "${BASH_VERSINFO[0]}" ] ||
[ "${BASH_VERSINFO[0]}" -lt 4 ]; then
echo "This script requires Bash version >= 4"
exit 1;
fi
# ------------------------------
# err_report
# Globals:
# None
# Arguments:
# None
# Returns:
# None
# ------------------------------
__err_report() #{{{
{
echo "Errexit on line $(caller)"
}
# set the global error reporting trap
trap __err_report ERR
# ------------------------------
# Source other resources
# ------------------------------
# source utils
source "$__dir/utils"
# source conf file
source "$__file.conf"
# source router. After the conf file!
source "$__dir/router"
# ------------------------------
# Main function
# Globals:
# $@
# Arguments:
# argv
# Returns:
# ------------------------------
main() #{{{1
{
# check the number of parameters
if [ "$#" -lt 2 ]; then
log "This script requires minimum two arguments: <command> <resource>"
log "Current environment: $#, $*"
return 1;
fi
# parse command and resource
local cmd=resc=params=""
read -r cmd resc params <<< "$@"
echo "parse params: ${cmd}, ${resc}, ${params}"
# parse params and flags
echo "parse params: ${params}"
# check the PATH for available binaries
echo "check dependencies"
# load resource controller
echo "load resource controller: ${resc}"
#source $(abspath "$RESOURCE_DIR/$resc")
# check if command handler is in the scope
if ! declare -f "$cmd" > /dev/null; then
echo "'$cmd' is not a known function name" >&2
exit 1
fi
# run the command on the resource
log "run cmd: \${cmd} \${resc}" DEBUG
\$cmd\
"\${ms}" \
"\${env}" \
"\${tag}" \
"\${file}" \
"\${params[@]}"
}
# Shell runmode check
if [ "$0" = "$BASH_SOURCE" ]; then
echo "running main"
main "$@"
fi
# vim: set ts=4 sts=4 sw=4 et ft=sh:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment