github.com/dotlike13/wemix30_go@v1.8.23/swarm/dev/scripts/util.sh (about)

     1  # shared shell functions
     2  
     3  info() {
     4    local msg="$@"
     5    local timestamp="$(date +%H:%M:%S)"
     6    say "===> ${timestamp} ${msg}" "green"
     7  }
     8  
     9  warn() {
    10    local msg="$@"
    11    local timestamp=$(date +%H:%M:%S)
    12    say "===> ${timestamp} WARN: ${msg}" "yellow" >&2
    13  }
    14  
    15  fail() {
    16    local msg="$@"
    17    say "ERROR: ${msg}" "red" >&2
    18    exit 1
    19  }
    20  
    21  # say prints the given message to STDOUT, using the optional color if
    22  # STDOUT is a terminal.
    23  #
    24  # usage:
    25  #
    26  #   say "foo"              - prints "foo"
    27  #   say "bar" "red"        - prints "bar" in red
    28  #   say "baz" "green"      - prints "baz" in green
    29  #   say "qux" "red" | tee  - prints "qux" with no colour
    30  #
    31  say() {
    32    local msg=$1
    33    local color=$2
    34  
    35    if [[ -n "${color}" ]] && [[ -t 1 ]]; then
    36      case "${color}" in
    37        red)
    38          echo -e "\033[1;31m${msg}\033[0m"
    39          ;;
    40        green)
    41          echo -e "\033[1;32m${msg}\033[0m"
    42          ;;
    43        yellow)
    44          echo -e "\033[1;33m${msg}\033[0m"
    45          ;;
    46        *)
    47          echo "${msg}"
    48          ;;
    49      esac
    50    else
    51      echo "${msg}"
    52    fi
    53  }