|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# common util functions. |
| 4 | +# use short namespace `cu`, since these functions will be used frequent. |
| 5 | +# |
| 6 | +#_ source guard start _# |
| 7 | +[ -z "${__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA:+dummy}" ] || return 0 |
| 8 | +__source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" |
| 9 | +readonly __source_guard_B016CBE5_CBB5_4AF4_BE46_ECA9FD30BACA |
| 10 | +#_ source guard end _# |
| 11 | + |
| 12 | +set -eEuo pipefail |
| 13 | + |
| 14 | +################################################################################ |
| 15 | +# simple color print functions |
| 16 | +################################################################################ |
| 17 | + |
| 18 | +cu::color_echo() { |
| 19 | + local color=$1 |
| 20 | + shift |
| 21 | + |
| 22 | + # NOTE: $'foo' is the escape sequence syntax of bash |
| 23 | + local -r ec=$'\033' # escape char |
| 24 | + local -r eend=$'\033[0m' # escape end |
| 25 | + |
| 26 | + # if stdout is the console, turn on color output. |
| 27 | + [ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*" |
| 28 | +} |
| 29 | + |
| 30 | +cu::red_echo() { |
| 31 | + cu::color_echo 31 "$@" |
| 32 | +} |
| 33 | + |
| 34 | +cu::yellow_echo() { |
| 35 | + cu::color_echo 33 "$@" |
| 36 | +} |
| 37 | + |
| 38 | +cu::blue_echo() { |
| 39 | + cu::color_echo 36 "$@" |
| 40 | +} |
| 41 | + |
| 42 | +cu::head_line_echo() { |
| 43 | + cu::color_echo "2;35;46" ================================================================================ |
| 44 | + cu::yellow_echo "$*" |
| 45 | + cu::color_echo "2;35;46" ================================================================================ |
| 46 | +} |
| 47 | + |
| 48 | +################################################################################ |
| 49 | +# validation functions |
| 50 | +################################################################################ |
| 51 | + |
| 52 | +cu::is_number_string() { |
| 53 | + (($# == 1)) || cu::die "${FUNCNAME[0]} need only 1 arguments, actual arguments: $*" |
| 54 | + |
| 55 | + [[ "$1" =~ ^[0-9]+$ ]] |
| 56 | +} |
| 57 | + |
| 58 | +################################################################################ |
| 59 | +# version comparison functions |
| 60 | +# |
| 61 | +# How to compare a program's version in a shell script? |
| 62 | +# https://unix.stackexchange.com/questions/285924 |
| 63 | +################################################################################ |
| 64 | + |
| 65 | +# cu::version_ge <version1> <version2> |
| 66 | +# version1 is greater than or equal to version2 |
| 67 | +cu::version_ge() { |
| 68 | + (($# == 2)) || cu::die "${FUNCNAME[0]} need only 2 arguments, actual arguments: $*" |
| 69 | + |
| 70 | + local ver=$1 |
| 71 | + local destVer=$2 |
| 72 | + |
| 73 | + [ "$ver" = "$destVer" ] && return 0 |
| 74 | + |
| 75 | + [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$destVer" ] |
| 76 | +} |
| 77 | + |
| 78 | +# cu::version_lt <version1> <version2> |
| 79 | +# version1 is less than version2 |
| 80 | +cu::version_lt() { |
| 81 | + (($# == 2)) || cu::die "${FUNCNAME[0]} need only 2 arguments, actual arguments: $*" |
| 82 | + |
| 83 | + local ver=$1 |
| 84 | + local destVer=$2 |
| 85 | + |
| 86 | + [ "$ver" = "$destVer" ] && return 1 |
| 87 | + |
| 88 | + [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ] |
| 89 | +} |
| 90 | + |
| 91 | +################################################################################ |
| 92 | +# execution helper functions |
| 93 | +################################################################################ |
| 94 | + |
| 95 | +cu::loose_run() { |
| 96 | + set +eEuo pipefail |
| 97 | + "$@" |
| 98 | + local exit_code=$? |
| 99 | + set -eEuo pipefail |
| 100 | + return $exit_code |
| 101 | +} |
| 102 | + |
| 103 | +cu::log_then_run() { |
| 104 | + local simple_mode=false |
| 105 | + [ "$1" = "-s" ] && { |
| 106 | + simple_mode=true |
| 107 | + shift |
| 108 | + } |
| 109 | + |
| 110 | + if $simple_mode; then |
| 111 | + echo "Run under work directory $PWD : $*" |
| 112 | + "$@" |
| 113 | + else |
| 114 | + cu::blue_echo "Run under work directory $PWD :" |
| 115 | + cu::blue_echo "$*" 1>&2 |
| 116 | + time "$@" |
| 117 | + fi |
| 118 | +} |
| 119 | + |
| 120 | +cu::die() { |
| 121 | + cu::red_echo "Error: $*" 1>&2 |
| 122 | + exit 1 |
| 123 | +} |
0 commit comments