reecriture validate_config_js
This commit is contained in:
parent
cb43cb75b7
commit
2da84289f1
99
update.sh
99
update.sh
@ -222,77 +222,46 @@ check_conf() {
|
||||
check_conf "$CONF_SH"
|
||||
|
||||
# ────────── VALIDATION config.js (avec Node) ──────────
|
||||
# remplace validate_config_js par :
|
||||
validate_config_js() {
|
||||
[[ -f "$CFG_JS" ]] || { err "Manquant: $CFG_JS"; errors=$((errors+1)); return; }
|
||||
# récupère des paires clé: valeur basiques (sans exécuter)
|
||||
parse() { grep -E "^\s*$1\s*:" "$CFG_JS" | head -n1 | sed -E "s/.*:\s*//; s/[,'\"]//g; s,//.*,,"; }
|
||||
|
||||
log "Validation de $CFG_JS…"
|
||||
local CHECK="$TMP_DIR/check_config.js"
|
||||
cat > "$CHECK" <<'JS'
|
||||
const fs = require('fs');
|
||||
const path = process.argv[2];
|
||||
function isEmptyOrPlaceholder(v){
|
||||
if (v == null) return true;
|
||||
if (typeof v === 'string') {
|
||||
const s = v.trim();
|
||||
if (!s) return true;
|
||||
const P = [/^voir/i, /^change/i, /^changeme/i, /^todo/i, /^example/i, /^your/i, /^\/path\/to/i];
|
||||
if (P.some(rx => rx.test(s))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const cfg = require(path);
|
||||
let errs = [];
|
||||
dbtype="$(parse dbtype)"
|
||||
port="$(parse port)"
|
||||
name="$(parse name)"
|
||||
sessionSecret="$(parse sessionSecret)"
|
||||
DB_TABLE="$(parse DB_TABLE)"
|
||||
finishdirectory="$(parse finishdirectory)"
|
||||
logdirectory="$(parse logdirectory)"
|
||||
infodirectory="$(parse infodirectory)"
|
||||
|
||||
// requis généraux
|
||||
if (!Number.isInteger(cfg.port) || cfg.port < 1 || cfg.port > 65535)
|
||||
errs.push("config.js: 'port' doit être un entier 1-65535");
|
||||
if (isEmptyOrPlaceholder(cfg.name)) errs.push("config.js: 'name' non renseigné");
|
||||
if (isEmptyOrPlaceholder(cfg.sessionSecret)) errs.push("config.js: 'sessionSecret' non renseigné");
|
||||
if (typeof cfg.trustProxy === 'undefined') errs.push("config.js: 'trustProxy' manquant");
|
||||
if (typeof cfg.cookieSecure === 'undefined') errs.push("config.js: 'cookieSecure' manquant");
|
||||
if (isEmptyOrPlaceholder(cfg.DB_TABLE)) errs.push("config.js: 'DB_TABLE' non renseigné");
|
||||
# checks minimaux
|
||||
[[ "$port" =~ ^[0-9]+$ ]] && [ "$port" -ge 1 ] && [ "$port" -le 65535 ] || { err "config.js: port invalide"; errors=$((errors+1)); }
|
||||
[ -n "$name" ] || { err "config.js: name vide"; errors=$((errors+1)); }
|
||||
[[ "$sessionSecret" =~ ^(Voir|change|CHANGE|todo|TODO|example|your|/path/to|)$ ]] && { err "config.js: sessionSecret non renseigné"; errors=$((errors+1)); }
|
||||
[ -n "$DB_TABLE" ] || { err "config.js: DB_TABLE vide"; errors=$((errors+1)); }
|
||||
|
||||
// chemins
|
||||
const pathKeys = ['finishdirectory', 'logdirectory', 'infodirectory', 'sessionStorePath'];
|
||||
for (const k of pathKeys) {
|
||||
if (typeof cfg[k] === 'string' && cfg[k].trim()) {
|
||||
try { if (!fs.existsSync(cfg[k])) errs.push(`config.js: chemin inexistant pour '${k}': ${cfg[k]}`); }
|
||||
catch {}
|
||||
} else if (k !== 'sessionStorePath') {
|
||||
errs.push(`config.js: '${k}' non renseigné`);
|
||||
}
|
||||
}
|
||||
for d in "$finishdirectory" "$logdirectory" "$infodirectory"; do
|
||||
[ -d "$d" ] || { err "config.js: dossier manquant: $d"; errors=$((errors+1)); }
|
||||
done
|
||||
|
||||
// règles conditionnelles dbtype
|
||||
if (!cfg.dbtype || !['sqlite','mysql'].includes(cfg.dbtype)) {
|
||||
errs.push("config.js: 'dbtype' doit être 'sqlite' ou 'mysql'");
|
||||
} else if (cfg.dbtype === 'sqlite') {
|
||||
if (isEmptyOrPlaceholder(cfg.dbFile)) {
|
||||
errs.push("config.js: 'dbFile' requis en mode sqlite");
|
||||
} else {
|
||||
const dir = require('path').dirname(cfg.dbFile);
|
||||
if (!fs.existsSync(dir)) errs.push(`config.js: dossier de 'dbFile' inexistant: ${dir}`);
|
||||
}
|
||||
// champs MySQL facultatifs → ne rien exiger
|
||||
} else if (cfg.dbtype === 'mysql') {
|
||||
// dbFile facultatif → ne rien exiger
|
||||
if (isEmptyOrPlaceholder(cfg.DB_HOST)) errs.push("config.js: 'DB_HOST' requis en mode mysql");
|
||||
if (!Number.isInteger(cfg.DB_PORT)) errs.push("config.js: 'DB_PORT' entier requis en mode mysql");
|
||||
if (isEmptyOrPlaceholder(cfg.DB_USER)) errs.push("config.js: 'DB_USER' requis en mode mysql");
|
||||
if (isEmptyOrPlaceholder(cfg.DB_PASSWORD)) errs.push("config.js: 'DB_PASSWORD' requis en mode mysql");
|
||||
if (isEmptyOrPlaceholder(cfg.DB_DATABASE)) errs.push("config.js: 'DB_DATABASE' requis en mode mysql");
|
||||
}
|
||||
|
||||
if (errs.length) { console.error(errs.join('\n')); process.exit(2); }
|
||||
} catch (e) {
|
||||
console.error(`Impossible de charger ${path}: ${e.message}`);
|
||||
process.exit(2);
|
||||
}
|
||||
JS
|
||||
if ! node "$CHECK" "$CFG_JS"; then
|
||||
errors=$((errors+1))
|
||||
fi
|
||||
case "$dbtype" in
|
||||
sqlite)
|
||||
dbFile="$(parse dbFile)"
|
||||
[ -n "$dbFile" ] && [ -d "$(dirname "$dbFile")" ] || { err "config.js: dbFile requis (sqlite)"; errors=$((errors+1)); }
|
||||
;;
|
||||
mysql)
|
||||
DB_HOST="$(parse DB_HOST)"; DB_PORT="$(parse DB_PORT)"; DB_USER="$(parse DB_USER)"; DB_PASSWORD="$(parse DB_PASSWORD)"; DB_DATABASE="$(parse DB_DATABASE)"
|
||||
[ -n "$DB_HOST" ] || { err "config.js: DB_HOST requis (mysql)"; errors=$((errors+1)); }
|
||||
[[ "$DB_PORT" =~ ^[0-9]+$ ]] || { err "config.js: DB_PORT entier requis (mysql)"; errors=$((errors+1)); }
|
||||
[ -n "$DB_USER" ] || { err "config.js: DB_USER requis (mysql)"; errors=$((errors+1)); }
|
||||
[ -n "$DB_PASSWORD" ] || { err "config.js: DB_PASSWORD requis (mysql)"; errors=$((errors+1)); }
|
||||
[ -n "$DB_DATABASE" ] || { err "config.js: DB_DATABASE requis (mysql)"; errors=$((errors+1)); }
|
||||
;;
|
||||
*) err "config.js: dbtype doit être sqlite ou mysql"; errors=$((errors+1));;
|
||||
esac
|
||||
}
|
||||
validate_config_js
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user