From 7f1260b0dfea876e494cb02d1b07c9318cce363f Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 25 Jul 2023 12:15:28 +0200 Subject: [PATCH] Refactor the manipulation of Nextcloud configurations --- scripts/Windows/Scripts/Context.ps1 | 32 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index 7daf54aa..20b22b39 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -273,26 +273,38 @@ class Context { $nextcloudProcess | Stop-Process -Force; $accountSectionEntered = $false; + $accountSectionLeft = $false; - (Get-Content $($this.GetNextcloudConfigFile())) | ` - ForEach-Object { + $newSettings = [string]::Join( + "`n", + @( + "0\$configName\$folderID\localPath=$localPath", + "0\$configName\$folderID\targetPath=$targetPath")); + + $oldContent = Get-Content ($this.GetNextcloudConfigFile()); + + $( + for ($i = 0; i -lt $oldContent.Count; $i++) { if ($_ -eq "[Accounts]") { $accountSectionEntered = $true; } - if ($_ -eq "" -and $accountSectionEntered) { - [string]::Join( - "`n", - @( - "0\$configName\$folderID\localPath=$localPath", - "0\$configName\$folderID\targetPath=$targetPath")); + if ( + ($_ -eq "" -and $accountSectionEntered) -or + ( + (-not $accountSectionLeft) -and + ($_ -ne "") -and + ($i -eq ($oldContent.Count - 1)) + )) { + $accountSectionLeft = $true; + $newSettings; } $_; - } | Set-Content $this.GetNextcloudConfigFile(); + }) | Set-Content ($this.GetNextcloudConfigFile()); Write-Information "New nextcloud config:"; - Write-Information Get-Content $($this.GetNextcloudConfigFile()); + Write-Information (Get-Content $($this.GetNextcloudConfigFile())); Write-Information "Restarting Nextcloud"; Start-Process $nextcloudPath;