126 lines
4.6 KiB
PowerShell
126 lines
4.6 KiB
PowerShell
# Percorsi file
|
|
$inputJsonPath = Join-Path $PSScriptRoot ".." "env.json.template" # JSON originale
|
|
$outputJsonPath = Join-Path $PSScriptRoot ".." "env.json" # JSON di destinazione
|
|
|
|
# Lettura JSON originale mantenendo l'ordine
|
|
$rawJson = Get-Content $inputJsonPath -Raw
|
|
$parsedJson = $rawJson | ConvertFrom-Json -AsHashtable
|
|
|
|
# Se env.json esiste, lo carico per preservare le sezioni non aggiornate
|
|
$existingEnv = $null
|
|
if (Test-Path $outputJsonPath) {
|
|
$existingEnv = (Get-Content $outputJsonPath -Raw) | ConvertFrom-Json -AsHashtable
|
|
}
|
|
|
|
# Funzione per chiedere Y|S/N
|
|
function Ask-YesNo($message) {
|
|
while ($true) {
|
|
$resp = Read-Host "$message [YySs/Nn]"
|
|
switch ($resp.ToUpper()) {
|
|
"Y" { return $true }
|
|
"S" { return $true }
|
|
default { return $false }
|
|
}
|
|
}
|
|
}
|
|
|
|
# Funzione per chiedere un valore
|
|
function Ask-Value($key, $currentValue) {
|
|
$currentValueHint = ""
|
|
if (-not [string]::IsNullOrEmpty($currentValue)) {
|
|
$currentValueHint = "[attuale: '$currentValue'] "
|
|
}
|
|
return Read-Host "Chiave '$key' $currentValueHint`n(digita `"`" per inserire stringa vuota e lascia vuoto per NON aggiornare)"
|
|
}
|
|
|
|
# Copia profonda preservando ordine
|
|
$newJson = [ordered]@{}
|
|
|
|
foreach ($topKey in $parsedJson.Keys) {
|
|
if ($topKey -eq "Config") {
|
|
# Copia Config senza modifiche, i parametri serviranno dopo la generazione
|
|
$newJson["Config"] = $parsedJson["Config"]
|
|
continue
|
|
}
|
|
|
|
# Sezione API → trattamento speciale
|
|
if ($topKey -eq "API") {
|
|
$newJson["API"] = [ordered]@{}
|
|
|
|
foreach ($sectionName in $parsedJson["API"].Keys) {
|
|
$section = $parsedJson["API"][$sectionName]
|
|
|
|
$shouldConfigure = Ask-YesNo "Vuoi configurare la sezione '$sectionName'?"
|
|
|
|
# Se NON voglio configurare → devo distinguere:
|
|
if (-not $shouldConfigure) {
|
|
Write-Host "Sezione '$sectionName' non configurata. Mantengo valori esistenti, se presenti." -ForegroundColor Yellow
|
|
|
|
# 1. Se NON esiste già env.json → creo sezione vuota uguale al template
|
|
if (-not $existingEnv) {
|
|
$newJson["API"][$sectionName] = $section
|
|
continue
|
|
}
|
|
|
|
# 2. Se esiste già env.json e sia API che la sezione sono presenti → mantengo la versione esistente
|
|
if ($existingEnv["API"] -and $existingEnv["API"].Contains($sectionName)) {
|
|
$newJson["API"][$sectionName] = $existingEnv["API"][$sectionName]
|
|
continue
|
|
}
|
|
|
|
# 3. Se esiste già env.json, ma API o la sezione non sono presenti → creo sezione vuota uguale al template
|
|
$newJson["API"][$sectionName] = $section
|
|
continue
|
|
}
|
|
|
|
# Altrimenti creo una nuova sezione (o aggiorno quella esistente)
|
|
$newSection = [ordered]@{}
|
|
|
|
foreach ($key in $section.Keys) {
|
|
if ($key -eq "_httpyac_only") {
|
|
# Mantieni la chiave speciale senza chiedere input
|
|
$newSection[$key] = $section[$key]
|
|
continue
|
|
}
|
|
|
|
if ($existingEnv -and $existingEnv["API"] -and $existingEnv["API"].Contains($sectionName)) {
|
|
$oldValue = $existingEnv["API"][$sectionName][$key]
|
|
}
|
|
else {
|
|
$oldValue = ""
|
|
}
|
|
|
|
$inputValue = Ask-Value $key $oldValue
|
|
|
|
if ($inputValue -eq "") {
|
|
# Mantieni valore esistente se presente
|
|
$newSection[$key] = $oldValue
|
|
}
|
|
elseif ($inputValue -eq "`"`"") {
|
|
# Aggiorna con stringa vuota
|
|
$newSection[$key] = ""
|
|
}
|
|
else {
|
|
# Aggiorna con nuovo valore
|
|
$newSection[$key] = $inputValue
|
|
}
|
|
}
|
|
|
|
$newJson["API"][$sectionName] = $newSection
|
|
}
|
|
}
|
|
else {
|
|
# Altri elementi allo stesso livello di API → preservati integralmente
|
|
if ($existingEnv -and $existingEnv.Contains($topKey)) {
|
|
$newJson[$topKey] = $existingEnv[$topKey]
|
|
} else {
|
|
$newJson[$topKey] = $parsedJson[$topKey]
|
|
}
|
|
}
|
|
}
|
|
|
|
# Scrittura JSON finale mantenendo l'ordine
|
|
$newJson | ConvertTo-Json -Depth 20 | Set-Content $outputJsonPath -Encoding UTF8
|
|
|
|
Write-Host "`nFile env.json aggiornato correttamente."
|