diff --git a/src/application/collections/linux.yaml b/src/application/collections/linux.yaml index c1c25ab9..9bb002f1 100644 --- a/src/application/collections/linux.yaml +++ b/src/application/collections/linux.yaml @@ -3183,7 +3183,7 @@ functions: parameters: code: |- from pathlib import Path - import os, json + import os, json, sys property_name = '{{ $setting }}' target = json.loads('{{ $jsonValue }}') home_dir = f'/home/{os.getenv("SUDO_USER", os.getenv("USER"))}' @@ -3200,7 +3200,15 @@ functions: continue print(f'Reading file at "{settings_file}".') file_content = file.read_text() - json_object = json.loads(file_content) + if not file_content.strip(): + print('Settings file is empty. Treating it as default empty JSON object.') + file_content = '{}' + json_object = None + try: + json_object = json.loads(file_content) + except json.JSONDecodeError: + print(f'Error, invalid JSON format in the settings file: "{settings_file}".', file=sys.stderr) + continue if property_name not in json_object: print(f'Settings "{property_name}" is not configured.') else: @@ -3215,7 +3223,7 @@ functions: print(f'Successfully configured "{property_name}" to {json.dumps(target)}.') revertCode: |- from pathlib import Path - import os, json + import os, json, sys property_name = '{{ $setting }}' target = json.loads('{{ $jsonValue }}') home_dir = f'/home/{os.getenv("SUDO_USER", os.getenv("USER"))}' @@ -3232,7 +3240,14 @@ functions: continue print(f'Reading file at "{settings_file}".') file_content = file.read_text() - json_object = json.loads(file_content) + if not file_content.strip(): + print(f'Skipping, no need to revert because settings file is empty: "{settings_file}".') + continue + try: + json_object = json.loads(file_content) + except json.JSONDecodeError: + print(f'Error, invalid JSON format in the settings file: "{settings_file}".', file=sys.stderr) + continue if property_name not in json_object: print(f'Skipping, "{property_name}" is not configured.') continue diff --git a/src/application/collections/windows.yaml b/src/application/collections/windows.yaml index 4947d328..1f382778 100644 --- a/src/application/collections/windows.yaml +++ b/src/application/collections/windows.yaml @@ -7919,22 +7919,68 @@ functions: function: RunPowerShell parameters: code: |- - $jsonfile = "$env:APPDATA\Code\User\settings.json" - if (!(Test-Path $jsonfile -PathType Leaf)) { - Write-Host "No updates. Settings file was not at $jsonfile" + $settingKey='{{ $setting }}' + $settingValue={{ $powerShellValue }} + $jsonFilePath = "$($env:APPDATA)\Code\User\settings.json" + if (!(Test-Path $jsonFilePath -PathType Leaf)) { + Write-Host "Skipping, no updates. Settings file was not at `"$jsonFilePath`"." exit 0 } - $json = Get-Content $jsonfile | Out-String | ConvertFrom-Json - $json | Add-Member -Type NoteProperty -Name '{{ $setting }}' -Value {{ $powerShellValue }} -Force - $json | ConvertTo-Json | Set-Content $jsonfile - revertCode: |- - $jsonfile = "$env:APPDATA\Code\User\settings.json" - if (!(Test-Path $jsonfile -PathType Leaf)) { - Write-Error "Settings file could not be found at $jsonfile" -ErrorAction Stop + try { + $fileContent = Get-Content $jsonFilePath -ErrorAction Stop + } catch { + throw "Error, failed to read the settings file: `"$jsonFilePath`". Error: $_" } - $json = Get-Content $jsonfile | ConvertFrom-Json - $json.PSObject.Properties.Remove('{{ $setting }}') - $json | ConvertTo-Json | Set-Content $jsonfile + if ([string]::IsNullOrWhiteSpace($fileContent)) { + Write-Host "Settings file is empty. Treating it as default empty JSON object." + $fileContent = "{}" + } + try { + $json = $fileContent | ConvertFrom-Json + } catch { + throw "Error, invalid JSON format in the settings file: `"$jsonFilePath`". Error: $_" + } + $existingValue = $json.$settingKey + if ($existingValue -eq $settingValue) { + Write-Host "Skipping, `"$settingKey`" is already configured as `"$settingValue`"." + exit 0 + } + $json | Add-Member -Type NoteProperty -Name $settingKey -Value $settingValue -Force + $json | ConvertTo-Json | Set-Content $jsonFilePath + Write-Host "Successfully applied the setting to the file: `"$jsonFilePath`"." + revertCode: |- + $settingKey='{{ $setting }}' + $settingValue={{ $powerShellValue }} + $jsonFilePath = "$($env:APPDATA)\Code\User\settings.json" + if (!(Test-Path $jsonFilePath -PathType Leaf)) { + Write-Host "Skipping, no need to revert because settings file is not found: `"$jsonFilePath`"." + exit 0 + } + try { + $fileContent = Get-Content $jsonFilePath -ErrorAction Stop + } catch { + throw "Error, failed to read the settings file: `"$jsonFilePath`". Error: $_" + } + if ([string]::IsNullOrWhiteSpace($fileContent)) { + Write-Host "Skipping, no need to revert because settings file is empty: `"$jsonFilePath`"." + exit 0 + } + try { + $json = $fileContent | ConvertFrom-Json + } catch { + throw "Error, invalid JSON format in the settings file: `"$jsonFilePath`". Error: $_" + } + if (!$json.PSObject.Properties[$settingKey]) { + Write-Host "Skipping, no need to revert because setting `"$settingKey`" does not exist." + exit 0 + } + if ($json.$settingKey -ne $settingValue) { + Write-Host "Skipping, setting (`"$settingKey`") has different configuration than `"$settingValue`": `"$($json.$settingKey)`"." + exit 0 + } + $json.PSObject.Properties.Remove($settingKey) + $json | ConvertTo-Json | Set-Content $jsonFilePath + Write-Host "Successfully reverted the setting from file: `"$jsonFilePath`"." - name: RunPowerShell parameters: