-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter-codingstyle
113 lines (99 loc) · 3.52 KB
/
better-codingstyle
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
#!/bin/bash
# Définition des couleurs
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
function my_readlink() {
cd "$1" || exit 1
pwd
cd - > /dev/null || exit 1
}
function cat_readme() {
echo ""
echo "Usage: $(basename "$0") DELIVERY_DIR REPORTS_DIR"
echo -e "\tDELIVERY_DIR\tShould be the directory where your project files are"
echo -e "\tREPORTS_DIR\tShould be the directory where we output the reports"
echo -e "\t\t\tTake note that existing reports will be overridden"
echo ""
}
if [ "$#" -eq 1 ] && [ "$1" == "--help" ]; then
cat_readme
elif [ "$#" -eq 1 ] || [ "$#" -eq 2 ]; then
if [ "$#" -eq 1 ]; then
REPORTS_DIR=$(my_readlink ".")
else
REPORTS_DIR=$(my_readlink "$2")
fi
DELIVERY_DIR=$(my_readlink "$1")
DOCKER_SOCKET_PATH="/var/run/docker.sock"
HAS_SOCKET_ACCESS=$(test -r "$DOCKER_SOCKET_PATH"; echo "$?")
GHCR_REGISTRY_TOKEN=$(curl -s "https://ghcr.io/token?service=ghcr.io&scope=repository:epitech/coding-style-checker:pull" | grep -o '"token":"[^"]*' | grep -o '[^"]*$')
GHCR_REPOSITORY_STATUS=$(curl -I -f -s -o /dev/null -H "Authorization: Bearer $GHCR_REGISTRY_TOKEN" "https://ghcr.io/v2/epitech/coding-style-checker/manifests/latest" && echo 0 || echo 1)
BASE_EXEC_CMD="docker"
# Vérification de l'accès au socket Docker
if [ "$HAS_SOCKET_ACCESS" -ne 0 ]; then
echo -e "${YELLOW}WARNING: Socket access is denied${NC}"
echo "To fix this, we will add the current user to the docker group: sudo usermod -a -G docker $USER"
read -rp "Do you want to proceed? (yes/no) " yn
case $yn in
yes|y|Y|Yes|YES)
echo "Adding user to docker group..."
sudo usermod -a -G docker "$USER"
echo -e "${YELLOW}You must reboot your computer for the changes to take effect.${NC}"
;;
no|n|N|No|NO)
echo "Skipping..."
;;
*)
echo "Invalid response. Skipping..."
;;
esac
BASE_EXEC_CMD="sudo ${BASE_EXEC_CMD}"
fi
# Mise à jour de l'image Docker si accessible
if [ "$GHCR_REPOSITORY_STATUS" -eq 0 ]; then
echo "Downloading new image and cleaning old one..."
$BASE_EXEC_CMD pull ghcr.io/epitech/coding-style-checker:latest && $BASE_EXEC_CMD image prune -f
echo "Download OK"
else
echo -e "${YELLOW}WARNING: Skipping image download${NC}"
fi
# Exécution du checker de coding style
$BASE_EXEC_CMD run --rm --security-opt "label:disable" -i -v "$DELIVERY_DIR":"/mnt/delivery" -v "$REPORTS_DIR":"/mnt/reports" ghcr.io/epitech/coding-style-checker:latest "/mnt/delivery" "/mnt/reports"
# Affichage des résultats triés par fichier avec couleurs
if [ -f "$REPORTS_DIR/coding-style-reports.log" ]; then
echo -e "\nCoding Style Report:\n"
declare -A report_map
while IFS= read -r line; do
file=$(echo "$line" | cut -d':' -f1)
error_code=$(echo "$line" | cut -d':' -f3)
error_type=$(echo "$line" | cut -d':' -f2)
case $error_type in
INFO)
color=$BLUE
;;
MINOR)
color=$YELLOW
;;
MAJOR)
color=$RED
;;
*)
color=$NC
;;
esac
report_map["$file"]+="${error_code} : ${line}${NC}\n"
done < "$REPORTS_DIR/coding-style-reports.log"
# Affichage des fichiers triés et erreurs associées
for file in "${!report_map[@]}"; do
echo -e "${report_map[$file]}"
done
rm -f "$REPORTS_DIR/coding-style-reports.log"
else
echo -e "${GREEN}No coding style errors reported.${NC}"
fi
else
cat_readme
fi