Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions powershell/src/Public/Set-LocalEnvFileVariable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Set-StrictMode -Version Latest

<#
.SYNOPSIS
Sets a variable in a Docker environment (.env.local) file.
.DESCRIPTION
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
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.local) file path. Assumes .env.local file is in the current directory by default.
.EXAMPLE
PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "value one"
.EXAMPLE
PS C:\> "value one" | Set-EnvFileVariable "VAR1"
.EXAMPLE
PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "value one" -Path .\src\.env.local
.EXAMPLE
PS C:\> Set-LocalEnvFileVariable -Variable VAR1 -Value "literal $tring" -AsLiteral
.INPUTS
System.String. You can pipe in the Value parameter.
.OUTPUTS
None.
#>
function Set-LocalEnvFileVariable
{
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$Variable,

[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[AllowEmptyString()]
[string]
$Value,

[switch]
$AsLiteral = $false,

[string]
$Path = ".\.env.local"
)

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)
}