Merge pull request #600 from Icinga:fix/cmd_aliases_are_ignored_on_plugin_execution

Fix: Command aliases not evaluated on plugin run

Fixes plugin argument parser to proceed with real argument names and possible provided aliases
This commit is contained in:
Lord Hepipud 2022-12-19 13:18:37 +01:00 committed by GitHub
commit b3d4a2cdcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View file

@ -7,7 +7,7 @@ documentation before upgrading to a new release.
Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-framework/milestones?state=closed).
## 1.10.1 (2022-27-10)
## 1.10.1 (2022-12-20)
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/27?closed=1)
@ -15,6 +15,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before
* [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression
* [#599](https://github.com/Icinga/icinga-powershell-plugins/issues/599) Fixes plugin argument parser to proceed with real argument names and possible provided aliases
## 1.10.0 (2022-08-30)

View file

@ -25,13 +25,27 @@ function ConvertTo-IcingaPowerShellArguments()
return @{ };
}
$CommandHelp = Get-Help -Name $Command -Full -ErrorAction SilentlyContinue;
$CmdData = Get-Command $Command -ErrorAction SilentlyContinue;
[array]$CmdAllowedArgs = $CmdData.Parameters.Keys;
# Ensure we do not cause exceptions along the border in case the plugin is not installed
if ($null -eq $CommandHelp) {
if ($null -eq $CmdAllowedArgs) {
return @{ };
}
# Ensure we not only add the parameter name to our allow list but also possible aliases
foreach ($entry in $CmdData.Parameters.Keys) {
if ($CmdData.Parameters[$entry].Aliases.Count -eq 0) {
continue;
}
foreach ($cmdAlias in $CmdData.Parameters[$entry].Aliases) {
if ($CmdAllowedArgs -NotContains $cmdAlias) {
$CmdAllowedArgs += $cmdAlias;
}
}
}
[hashtable]$IcingaArguments = @{ };
[int]$ArgumentIndex = 0;
@ -50,7 +64,7 @@ function ConvertTo-IcingaPowerShellArguments()
}
# Check if our string value is a argument contained inside the command being executed
if ($CommandHelp.parameters.parameter.name -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) {
if ($CmdAllowedArgs -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) {
# Continue if we are not an argument
$ArgumentIndex += 1;
continue;
@ -77,7 +91,7 @@ function ConvertTo-IcingaPowerShellArguments()
# If our next value on the index is an argument in our command
# -> The current argument seems to be a switch argument
if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
if ($CmdAllowedArgs -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add($Argument, $TRUE);
}
@ -103,7 +117,7 @@ function ConvertTo-IcingaPowerShellArguments()
[string]$NextValue = $Arguments[$ReadStringIndex + 1];
# Check the next string element and evaluate if it is an argument for our command
if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
if ($CmdAllowedArgs -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
break;
}