mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
powershell.ps1: Use Get-AnsibleParam and more (#20518)
- Harmonize the name $params like most modules - Use `return` consistently for return values (easier to read) - Implement Get-AnsibleParam internally using -type "bool" - Use a dictionary for $result
This commit is contained in:
parent
c94c53e8a4
commit
a655e90e41
1 changed files with 13 additions and 16 deletions
|
@ -168,7 +168,7 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail
|
|||
$value = $value | ConvertTo-Bool
|
||||
}
|
||||
|
||||
$value
|
||||
return $value
|
||||
}
|
||||
|
||||
#Alias Get-attr-->Get-AnsibleParam for backwards compat. Only add when needed to ease debugging of scripts
|
||||
|
@ -193,13 +193,12 @@ Function ConvertTo-Bool
|
|||
|
||||
if (($obj.GetType().Name -eq "Boolean" -and $obj) -or $boolean_strings -contains $obj_string.ToLower())
|
||||
{
|
||||
$true
|
||||
return $true
|
||||
}
|
||||
Else
|
||||
{
|
||||
$false
|
||||
return $false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
# Helper function to parse Ansible JSON arguments from a "file" passed as
|
||||
|
@ -207,28 +206,27 @@ Function ConvertTo-Bool
|
|||
# Example: $params = Parse-Args $args
|
||||
Function Parse-Args($arguments, $supports_check_mode = $false)
|
||||
{
|
||||
$parameters = New-Object psobject
|
||||
$params = New-Object psobject
|
||||
If ($arguments.Length -gt 0)
|
||||
{
|
||||
$parameters = Get-Content $arguments[0] | ConvertFrom-Json
|
||||
$params = Get-Content $arguments[0] | ConvertFrom-Json
|
||||
}
|
||||
$check_mode = Get-Attr $parameters "_ansible_check_mode" $false | ConvertTo-Bool
|
||||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
||||
If ($check_mode -and -not $supports_check_mode)
|
||||
{
|
||||
$obj = New-Object psobject
|
||||
Set-Attr $obj "skipped" $true
|
||||
Set-Attr $obj "changed" $false
|
||||
Set-Attr $obj "msg" "remote module does not support check mode"
|
||||
Exit-Json $obj
|
||||
Exit-Json @{
|
||||
skipped = $true
|
||||
changed = $false
|
||||
msg = "remote module does not support check mode"
|
||||
}
|
||||
}
|
||||
$parameters
|
||||
return $params
|
||||
}
|
||||
|
||||
# Helper function to calculate a hash of a file in a way which powershell 3
|
||||
# and above can handle:
|
||||
Function Get-FileChecksum($path)
|
||||
{
|
||||
$hash = ""
|
||||
If (Test-Path -PathType Leaf $path)
|
||||
{
|
||||
$sp = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider;
|
||||
|
@ -238,7 +236,7 @@ Function Get-FileChecksum($path)
|
|||
}
|
||||
ElseIf (Test-Path -PathType Container $path)
|
||||
{
|
||||
$hash= "3";
|
||||
$hash = "3";
|
||||
}
|
||||
Else
|
||||
{
|
||||
|
@ -262,5 +260,4 @@ Function Get-PendingRebootStatus
|
|||
{
|
||||
return $False
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue