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:
Dag Wieers 2017-01-27 23:46:21 +01:00 committed by Matt Davis
parent c94c53e8a4
commit a655e90e41

View file

@ -168,7 +168,7 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail
$value = $value | ConvertTo-Bool $value = $value | ConvertTo-Bool
} }
$value return $value
} }
#Alias Get-attr-->Get-AnsibleParam for backwards compat. Only add when needed to ease debugging of scripts #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()) if (($obj.GetType().Name -eq "Boolean" -and $obj) -or $boolean_strings -contains $obj_string.ToLower())
{ {
$true return $true
} }
Else Else
{ {
$false return $false
} }
return
} }
# Helper function to parse Ansible JSON arguments from a "file" passed as # Helper function to parse Ansible JSON arguments from a "file" passed as
@ -207,28 +206,27 @@ Function ConvertTo-Bool
# Example: $params = Parse-Args $args # Example: $params = Parse-Args $args
Function Parse-Args($arguments, $supports_check_mode = $false) Function Parse-Args($arguments, $supports_check_mode = $false)
{ {
$parameters = New-Object psobject $params = New-Object psobject
If ($arguments.Length -gt 0) 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) If ($check_mode -and -not $supports_check_mode)
{ {
$obj = New-Object psobject Exit-Json @{
Set-Attr $obj "skipped" $true skipped = $true
Set-Attr $obj "changed" $false changed = $false
Set-Attr $obj "msg" "remote module does not support check mode" msg = "remote module does not support check mode"
Exit-Json $obj }
} }
$parameters return $params
} }
# Helper function to calculate a hash of a file in a way which powershell 3 # Helper function to calculate a hash of a file in a way which powershell 3
# and above can handle: # and above can handle:
Function Get-FileChecksum($path) Function Get-FileChecksum($path)
{ {
$hash = ""
If (Test-Path -PathType Leaf $path) If (Test-Path -PathType Leaf $path)
{ {
$sp = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider; $sp = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider;
@ -238,7 +236,7 @@ Function Get-FileChecksum($path)
} }
ElseIf (Test-Path -PathType Container $path) ElseIf (Test-Path -PathType Container $path)
{ {
$hash= "3"; $hash = "3";
} }
Else Else
{ {
@ -262,5 +260,4 @@ Function Get-PendingRebootStatus
{ {
return $False return $False
} }
} }