From 37300e62a3e961892add1c26fd011906f6088209 Mon Sep 17 00:00:00 2001 From: FrankvanZ <38281243+FrankvanZ@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:33:22 +0100 Subject: [PATCH 1/2] Create Set-LocalEnvFileVariable.ps1 --- .../src/Public/Set-LocalEnvFileVariable.ps1 | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 powershell/src/Public/Set-LocalEnvFileVariable.ps1 diff --git a/powershell/src/Public/Set-LocalEnvFileVariable.ps1 b/powershell/src/Public/Set-LocalEnvFileVariable.ps1 new file mode 100644 index 0000000..14e75eb --- /dev/null +++ b/powershell/src/Public/Set-LocalEnvFileVariable.ps1 @@ -0,0 +1,79 @@ +Set-StrictMode -Version Latest + +<# +.SYNOPSIS + Sets a variable in a Docker environment (.env) file. +.DESCRIPTION + Sets a variable in a Docker environment (.env) file. + Assumes .env file is in the current directory by default. +.PARAMETER Variable + Specifies the variable name. +.PARAMETER Value + Specifies the variable value. +.PARAMETER AsLiteral + Specifies whether the Value should be written as a literal (i.e wrapped in single quotes) +.PARAMETER Path + Specifies the Docker environment (.env) file path. Assumes .env file is in the current directory by default. +.EXAMPLE + PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "value one" +.EXAMPLE + PS C:\> "value one" | Set-EnvFileVariable "VAR1" +.EXAMPLE + PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "value one" -Path .\src\.env +.EXAMPLE + PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "literal $tring" -AsLiteral +.INPUTS + System.String. You can pipe in the Value parameter. +.OUTPUTS + None. +#> +function Set-EnvFileVariable +{ + Param ( + [Parameter(Mandatory = $true, Position = 0)] + [ValidateNotNullOrEmpty()] + [string] + $Variable, + + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [ValidateNotNull()] + [AllowEmptyString()] + [string] + $Value, + + [switch] + $AsLiteral = $false, + + [string] + $Path = ".\.env" + ) + + if (!(Test-Path $Path)) { + throw "The environment file $Path does not exist" + } + + if ($AsLiteral){ + # Escape any ' to avoid terminating the value unexpectedly + $Value = "'$($Value.Replace("'", "''"))'" + } + + $found = $false + + $lines = @(Get-Content $Path -Encoding UTF8 | ForEach-Object { + if ($_ -imatch "^$Variable=.*") { + # Escape any '$' to prevent being used as a regex substitution + $Value = $Value.Replace('$', '$$') + $_ -ireplace "^$Variable=.*", "$Variable=$Value" + $found = $true + } + else { + $_ + } + }) + + if (!$found) { + $lines += "$Variable=$Value" + } + + WriteLines -File (Resolve-Path $Path) -Content $lines -Encoding ([System.Text.Encoding]::UTF8) +} From 74a5a7ea86f334beef990a152322a0cc42c8bbd5 Mon Sep 17 00:00:00 2001 From: FrankvanZ <38281243+FrankvanZ@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:39:31 +0100 Subject: [PATCH 2/2] Update Set-LocalEnvFileVariable.ps1 --- .../src/Public/Set-LocalEnvFileVariable.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/powershell/src/Public/Set-LocalEnvFileVariable.ps1 b/powershell/src/Public/Set-LocalEnvFileVariable.ps1 index 14e75eb..ea28354 100644 --- a/powershell/src/Public/Set-LocalEnvFileVariable.ps1 +++ b/powershell/src/Public/Set-LocalEnvFileVariable.ps1 @@ -2,10 +2,10 @@ Set-StrictMode -Version Latest <# .SYNOPSIS - Sets a variable in a Docker environment (.env) file. + Sets a variable in a Docker environment (.env.local) file. .DESCRIPTION - Sets a variable in a Docker environment (.env) file. - Assumes .env file is in the current directory by default. + Sets a variable in a Docker environment (.env.local) file. + Assumes .env.local file is in the current directory by default. .PARAMETER Variable Specifies the variable name. .PARAMETER Value @@ -13,21 +13,21 @@ Set-StrictMode -Version Latest .PARAMETER AsLiteral Specifies whether the Value should be written as a literal (i.e wrapped in single quotes) .PARAMETER Path - Specifies the Docker environment (.env) file path. Assumes .env file is in the current directory by default. + Specifies the Docker environment (.env.local) file path. Assumes .env.local file is in the current directory by default. .EXAMPLE - PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "value one" + PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "value one" .EXAMPLE PS C:\> "value one" | Set-EnvFileVariable "VAR1" .EXAMPLE - PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "value one" -Path .\src\.env + PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "value one" -Path .\src\.env.local .EXAMPLE - PS C:\> Set-EnvFileVariable -Variable VAR1 -Value "literal $tring" -AsLiteral + PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "literal $tring" -AsLiteral .INPUTS System.String. You can pipe in the Value parameter. .OUTPUTS None. #> -function Set-EnvFileVariable +function Set-LocalEnvFileVariable { Param ( [Parameter(Mandatory = $true, Position = 0)] @@ -45,7 +45,7 @@ function Set-EnvFileVariable $AsLiteral = $false, [string] - $Path = ".\.env" + $Path = ".\.env.local" ) if (!(Test-Path $Path)) {