forked from alibaba/COLA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
78 lines (61 loc) · 1.9 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
[ -z "${__source_guard_309D8FD8_7655_42EE_B32A_9604A082BD9E:+dummy}" ] || return 0
__source_guard_309D8FD8_7655_42EE_B32A_9604A082BD9E=true
set -eEuo pipefail
################################################################################
# constants
################################################################################
# NOTE: $'foo' is the escape sequence syntax of bash
readonly nl=$'\n' # new line
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end
################################################################################
# common util functions
################################################################################
colorEcho() {
local color=$1
shift
# if stdout is the console, turn on color output.
[ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*"
}
redEcho() {
colorEcho 31 "$@"
}
yellowEcho() {
colorEcho 33 "$@"
}
blueEcho() {
colorEcho 36 "$@"
}
headInfo() {
colorEcho "0;34;46" ================================================================================
yellowEcho "$*"
colorEcho "0;34;46" ================================================================================
}
# How to compare a program's version in a shell script?
# https://unix.stackexchange.com/questions/285924
versionLessThan() {
(($# == 2)) || die "${FUNCNAME[0]} need only 2 arguments, actual arguments: $*"
local ver=$1
local destVer=$2
[ "$ver" = "$destVer" ] && return 1
[ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ]
}
logAndRun() {
local simple_mode=false
[ "$1" = "-s" ] && {
simple_mode=true
shift
}
if $simple_mode; then
echo "Run under work directory $PWD : $*"
"$@"
else
blueEcho "Run under work directory $PWD :$nl$*"
time "$@"
fi
}
die() {
redEcho "Error: $*" 1>&2
exit 1
}