-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·119 lines (105 loc) · 2.89 KB
/
run
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
119
#!/bin/bash
cd "$(dirname "$0")" || exit
RED="\033[2;31m"
GREEN="\033[2;32m"
NC="\033[0m"
echo_green() {
echo -e "${GREEN}$1${NC}"
}
echo_red() {
echo -e "${RED}$1${NC}"
}
echo_separator() {
echo ""
}
check_dependencies() {
if ! which "jq" 1>/dev/null 2>&1
then
echo_red "Failed to run CLI script, 'jq' must be installed"
return 1;
fi
if ! which "pnpm" 1>/dev/null 2>&1
then
echo_red "Failed to run CLI script, 'pnpm' must be installed"
return 1;
fi
}
check_dependencies || exit 1
EXECUTED=0
SCRIPT_NAME=${1:-help}
SCRIPT_BODY=$(jq -c '.scripts."'$SCRIPT_NAME'"' ./cli/cli.json)
[ -z ${RUN_SCRIPT_CALL_SRC+x} ] && IS_RECURSIVE_CALL=false || IS_RECURSIVE_CALL=true
# Recursion / system signals
export RUN_SCRIPT_CALL_SRC=1
trap 'echo_separator; echo_red " - $SCRIPT_NAME interrupted"; exit 130;' INT
# Initial execution information context info
if [[ $IS_RECURSIVE_CALL == false ]]
then
echo_green "============================================================================="
echo_green "= $SCRIPT_NAME"
echo_green "============================================================================="
echo_separator
else
echo "$(tput setaf 2) * $SCRIPT_NAME $(tput sgr 0)"
echo_separator
fi
# Script execution
if [[ $SCRIPT_BODY == "null" ]]
then
echo "Script '$SCRIPT_NAME' was not found"
else
if [[ $(jq "type" <<< $SCRIPT_BODY) = '"object"' ]]
# Script is configuration object
then
SCRIPT_BODY=$(jq ".exec" <<< $SCRIPT_BODY)
if [[ $(jq "type" <<< $SCRIPT_BODY) = '"array"' ]]
# Script is complex array
then
while read -r script;
do
eval $script && EXECUTED=1 || EXECUTED=0
if [[ $EXECUTED == 0 ]]
then
break
fi
done <<< "$(jq -r '.[]' <<< "$SCRIPT_BODY")"
# Script is simple string
else
SCRIPT_BODY=$(jq -r "." <<< $SCRIPT_BODY)
eval $SCRIPT_BODY "${@:2}" && EXECUTED=1
fi
# Script is plain string
else
SCRIPT_BODY=$(jq -r "." <<< $SCRIPT_BODY)
eval $SCRIPT_BODY "${@:2}" && EXECUTED=1
fi
fi
# Script execution resulting info
if [[ $IS_RECURSIVE_CALL == false ]]
then
if [[ "$EXECUTED" == 1 ]]
then
echo_separator
echo_green "============================================================================="
echo_green "= Successfully finished command '$SCRIPT_NAME'"
echo_green "============================================================================="
exit 0
else
echo_separator
echo_red "============================================================================"
echo_red "= Script execution was not successful '$SCRIPT_NAME'"
echo_red "============================================================================="
exit 1
fi
else
if [[ "$EXECUTED" == 1 ]]
then
echo_green " + '$SCRIPT_NAME' successful"
echo_separator
exit 0
else
echo_red " - '$SCRIPT_NAME' failed"
echo_separator
exit 1
fi
fi