config.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
Set-StrictMode -Version Latest function Find-LocalConfigPath { param ( [Parameter(Mandatory)][String] $Dir, [Parameter(Mandatory)][String] $Name ) while ($true) { if ($Dir -eq $Env:USERPROFILE -or [String]::IsNullOrEmpty($Dir)) { $null return } $test = Join-Path $Dir $Name if (Test-Path $test) { $test return } $Dir = Split-Path $Dir -Parent } } function Copy-HashtableDeeply { param ( [Hashtable] $Hashtable ) $result = @{} foreach ($key in $Hashtable.Keys) { $item = $Hashtable[$key] if ($item -is [Hashtable]) { $result.Add($key, (Copy-HashtableDeeply $item)) continue } if ($item -is [System.ICloneable]) { $result.Add($key, $item.Clone()) continue } $result.Add($key, $item) } $result } # right-biased. function Join-Hashtables { param ( [Hashtable[]] $Hashtables, [Switch] $Breaking = $false ) if ($null -eq $Hashtables -or @() -eq $Hashtables) { $null return } $result = $null foreach ($h in $Hashtables) { if ($null -eq $h) { continue } if ($null -eq $result) { if ($Breaking) { $result = $h } else { $result = Copy-HashtableDeeply $h } continue } foreach ($key in $h.Keys) { $value = $h[$key] if ($result.ContainsKey($key)) { if ($value -is [Hashtable]) { if ($null -ne $result[$key]) { [void] (Join-Hashtables $result[$key], $value -Breaking) } else { $result.Remove($key) $result.Add($key, $h[$key]) } } else { $result.Remove($key) $result.Add($key, $h[$key]) } } else { $result.Add($key, $h[$key]) } } } $result } # .SYNOPSIS # Find and read configurations. This returns a joined hashtable. # # .PARAMETER $Name # A name of an application. # # .DESCRIPTION # When the Name parameter is “foo”, this searches configuration files that named “config.yaml” in “$Env:ProgramData\$Name”, “$Env:APPDATA\$Name” # and “$Name.yaml” in the current working directory or its parents recursively. function Get-Config { param ( [Parameter(Mandatory)][String] $Name ) $systemGlobalDataPath = "$Env:ProgramData\$Name" $userGlobalDataPath = "$Env:APPDATA\$Name" $localConfigName = "$Name.yaml" $globalConfigName = 'config.yaml' $localConfigPath = Find-LocalConfigPath (Get-Location) $localConfigName $configPaths = "$systemGlobalDataPath\$globalConfigName", "$userGlobalDataPath\$globalConfigName", $localConfigPath $configs = @() foreach ($path in $configPaths) { if ($null -ne $path -and (Test-Path $path)) { $configs += ConvertFrom-Yaml (Get-Content $path -Raw) } } Join-Hashtables $configs } # .SYNOPSIS # Get an specified item in a hashtable. # # .PARAMETER Name # An array of nested keys. # # .PARAMETER Hashtable # A target. function Get-HashtaleItem { param ( [Parameter(Mandatory)][Object[]] $Name, [Hashtable] $Hashtable ) $item = $Hashtable foreach ($n in $Name) { if ($null -eq $item) { $null return } $item = $item[$n] } $item } Export-ModuleMember -Function Get-Config, Get-HashtaleItem |