-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilt_in.sh
110 lines (101 loc) · 2.47 KB
/
built_in.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
#!/bin/bash
#
# RFSH built-in functions.
# Home: https://github.com/docsion/rfsh
#######################################
# Check require command
# Arguments:
# Command, a string.
# More info url, a string.
# Returns:
# 0 if command exist, 1 on error.
#######################################
rf_require() {
cmd=$1
more=$2
if ! [ -x "$(command -v ${cmd})" ]; then
msg="${cmd} is required."
if [[ ! -z "${more}" ]]; then
msg="${msg} More info: ${more}"
fi
rf_err ${msg}
return 1
fi
}
#######################################
# Write log to stderror
# Arguments:
# Log to show, a string.
# Returns:
# Writes log to stderror, then return 1.
#######################################
rf_err() {
echo "$*" >&2 # print to stderr
}
#######################################
# Assert equals between expected value and actual value
# Arguments:
# Test case name, a string.
# Expected value, any.
# Actual value, any.
# Returns:
# 0 if equals, 1 on error.
#######################################
rf_asserts() {
name=$1
expect=$2
actual=$3
if [[ ! "${expect}" == "${actual}" ]];
then
rf_err Test \"${name}\" failed . Expect "${expect}". Actual "${actual}."
return 1
fi
}
#######################################
# Get $RESULT_IN
# Globals:
# RESULT_IN
# Arguments:
# RESULT_IN, a string.
# Returns:
# String if RESULT_IN exists, 1 on errors.
#######################################
rf_result_in() {
if [[ -z $RESULT_IN ]];
then
rf_err "RESULT_IN is required"
return 1
fi
echo "${RESULT_IN}"
}
#######################################
# Exec curl with -w "%{json}"
# Arguments:
# Curl request likes, a string
# Returns:
# Json -w "%{json}", a string.
# 1 on error.
#######################################
rf_curl() {
rf_require curl https://github.com/curl/curl \
&& curl -o $(mktemp) -s -w "%{json}" "$@"
}
#######################################
# Exec http/https request, curl likes
# Arguments:
# Curl request likes, a string
# Returns:
# Json {response_code, response}.
# - response_code: http response status code, a number, e.g. 200
# - response: response content file location, a string
# 1 on error.
#######################################
rf_http() {
rf_require jq https://github.com/jqlang/jq \
&& rf_curl "$@" \
| jq -c '{"response_code": .response_code} + {"response": .filename_effective}'
return_codes=( "${PIPESTATUS[@]}" )
if (( return_codes[0] != 0 )); then
return ${return_codes[0]}
fi
}