win_nssm: fix error with app_parameters parameter

Fix #25265

(cherry picked from commit aba0d48ba5)
This commit is contained in:
Kévin Subileau 2018-08-27 12:44:34 +02:00 committed by Toshio Kuratomi
parent df1aaab3df
commit 5eef5f629e

View file

@ -20,7 +20,7 @@ $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $tru
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","started","stopped","restarted" -resultobj $result $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","started","stopped","restarted" -resultobj $result
$application = Get-AnsibleParam -obj $params -name "application" -type "str" $application = Get-AnsibleParam -obj $params -name "application" -type "str"
$appParameters = Get-AnsibleParam -obj $params -name "app_parameters" -type "str" $appParameters = Get-AnsibleParam -obj $params -name "app_parameters"
$appParametersFree = Get-AnsibleParam -obj $params -name "app_parameters_free_form" -type "str" $appParametersFree = Get-AnsibleParam -obj $params -name "app_parameters_free_form" -type "str"
$startMode = Get-AnsibleParam -obj $params -name "start_mode" -type "str" -default "auto" -validateset "auto","delayed","manual","disabled" -resultobj $result $startMode = Get-AnsibleParam -obj $params -name "start_mode" -type "str" -default "auto" -validateset "auto","delayed","manual","disabled" -resultobj $result
@ -35,6 +35,9 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null))
{ {
Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both" Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both"
} }
if (($appParameters -ne $null) -and ($appParameters -isnot [System.Collections.Hashtable])) {
Fail-Json -obj $result -message "The app_parameters parameter must be a dict"
}
Function Nssm-Invoke Function Nssm-Invoke
{ {
@ -167,30 +170,13 @@ Function Nssm-Install
} }
} }
Function ParseAppParameters()
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]$appParameters
)
$escapedAppParameters = $appParameters.TrimStart("@").TrimStart("{").TrimEnd("}").Replace("; ","`n").Replace("\","\\")
return ConvertFrom-StringData -StringData $escapedAppParameters
}
Function Nssm-Update-AppParameters Function Nssm-Update-AppParameters
{ {
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[string]$name, [string]$name,
[Parameter(Mandatory=$true)] $appParameters,
[AllowEmptyString()]
[string]$appParameters,
[string]$appParametersFree [string]$appParametersFree
) )
@ -208,10 +194,9 @@ Function Nssm-Update-AppParameters
$appParamVals = @() $appParamVals = @()
$singleLineParams = "" $singleLineParams = ""
if ($appParameters) if ($null -ne $appParameters)
{ {
$appParametersHash = ParseAppParameters -appParameters $appParameters $appParameters.GetEnumerator() |
$appParametersHash.GetEnumerator() |
% { % {
$key = $($_.Name) $key = $($_.Name)
$val = $($_.Value) $val = $($_.Value)
@ -222,15 +207,14 @@ Function Nssm-Update-AppParameters
if ($key -eq "_") { if ($key -eq "_") {
$singleLineParams = "$val " + $singleLineParams $singleLineParams = "$val " + $singleLineParams
} else { } else {
$singleLineParams = $singleLineParams + "$key ""$val""" $singleLineParams = $singleLineParams + " $key ""$val"""
} }
} }
$result.nssm_app_parameters_parsed = $appParametersHash
$result.nssm_app_parameters_keys = $appParamKeys $result.nssm_app_parameters_keys = $appParamKeys
$result.nssm_app_parameters_vals = $appParamVals $result.nssm_app_parameters_vals = $appParamVals
} }
elseif ($appParametersFree) { elseif ($null -ne $appParametersFree) {
$result.nssm_app_parameters_free_form = $appParametersFree $result.nssm_app_parameters_free_form = $appParametersFree
$singleLineParams = $appParametersFree $singleLineParams = $appParametersFree
} }