From 124fd53fe9c9c1b05fb2ccef9a63bfb8e4918fbc Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 28 Dec 2022 13:15:07 -0600 Subject: [PATCH] (#2854) Add helper to read config values This adds a PowerShell helper to get config values from the Chocolatey configuration file. This helper is intended to be used to allow hook scripts to access configuration values set in the config. --- .../chocolatey.resources.csproj | 1 + .../functions/Get-ChocolateyConfigValue.ps1 | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/chocolatey.resources/helpers/functions/Get-ChocolateyConfigValue.ps1 diff --git a/src/chocolatey.resources/chocolatey.resources.csproj b/src/chocolatey.resources/chocolatey.resources.csproj index efc1e7e305..a43e256039 100644 --- a/src/chocolatey.resources/chocolatey.resources.csproj +++ b/src/chocolatey.resources/chocolatey.resources.csproj @@ -66,6 +66,7 @@ + diff --git a/src/chocolatey.resources/helpers/functions/Get-ChocolateyConfigValue.ps1 b/src/chocolatey.resources/helpers/functions/Get-ChocolateyConfigValue.ps1 new file mode 100644 index 0000000000..f41bc1705e --- /dev/null +++ b/src/chocolatey.resources/helpers/functions/Get-ChocolateyConfigValue.ps1 @@ -0,0 +1,64 @@ +# Copyright © 2022 Chocolatey Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +function Get-ChocolateyConfigValue { + <# +.SYNOPSIS +Retrieve a value from the Chocolatey Configuration file + +.DESCRIPTION +This function will attempt to retrieve the path according to the specified Path Type +to a valid location that can be used by maintainers in certain scenarios. + +.NOTES +Available in 2.1.0+ + +.INPUTS +None + +.OUTPUTS +This function outputs the value of the specified configuration key. +If the key is not found, there is no output. + +.PARAMETER configKey +The name of the configuration value that should be looked up. + +.PARAMETER IgnoredArguments +Allows splatting with arguments that do not apply. Do not use directly. + +.EXAMPLE +> +$value = Get-ChocolateyConfigValue -configKey 'cacheLocation' +#> + param( + [parameter(Mandatory = $true)] + [string] $configKey, + [parameter(ValueFromRemainingArguments = $true)] + [Object[]] $ignoredArguments + ) + + try { + $installLocation = Get-ChocolateyPath -pathType 'InstallPath' + $configPath = Join-Path $installLocation "config\chocolatey.config" + [xml]$configContents = Get-Content -Path $configPath + return $configContents.chocolatey.config.add | + Where-Object { $_.key -eq $configKey } | + Select-Object -ExpandProperty value + } + catch { + Write-Warning "Unable to read config value '$configKey' with error" + Write-Warning $_ + } +}