-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.sh
More file actions
executable file
·188 lines (156 loc) · 4.22 KB
/
lib.sh
File metadata and controls
executable file
·188 lines (156 loc) · 4.22 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/sh
cd "$PROJECT_PATH" || exit
ABOUT=""
DELIMITER="<>"
line() {
left="$1"
right="$2"
printf "${NC} %s" "$left"
fill "$left" "$right"
printf "${CYAN}%s\n" "$right"
}
line2() {
left="$1"
right="$2"
printf " %s%s${NC}" "$HEADER_SYMBOL " "$left"
fill "$left" "$right"
printf "%s" "$right"
}
line3() {
left="$1"
right="$2"
width="${3:-$WIDTH}"
filler="${4:-.}"
printf "%s" "$left"
fill "$left" "$right" "$width" "$filler"
printf "%s\n" "$right"
}
# Concatenate the Left/Right strings to the about section data.
add_about_info() {
ABOUT_LEFT="$1"
ABOUT_RIGHT="$2"
if [ -z "$ABOUT" ]; then
ABOUT="$ABOUT_LEFT|$ABOUT_RIGHT"
else
ABOUT="$ABOUT$DELIMITER$ABOUT_LEFT|$ABOUT_RIGHT"
fi
}
# Split the about section data into strings that can be used to generate a new line in the about section.
split_about_info() {
printf '%s\n' "$ABOUT" | tr -s "$DELIMITER" '\n'
}
# Return 1 if the 2nd argument contains the 1st argument. Return 0 otherwise.
contains_string() {
_string="$2"
_substring="$1"
case "$_string" in
*"$_substring"*)
return 0 # Found
;;
*)
return 1 # Not found
;;
esac
}
# Extract the version of an application from a command output.
extract_version() {
if [ $# -eq 0 ]; then
printf "(Unknown)\n"
return 1
fi
version=$(printf "%s" "$1" | sed -n '
# Match either:
# 1. Version numbers preceded by /
# 2. A "v" followed by version numbers after non-digit
# 3. Version numbers preceded by non-digit
# 4. Version numbers at string start
s/^.*\/\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p
t
s/^.*[^0-9]v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p
t
s/^.*[^0-9]\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p
t
s/^v\{0,1\}\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p
')
if [ -z "$version" ]; then
printf "(Unknown)\n"
return 1
fi
printf "%s" "$version"
}
# Fill a line in the terminal with $4 chars until it reaches $WIDTH in length (including the left and right side).
fill() {
left="$1"
right="$2"
width="${3:-$WIDTH}"
filler="${4:-.}"
usedLength=$(printf "%s%s" "$left" "$right" | wc -m)
fillerLength=$((width - usedLength))
i=0
while [ "$i" -lt "$fillerLength" ]; do
printf "%s" "$filler"
i=$((i + 1))
done
}
# Parse the output of npm audit and provide concise information.
parse_npm_audit() {
if [ $# -eq 0 ]; then
printf "Error: No input file specified"
printf "Usage: %s npm-report.json" "$0"
exit 1
fi
auditFile=$(cat "$1")
modules=$(printf '%s' "$auditFile" | jq -r '.vulnerabilities | length')
critical=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.critical)
high=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.high)
moderate=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.moderate)
low=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.low)
total=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.total)
}
# Create a Log file entry according to config value LOG_FORMAT and redirects to config value LOG_FILE
log() {
level="$1"
header="$2"
message="$3"
logFile="${4:-${LOG_FILE}}"
case "$level" in # Validate log level
info|warning|error)
;;
*)
printf "Invalid log level: %s. Must be info, warning, or error" "$level" >&2
return 1
;;
esac
dateTime=$(date "+%Y-%m-%d %H:%M:%S")
logEntry=$(printf "$LOG_FORMAT" "$dateTime" "${level}" "${header}" "$message")
# Pass log output from sed to remove control chars and color codes
printf "%s\n" "$(printf "%s" "$logEntry" | sed -e "s/\x1b\[.\{1,5\}m//g")" >> "$logFile"
}
message() {
level="$1"
titleOrText="$2"
text="$3"
if [ "$#" -eq 2 ] && [ "$SHOW_OUTPUT" = 1 ]; then
printf " %s\n" "$titleOrText"
fi
if [ "$#" -eq 3 ]; then
[ "$SHOW_OUTPUT" = 1 ] && printf " $text\n"
[ "$LOG" = 1 ] && log "$level" "$titleOrText" "$text"
fi
}
pass() {
sek=$((sek-1))
printf "\b%s\n" "$PASS_SYMBOL"
[ -n "$1" ] && message info "${1}" "${2}"
}
warn() {
sek=$((sek-1))
printf "\b%s\n" "$WARN_SYMBOL"
message warning "${1}" "${2}"
}
fail() {
sek=$((sek-1))
printf "\b%s\n" "$FAIL_SYMBOL"
message error "${1}" "${2}"
HAS_ERRORS=1
}