Source/Test-DotEnvValue.ps1
|
<# .SYNOPSIS Tests if a specific key exists in a .env file. .DESCRIPTION Checks whether the given key exists in the specified .env file, without exposing its value. Returns False if the file doesn't exist or isn't well-formed. .PARAMETER Name The key to look for. .PARAMETER Path The path to the .env file. If not specified, checks .env in the current directory. .EXAMPLE Test-DotEnvValue -Name "Foo" -Path ".env" .EXAMPLE "Foo", "Bar" | Test-DotEnvValue -Path ".env" .INPUTS [System.String] The key to look for. Accepts pipeline input by value or by property name. .OUTPUTS [System.Boolean] True if the key exists in the .env file, otherwise False. #> function Test-DotEnvValue { [OutputType([bool])] [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Name, [string] $Path = ".env" ) begin { $values = $null if (Test-Path -Path $Path) { try { $values = ConvertFrom-DotEnv -Text (Get-Content -Path $Path -Raw) } catch { $values = $null } } } process { if ($null -eq $values) { return $false } return $values.ContainsKey($Name) } } |