FLL/.scripts/precheck.sh

116 lines
3.9 KiB
Bash
Executable file

#!/usr/bin/env bash
set -Eeuo pipefail
RED='\033[31m'
GRE='\033[32m'
YEL='\033[33m'
BLU='\033[34m'
RST='\033[0m'
die() {
echo -e "${RED}${1}${RST}" >&2
exit 1
}
info() {
echo -e "${1}${RST}"
}
ok() {
echo -e "${GRE}${1}${RST}"
}
warn() {
echo -e "${YEL}${1}${RST}"
}
info "Eseguo precheck…"
SCRIPT_PATH="$0"
# Risolvi eventuali symlink manualmente
while [ -h "$SCRIPT_PATH" ]; do
DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)"
SCRIPT_PATH="$(readlink "$SCRIPT_PATH")"
[[ $SCRIPT_PATH != /* ]] && SCRIPT_PATH="$DIR/$SCRIPT_PATH"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)"
EXPECTED_PYTHON="$(cd "$SCRIPT_DIR/../.venv/bin" >/dev/null 2>&1 && pwd)/python"
ACTUAL_PYTHON="$(command -v python || true)"
if [ -z "$ACTUAL_PYTHON" ]; then
warn "Nessun comando 'python' trovato nel PATH. Assicurati che di aver attivato il virtual environment."
fi
if [ ! -x "$EXPECTED_PYTHON" ]; then
warn "virtualenv non trovato nel progetto, segui la guida di installazione." >&2
fi
if [ "$ACTUAL_PYTHON" != "$EXPECTED_PYTHON" ]; then
warn "Attualmente non hai il virtual environment attivo, ricordati di attivarlo!"
fi
FILE_PATH="${SCRIPT_DIR}/name"
if [ ! -f "$FILE_PATH" ]; then
die "Il file col proprio nome non esiste.\n${BLU}crearlo col seguente comando: $GRE printf '<tuonome>' > .scripts/name\n${BLU}Naturalmente devi sostituire $YEL<tuonome>$BLU con ${YEL}il tuo nome$BLU, lo stesso che usi per autentigarti su git.latentspace.cloud.${NC}"
fi
CONTENT="$(cat "$FILE_PATH")"
# sostituisce a capo con spazi
CONTENT="$(echo "$CONTENT" | tr '\n' ' ' | tr -s ' ')"
# rimuove spazi iniziali e finali
CONTENT="$(echo "$CONTENT" | sed 's/^ *//; s/ *$//')"
if [[ "$CONTENT" == "" ]]; then
die "Errore: $YEL.scripts/name$RED esite ma non contiene il tuo nome."
fi
if [[ "$CONTENT" == "<tuonome>" ]]; then
die "Non hai sostituito il tuo nome nel comando 🙄, rieseguilo ma sostituendo $YEL<tuonome>$RED con ${YEL}il tuo nome$RED."
fi
if [[ "$CONTENT" == \<* && "$CONTENT" == *\> ]]; then
die "Naturalmente devi rimuovere le parentesi angolate (<>) ... pensavo fosse ovvio 🙄."
fi
if [[ ! "$CONTENT" =~ ^[a-z]+$ ]]; then
die "Il nome inserito ($YEL${CONTENT}$RED) non è valido, può contenere solo lettere minuscole bro."
fi
# isrepo
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || die "Non sembra una repository Git."
# uv
command -v uv >/dev/null 2>&1 || die "Comando 'uv' non trovato nel PATH. Installalo con '${YEL}curl -LsSf https://astral.sh/uv/install.sh | sh$RED'."
ok "uv trovato."
# ruff
command -v ruff >/dev/null 2>&1 || die "Comando 'ruff' non trovato nel PATH. Installalo con '${YEL}uv tool install ruff$RED'."
ok "ruff trovato."
# git name
GIT_NAME="$(git config --get user.name || true)"
[[ -n "${GIT_NAME}" ]] || die "git user.name non impostato. Esegui: git config user.name '<tuonome>'"
if [[ "$GIT_NAME" != "$CONTENT" ]]; then
die "git user.name non corrisponde al tuo nome impostato in .scripts/name. Assicurati che sia l'username che usi per accedere a git.latentspace.cloud"
fi
ok "git user.name: \t$BLU${GIT_NAME}$RST"
# git mail
GIT_EMAIL="$(git config --get user.email || true)"
[[ -n "${GIT_EMAIL}" ]] || die "git user.email non impostato. Esegui: git config user.email '<tuonome>@latentspace.cloud'"
# git mail latentspace
if [[ ! "${GIT_EMAIL}" =~ @latentspace\.cloud$ ]]; then
die "git user.email non valido: $YEL'${GIT_EMAIL}'$RED. Deve terminare con $YEL'@latentspace.cloud'$RED."
fi
USER_PART="${GIT_EMAIL%@latentspace.cloud}"
if [[ "$USER_PART" != "$CONTENT" ]]; then
die "git user.email non corrisponde al tuo nome impostato in .scripts/name. Assicurati che sia $YEL<tuonome>@latentspace.cloud$RED, dove$YEL <tuonome>$RED è il nome che usi per autenticarti su git.latentspace.cloud."
fi
ok "git user.email: \t$BLU${GIT_EMAIL}$RST"
ok "Precheck completato."