forked from crypto-com/thaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
before_push.sh
executable file
·119 lines (99 loc) · 2.37 KB
/
before_push.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Global function return value
RET_VALUE=0
VERBOSE=0
START_TIME=$(date +%s)
# @argument message
function print_message() {
CURRENT_TIME=$(date +%s)
TIME_ELAPSED=$(( CURRENT_TIME - START_TIME ))
echo "${1} ... (+${TIME_ELAPSED}s)"
}
# @argument Description
function print_step() {
print_message "${1}"
}
# @argument Key
# @argument Value
function print_config() {
print_message "[Config] ${1}=${2}"
}
# @argument Description
function print_error() {
print_message "[ERROR] ${1}"
}
# @argument Execution status (0=success, >=1=fail)
# @argument Description
function print_result() {
if [ x"${1}" = x0 ]; then
print_message "[PASS] ${2}"
else
print_message "[FAIL] ${2}"
fi
}
# @argument Command to test
function check_command_exist() {
set +e
command -v ${1} > /dev/null
if [ x"$?" = "x1" ]; then
print_error "command not found: ${1}"
exit 1
fi
set -e
}
# @argument Command to run
# @argument Command to run under verbose option
function command_suite() {
if [ x"${VERBOSE}" = x0 ]; then
CMD="${1}"
else
CMD="${2}"
fi
print_step "[RUNNING] ${CMD}"
set +e
eval "${CMD}"
RESULT=$?
set -e
print_result "${RESULT}" "${CMD}"
if [ x"${RESULT}" != x0 ]; then
exit "${RESULT}"
fi
}
function help()
{
cat << HELP >&2
Usage:
./before_push.sh [-v]
-v | --verbose Display command execution logs
-h | --help Show help
HELP
exit 0
}
while [ $# -gt 0 ]; do
case "$1" in
-v | --verbose)
VERBOSE=1
shift 1
;;
-h | --help)
help
;;
*)
print_message "Unknown argument: $1"
help
;;
esac
done
check_command_exist "cargo"
check_command_exist "rustup"
RUSTFLAGS="${RUSTFLAGS:-} -D warnings"
command_suite "cargo build --quiet" "cargo build"
command_suite "cargo test --quiet" "cargo test"
rustfmt --version > /dev/null || rustup component add rustfmt
command_suite "cargo fmt --quiet -- --check --color=auto" "cargo fmt -- --check --color=auto"
cargo-clippy --version > /dev/null || rustup component add clippy
command_suite "cargo clippy --quiet -- -D warnings" "cargo clippy -- -D warnings"
cargo-audit -h > /dev/null || cargo install cargo-audit
command_suite "cargo audit --quiet" "cargo audit"