From 85da97c9a97e2de63446da74cbeaf6c00e498c08 Mon Sep 17 00:00:00 2001 From: jantari Date: Thu, 10 Feb 2022 17:38:39 +0100 Subject: [PATCH] fix(config): Fix `set_config` bugs (#3681) Co-authored-by: Hsiao-nan Cheung --- CHANGELOG.md | 1 + lib/core.ps1 | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2a87a1393..506aad70b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - **autoupdate:** Allow checksum file that contains whitespaces ([#4619](https://github.com/ScoopInstaller/Scoop/issues/4619)) - **autoupdate:** Rename $response to $res ([#4706](https://github.com/ScoopInstaller/Scoop/issues/4706)) - **config:** Allow scoop config use Unicode characters ([#4631](https://github.com/ScoopInstaller/Scoop/issues/4631)) +- **config:** Fix `set_config` bugs ([#3681](https://github.com/ScoopInstaller/Scoop/issues/3681)) - **current:** Remove 'current' while it's not a junction ([#4687](https://github.com/ScoopInstaller/Scoop/issues/4687)) - **depends:** Prevent error on no URL ([#4595](https://github.com/ScoopInstaller/Scoop/issues/4595)) - **depends:** Check if extractor is available ([#4042](https://github.com/ScoopInstaller/Scoop/issues/4042)) diff --git a/lib/core.ps1 b/lib/core.ps1 index 84f033cbf3..d1189c2556 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -54,27 +54,33 @@ function get_config($name, $default) { return $scoopConfig.$name } -function set_config($name, $value) { - if($null -eq $scoopConfig -or $scoopConfig.Count -eq 0) { +function set_config { + Param ( + [ValidateNotNullOrEmpty()] + $name, + $value + ) + + if ($null -eq $scoopConfig -or $scoopConfig.Count -eq 0) { ensure (Split-Path -Path $configFile) | Out-Null - $scoopConfig = New-Object PSObject + $scoopConfig = New-Object -TypeName PSObject + } + + if ($value -eq [bool]::TrueString -or $value -eq [bool]::FalseString) { + $value = [System.Convert]::ToBoolean($value) + } + + if ($null -eq $scoopConfig.$name) { $scoopConfig | Add-Member -MemberType NoteProperty -Name $name -Value $value } else { - if($value -eq [bool]::TrueString -or $value -eq [bool]::FalseString) { - $value = [System.Convert]::ToBoolean($value) - } - if($null -eq $scoopConfig.$name) { - $scoopConfig | Add-Member -MemberType NoteProperty -Name $name -Value $value - } else { - $scoopConfig.$name = $value - } + $scoopConfig.$name = $value } - if($null -eq $value) { + if ($null -eq $value) { $scoopConfig.PSObject.Properties.Remove($name) } - ConvertTo-Json $scoopConfig | Set-Content $configFile -Encoding Default + ConvertTo-Json $scoopConfig | Set-Content -Path $configFile -Encoding Default return $scoopConfig }