# # gl-common.bash -- A library providing common utilities and functions # for Bash scripts, more information in file . # # Feel free to contribute to this project at: # https://github.com/golflima/gl-common.bash # # Copyright 2016-2017 Jérémy Walther (jeremy.walther@golflima.net). # # gl-common.bash is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # gl-common.bash is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with gl-common.bash. If not, see ############## Setup ############## # To setup gl-common.bash in your project GL_COMMON_BASH_PROGRAM_VAR_PREFIX="$1" ############## Constants ############## # Version GL_COMMON_BASH_VERSION='0.2.3+170220'; # Special chars # No color NC=$'\033[0m' # Foreground colors BLACK=$'\033[0;30m'; DARK_GRAY=$'\033[1;30m' RED=$'\033[0;31m'; LIGHT_RED=$'\033[1;31m' GREEN=$'\033[0;32m'; LIGHT_GREEN=$'\033[1;32m' BROWN=$'\033[0;33m'; YELLOW=$'\033[1;33m' BLUE=$'\033[0;34m'; LIGHT_BLUE=$'\033[1;34m' PURPLE=$'\033[0;35m'; LIGHT_PURPLE=$'\033[1;35m' CYAN=$'\033[0;36m'; LIGHT_CYAN=$'\033[1;36m' LIGHT_GRAY=$'\033[0;37m'; WHITE=$'\033[1;37m' # Background colors BG_BLACK=$'\033[40m'; BG_RED=$'\033[41m'; BG_GREEN=$'\033[42m'; BG_BROWN=$'\033[43m'; BG_BLUE=$'\033[44m'; BG_PURPLE=$'\033[45m'; BG_CYAN=$'\033[46m'; BG_LIGHT_GRAY=$'\033[47m'; # Controls CLEAR_BEFORE=$'\033[1K'; CLEAR_ALL=$'\033[2K'; # Fix for old sed versions (Mac OS...) TAB=$'\t'; LF=$'\n'; # Spinner chars used GL_COMMON_BASH_SPINNER_CHARS[0]='|/─\'; GL_COMMON_BASH_SPINNER_CHARS[1]='╵╶╷╴'; GL_COMMON_BASH_SPINNER_CHARS[2]='╀┾╁┽'; GL_COMMON_BASH_SPINNER_CHARS[3]='┤┘┴└├┌┬┐'; GL_COMMON_BASH_SPINNER_CHARS[4]='░▒▓█▓▒ '; GL_COMMON_BASH_SPINNER_CHARS[5]='▄▀'; # shFlags [[ -z "${FLAGS_TRUE+x}" ]] && FLAGS_TRUE=0; [[ -z "${FLAGS_FALSE+x}" ]] && FLAGS_FALSE=1; ############## General functions ############## # Gets value of given variable name, usage: # get_var get_var() { echo -n "$(eval "echo \"\${$1}\"")"; } # Sets value of given variable name, usage: # set_var set_var() { eval "$1=\"$2\""; } # Gets value of variable prefix used by gl-common.bash to distinguish caller's variables, usage: # gl_common_get_var_prefix gl_common_get_var_prefix() { get_var GL_COMMON_BASH_PROGRAM_VAR_PREFIX; } # Sets value of variable prefix used by gl-common.bash to distinguish caller's variables, usage: # gl_common_get_var_prefix gl_common_set_var_prefix() { set_var GL_COMMON_BASH_PROGRAM_VAR_PREFIX "$1"; } # Gets the content of a prefixed variable name, usage: # gl_common_get_var gl_common_get_var() { get_var "${GL_COMMON_BASH_PROGRAM_VAR_PREFIX}$1"; } # Sets the content of a prefixed variable name, usage: # gl_common_set_var gl_common_set_var() { set_var "${GL_COMMON_BASH_PROGRAM_VAR_PREFIX}$1" "$2"; } # Escapes piped value, usage: # escape_piped escape_piped() { sed -e 's/"/\\"/g' < /dev/stdin; } # Echoes piped content, usage: # echo_piped # echo_piped echo_piped() { local IFS= content; read -d '' content; echo $@ "${content}"; } # Initializes a variable from a prefixed environment variable, usage: # init_var # init_var init_var() { local env_var="$(get_var "$3$1")" [[ -z "${env_var+x}" ]] && set_var "$1" "$(eval "$2")" || set_var "$1" "${env_var}" } # Displays trace information message $@ in dark gray, usage: # trace trace() { echo -e "${DARK_GRAY}$@${NC}"; } # Displays variable content in dark gray, usage: # trace_var trace_var() { echo -e "${DARK_GRAY}$1\t = $(eval "echo \${$1}")${NC}"; } # Displays debug information message $@ in dark gray, only if flag 'debug' is set, usage: # debug debug() { has_flag "debug" && trace "$@"; } # Displays variable content in dark gray, only if flag 'debug' is set, usage: # debug_var debug_var() { has_flag "debug" && debug_var "$@"; } # Displays information message $@ in light blue, usage: # info info() { echo -e "${LIGHT_BLUE}$@${NC}"; } # Displays success message $@ in green, usage: # success success() { echo -e "${GREEN}$@${NC}"; } # Displays warning message $@ in brown/orange, usage: # warn warn() { echo -e "${BROWN}$@${NC}" >&2; } # Ends the execution, and displays $@ in bold red, usage: # die # die die() { warn "${LIGHT_RED}$@"; exit 1; } # Ends the execution, and displays a last message ($@ if set, 'Done.' otherwise), usage: # end # end end() { [[ -z "$@" ]] && echo -e "${GREEN}Done.${NC}" || echo -e "${GREEN}$@${NC}"; exit 0; } # Displays question message $1 in light purple, usage: # question # question # question question() { case $# in 1) echo -en "${LIGHT_PURPLE}$@${NC}" ;; 2) read -p "${LIGHT_PURPLE}$1${NC}" $2 < /dev/tty ;; 3) read -p "${LIGHT_PURPLE}$1${NC}" -ei "$3" $2 < /dev/tty ;; esac } # Asks user to enter a sensible data in light purple, usage: # password password() { read -sp "${LIGHT_PURPLE}$1${NC}" $2 < /dev/tty; } # Ends the execution if given argument $1 is empty and displays usage of subcommand $2, or global usage if $2 is empty, usage: # require_argument # require_argument require_argument() { [[ -z "$(eval "echo \${$1}")" ]] && usage $2 && echo && die "Missing <$1> argument !"; } # Loads required script file and dies if it is not found or if there is an error, usage: # require_script_file # require_script_file require_script_file() { ! [[ -e "$1" ]] && die "Required file '$1' not found." . "$@" local error_code=$? [[ "${error_code}" > 0 ]] && die "Error when loading file '$1', error_code: ${error_code}." } # Loads required script from URL and dies if it is not found or if there is an error, usage: # require_script_curl # require_script_curl require_script_curl() { local http_code="$(curl_http_code "$1")" [[ "${http_code}" != "200" ]] && die "Error when loading url '$1', http_code: ${http_code}." eval "$(curl -sL "$1")" local error_code=$? [[ "${error_code}" > 0 ]] && die "Error when loading script from '$1', error_code: ${error_code}." } # Ends the execution if given command is not found, usage: # require_command require_command() { [[ -z "$(type -t "$@")" ]] && die "Required command '$@' not found."; } # Ends the execution if given command $1 returns an error and displays debug information, usage: # assert_ok $LINENO assert_ok() { ! $1 && warn "${LIGHT_RED}assert_ok failed: $(gl_common_get_var NAME) v$(gl_common_get_var VERSION), line $2, following command failed (err: $?):" && die "$1"; } # Ends the execution if given command $1 doesn't return an error and displays debug information, usage: # assert_ko $LINENO assert_ko() { $1 && warn "${LIGHT_RED}assert_ko failed: $(gl_common_get_var NAME) v$(gl_common_get_var VERSION), line $2, following command succeed (err: $?):" && die "$1"; } # Ends the execution if given two values $1 and $2 aren't equals and displays debug information, usage: # assert_equals $LINENO assert_equals() { [[ "$1" != "$2" ]] && warn "${LIGHT_RED}assert_equals failed: $(gl_common_get_var NAME) v$(gl_common_get_var VERSION), line $3, assertion failed:" && die "Expected: '$1'\nActual: '$2'"; } # Ends the execution if given two values $1 and $2 are equals and displays debug information, usage: # assert_notequals $LINENO assert_notequals() { [[ "$1" = "$2" ]] && warn "${LIGHT_RED}assert_notequals failed: $(gl_common_get_var NAME) v$(gl_common_get_var VERSION), line $3, assertion failed:" && die "Unexpected: '$1'\nActual: '$2'"; } # Checks if given variable is set or not, usage: # is_set is_set() { return "$([[ -n "$(get_var "$1+x")" ]]; echo "$?";)"; } # Checks if given variable is set and empty, usage: # is_empty is_empty() { is_set "$1" && return "$([[ -z "$(get_var "$1")" ]]; echo "$?";)" || return 1; } # Removes all colors, should be called when option --no-color (-c) is used, usage: # remove_colors # [[ has_option 'c/no-color' ]] && remove_colors remove_colors() { NC=; BLACK=; DARK_GRAY=; RED=; LIGHT_RED=; GREEN=; LIGHT_GREEN=; BROWN=; YELLOW=; BLUE=; LIGHT_BLUE=; PURPLE=; LIGHT_PURPLE=; CYAN=; LIGHT_CYAN=; LIGHT_GRAY=; WHITE=; BG_BLACK=; BG_RED=; BG_GREEN=; BG_BROWN=; BG_BLUE=; BG_PURPLE=; BG_CYAN=; BG_LIGHT_GRAY=; } # Checks if an option is present, usage: # has_option "$@" # has_option