From: Frank Brehm Date: Tue, 23 May 2023 13:52:13 +0000 (+0200) Subject: Modify scripts/get-module-version to use functions.rc X-Git-Tag: 0.9.0~1^2~44 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=7d972d1c6948fefc31a2d0f4c081f85923e7b1bb;p=pixelpark%2Fpp-admin-tools.git Modify scripts/get-module-version to use functions.rc --- diff --git a/scripts/get-module-version b/scripts/get-module-version index 5a5b2a6..7443c8e 100755 --- a/scripts/get-module-version +++ b/scripts/get-module-version @@ -3,100 +3,52 @@ set -e set -u -VERBOSE="n" -DEBUG="n" - -VERSION="0.2" - -# console colors: -RED="" -YELLOW="" -GREEN="" -BLUE="" -NORMAL="" - -HAS_TTY='y' - -BASENAME="$(basename ${0})" -BASE_DIR="$(dirname ${0})" +BASE_NAME="$( basename ${0} )" +MY_REAL_NAME=$( readlink -f $0 ) +BIN_DIR=$( dirname "${MY_REAL_NAME}" ) +BASE_DIR=$( dirname "${BIN_DIR}" ) + +if [[ -f "${BIN_DIR}/functions.rc" ]] ; then + . "${BIN_DIR}/functions.rc" +else + echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2 + exit 5 +fi declare -a MODULES=() PUPPET_ENV='*' ENV_DIR="/etc/puppetlabs/code/environments" -#------------------------------------------------------------------- -detect_color() { - - local safe_term="${TERM//[^[:alnum:]]/?}" - local match_lhs="" - local use_color="false" - local term= - - if [[ -f ~/.dir_colors ]] ; then - match_lhs="${match_lhs}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM *//' -e 's/ .*//')" - fi - if [[ -f /etc/DIR_COLORS ]] ; then - match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | sed -e 's/^TERM *//' -e 's/ .*//')" - fi - if [[ -z ${match_lhs} ]] ; then - type -P dircolors >/dev/null && \ - match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//') - fi - for term in ${match_lhs} ; do - if [[ "${safe_term}" == "${term}" || "${TERM}" == "${term}" || "${TERM}" =~ .*color ]] ; then - use_color="true" - break - fi - done - - # console colors: - if [[ "${use_color}" = "true" ]] ; then - RED="\033[38;5;196m" - YELLOW="\033[38;5;226m" - GREEN="\033[38;5;46m" - # shellcheck disable=SC2034 - BLUE="\033[38;5;27m" - CYAN="\033[38;5;14m" - NORMAL="\033[39m" - else - RED="" - YELLOW="" - GREEN="" - # shellcheck disable=SC2034 - BLUE="" - CYAN="" - NORMAL="" - fi - -} -detect_color +DESCRIPTION=$( cat <<-EOF + Retrieving version number of given Puppet module(s) in all environments. -#------------------------------------------------------------------------------ -description() { - echo -e $( cat <<-EOF - Retrieving version number of given Puppet module(s) in all environments. - - EOF - ) -} + EOF +) #------------------------------------------------------------------------------ usage() { cat <<-EOF - Usage: ${BASENAME} [OPTIONS] [-E ] [] [-D ] [&2 usage >&2 @@ -118,103 +71,65 @@ get_options() { # Note the quotes around `$TEMP': they are essential! eval set -- "${tmp}" + eval_common_options "$@" + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p REMAINING_OPTS + declare -p REMAINING_ARGS + fi + + local len="${#REMAINING_OPTS[*]}" + local i="0" + local j= + local arg= + while [[ "$i" -lt "${len}" ]] ; do - local p= + arg="${REMAINING_OPTS[$i]}" - while true ; do - # echo "Evaluating option '$1' ..." - case "$1" in + case "${arg}" in -E|--env|--environment) - PUPPET_ENV="$2" - shift - shift - ;; - -d|--debug) - DEBUG="y" - shift - ;; - -v|--verbose) - VERBOSE="y" - shift - ;; - --nocolor) - RED="" - YELLOW="" - GREEN="" - BLUE="" - NORMAL="" - shift + j=$(( $i + 1 )) + PUPPET_ENV="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; - -h|--help) - description - usage - exit 0 + -D|--dir) + j=$(( $i + 1 )) + ENV_DIR="${REMAINING_OPTS[$j]}" + i=$(( $i + 2 )) ;; - -V|--version) - echo "${BASENAME} version: ${VERSION}" - exit 0 - ;; - --) shift - break - ;; - *) echo "Internal error!" + *) echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!" exit 1 ;; esac done - if [[ "${DEBUG}" = "y" ]] ; then - set -x - fi - - if [[ ! -d "${ENV_DIR}" ]] ; then - error "Puppet environments directory '${RED}${ENV_DIR}${NORMAL}' does not exists." - exit 5 + if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then + error "Unknown options: ${REMAINING_OPTS[*]}" + echo >&2 + usage >&2 + exit 2 fi - if [[ "$#" -lt 1 ]] ; then + if [[ "${#REMAINING_ARGS[@]}" == "0" ]] ; then error "No module name to search for the version given." - echo + echo >&2 usage >&2 exit 1 - else - local mod - for mod in "$@" ; do - MODULES+=( "${mod}" ) - done fi -} - -######################################### -# Some often used funktions - -#------------------------------------------------------------------------------ -my_date() { - date +'%F %T %:::z' -} - -#------------------------------------------------------------------------------ -debug() { - if [[ "${VERBOSE}" != "y" ]] ; then - return 0 + if [[ ! -d "${ENV_DIR}" ]] ; then + error "Puppet environments directory '${RED}${ENV_DIR}${NORMAL}' does not exists." + exit 5 fi - echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2 -} -#------------------------------------------------------------------------------ -info() { - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2 -} + local mod + for mod in "${REMAINING_ARGS[@]}" ; do + MODULES+=( "${mod}" ) + done -#------------------------------------------------------------------------------ -warn() { - echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 -} + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p MODULES + fi -#------------------------------------------------------------------------------ -error() { - echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 } #------------------------------------------------------------------------------ @@ -274,8 +189,10 @@ get_module_version() { max_len_env=$(( max_len_env + 1 )) for md_file in "${md_files[@]}" ; do - env=$( echo "${md_file}" | sed -e "s|${ENV_DIR}/||" -e 's|/modules/.*/metadata.json||' ) - ver=$( grep --no-filename '"version"' "${md_file}" | sed -e 's/"version"//' -e 's/,//g' -e 's/^[ ]*:[ ]*//g' -e 's/"//g' ) + env=$( echo "${md_file}" | \ + sed -e "s|${ENV_DIR}/||" -e 's|/modules/.*/metadata.json||' ) + ver=$( grep --no-filename '"version"' "${md_file}" | \ + sed -e 's/"version"//' -e 's/,//g' -e 's/^[ ]*:[ ]*//g' -e 's/"//g' ) printf "%-${max_len_env}s ${CYAN}${ver}${NORMAL}\n" "${env}:" done