ps/Modules/Cole.PowerShell.Developer/Public/Read-GitConfig.ps1
2023-05-30 22:51:22 -07:00

78 lines
2.4 KiB
PowerShell

function Read-GitConfig {
<#
.SYNOPSIS
Naively read the git config. This function is not great. It basically sort of works.
.PARAMETER Path
[string] The path of the git config to read
#>
param(
$Path
)
$return = @{}
$lines = Get-Content -Path $Path
$currentObject = @{}
$currentArray = @()
$currentObjectDirty = $false
$currentKey = $null
foreach($line in $lines) {
# ignore comments
$line = ($line -split '#' -split ';')[0]
# remove whitespace
$line = $line.Trim()
if ([string]::IsNullOrWhiteSpace($line)) {
continue
}
if ($line.StartsWith('[')) {
if ($currentObjectDirty) {
if ($null -eq $return.$currentKey) {
$return.$currentKey = @()
}
if ([string]::IsNullOrWhiteSpace($currentObject.name)) {
$return.$currentKey += $currentArray
} else {
$return.$currentKey += $currentObject
}
$currentObject = @{}
$currentArray = @()
$currentKey = $null
$currentObjectDirty = $false
}
$line = $line -replace '\[','' -replace '\]',''
$firstSpace = $line.IndexOf(' ')
$currentKey = $line
$name = $null
if ($firstSpace -gt -1) {
$currentKey = $line.Substring(0,$firstSpace).Trim()
$name = $line.Substring($firstSpace + 1).Trim() -replace '"',''
}
if (![string]::IsNullOrWhiteSpace($name)) {
$currentObject.name = $name
}
$currentObjectDirty = $true
} else {
Write-Host $line
$firstSplit = $line.IndexOf('=')
if ($firstSplit -gt -1) {
$name = $line.Substring(0,$firstSplit).Trim()
if ([string]::IsNullOrWhiteSpace($name)) {
Write-Error "no variable name found"
continue
}
$value = $line.Substring($firstSplit + 1).Trim() -replace '"',''
$currentArray += @{ $name = $value }
$currentObject.$name = $value
$currentObjectDirty = $true
} else {
$currentArray += $line
$currentObjectDirty = $true
}
}
}
return $return
}