add script to create and configure json environment file
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
# 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()) {
|
||||
"N" { return $false }
|
||||
default { return $true }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Funzione per chiedere un valore
|
||||
function Ask-Value($key) {
|
||||
return Read-Host "Valore per '$key' (digita `"`" per inserire stringa vuota e lascia vuoto per NON aggiornare)"
|
||||
}
|
||||
|
||||
# Copia profonda preservando ordine
|
||||
$newJson = [ordered]@{}
|
||||
|
||||
foreach ($topKey in $parsedJson.Keys) {
|
||||
|
||||
# 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) {
|
||||
$inputValue = Ask-Value $key
|
||||
|
||||
if ($existingEnv -and $existingEnv["API"] -and $existingEnv["API"].Contains($sectionName)) {
|
||||
$oldValue = $existingEnv["API"][$sectionName][$key]
|
||||
}
|
||||
else {
|
||||
$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."
|
||||
Reference in New Issue
Block a user