forked from e-m-b-a/emba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_project.sh
executable file
·156 lines (135 loc) · 4.61 KB
/
check_project.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
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
#!/bin/bash
# EMBA - EMBEDDED LINUX ANALYZER
#
# Copyright 2020-2022 Siemens AG
# Copyright 2020-2022 Siemens Energy AG
#
# EMBA comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
# EMBA is licensed under GPLv3
#
# Author(s): Michael Messner, Pascal Eckmann
# Description: Check all shell scripts inside ./helpers, ./modules, emba.sh and itself with shellchecker
STRICT_MODE=1
if [[ "$STRICT_MODE" -eq 1 ]]; then
# shellcheck disable=SC1091
source ./installer/wickStrictModeFail.sh
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
# https://github.com/tests-always-included/wick/blob/master/doc/bash-strict-mode.md
set -e # Exit immediately if a command exits with a non-zero status
set -u # Exit and trigger the ERR trap when accessing an unset variable
set -o pipefail # The return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
set -E # The ERR trap is inherited by shell functions, command substitutions and commands in subshells
shopt -s extdebug # Enable extended debugging
IFS=$'\n\t' # Set the "internal field separator"
trap 'wickStrictModeFail $?' ERR # The ERR trap is triggered when a script catches an error
fi
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BOLD='\033[1m'
NC='\033[0m' # no color
INSTALLER_DIR="./installer"
HELP_DIR="./helpers"
MOD_DIR="./modules"
CONF_DIR="./config"
REP_DIR="./report_templates"
SOURCES=()
MODULES_TO_CHECK_ARR=()
import_config_scripts() {
HELPERS=$(find "$CONF_DIR" -iname "*.sh" 2>/dev/null)
for LINE in $HELPERS; do
if (file "$LINE" | grep -q "shell script"); then
echo "$LINE"
SOURCES+=("$LINE")
fi
done
}
import_helper() {
HELPERS=$(find "$HELP_DIR" -iname "*.sh" 2>/dev/null)
for LINE in $HELPERS; do
if (file "$LINE" | grep -q "shell script"); then
echo "$LINE"
SOURCES+=("$LINE")
fi
done
}
import_reporting_templates() {
REP_TEMP=$(find "$REP_DIR" -iname "*.sh" 2>/dev/null)
for LINE in $REP_TEMP; do
if (file "$LINE" | grep -q "shell script"); then
echo "$LINE"
SOURCES+=("$LINE")
fi
done
}
import_module() {
MODULES=$(find "$MOD_DIR" -iname "*.sh" 2>/dev/null)
for LINE in $MODULES; do
if (file "$LINE" | grep -q "shell script"); then
echo "$LINE"
SOURCES+=("$LINE")
fi
done
}
import_installer() {
MODULES=$(find "$INSTALLER_DIR" -iname "*.sh" 2>/dev/null)
for LINE in $MODULES; do
if (file "$LINE" | grep -q "shell script"); then
echo "$LINE"
SOURCES+=("$LINE")
fi
done
}
check()
{
echo -e "\\n""$ORANGE""$BOLD""Embedded Linux Analyzer Shellcheck""$NC""\\n""$BOLD""=================================================================""$NC"
if ! command -v shellcheck >/dev/null 2>&1; then
echo -e "\\n""$ORANGE""Shellcheck not found!""$NC""\\n""$ORANGE""Install shellcheck via 'apt-get install shellcheck'!""$NC\\n"
exit 1
fi
echo -e "\\n""$GREEN""Run shellcheck on this script:""$NC""\\n"
if shellcheck ./check_project.sh || [[ $? -ne 1 && $? -ne 2 ]]; then
echo -e "$GREEN""$BOLD""==> SUCCESS""$NC""\\n"
else
echo -e "\\n""$ORANGE$BOLD==> FIX ERRORS""$NC""\\n"
MODULES_TO_CHECK_ARR+=("check_project.sh")
fi
echo -e "\\n""$GREEN""Run shellcheck on installer:""$NC""\\n"
if shellcheck ./installer.sh || [[ $? -ne 1 && $? -ne 2 ]]; then
echo -e "$GREEN""$BOLD""==> SUCCESS""$NC""\\n"
else
echo -e "\\n""$ORANGE$BOLD==> FIX ERRORS""$NC""\\n"
MODULES_TO_CHECK_ARR+=("installer.sh")
fi
echo -e "\\n""$GREEN""Load all files for check:""$NC""\\n"
echo "./emba.sh"
import_installer
import_helper
import_config_scripts
import_reporting_templates
import_module
echo -e "\\n""$GREEN""Run shellcheck:""$NC""\\n"
for SOURCE in "${SOURCES[@]}"; do
echo -e "\\n""$GREEN""Run shellcheck on $SOURCE""$NC""\\n"
if shellcheck -P "$HELP_DIR":"$MOD_DIR" -a ./emba.sh "$SOURCE" || [[ $? -ne 1 && $? -ne 2 ]]; then
echo -e "$GREEN""$BOLD""==> SUCCESS""$NC""\\n"
else
echo -e "\\n""$ORANGE""$BOLD""==> FIX ERRORS""$NC""\\n"
MODULES_TO_CHECK_ARR+=("$SOURCE")
fi
done
}
summary() {
if [[ "${#MODULES_TO_CHECK_ARR[@]}" -gt 0 ]]; then
echo -e "\\n\\n""$GREEN$BOLD""SUMMARY:$NC\\n"
echo -e "Modules to check: ${#MODULES_TO_CHECK_ARR[@]}\\n"
for MODULE in "${MODULES_TO_CHECK_ARR[@]}"; do
echo -e "$ORANGE$BOLD==> FIX MODULE: ""$MODULE""$NC"
done
echo -e "$ORANGE""WARNING: Fix the errors before pushing to the EMBA repository!"
fi
}
check
summary