mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
win_command/win_shell: Clean up and make coherent (#21371)
* win_command/win_shell: Clean up and make coherent Changes include: - Make win_command and win_shell more coherent - Make condensed one-line more readable * Revert to original formatting
This commit is contained in:
parent
f4b34a4c3b
commit
1cfaa5caab
2 changed files with 43 additions and 31 deletions
|
@ -22,24 +22,7 @@
|
|||
Set-StrictMode -Version 2
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$parsed_args = Parse-Args $args $false
|
||||
|
||||
$raw_command_line = $(Get-AnsibleParam $parsed_args "_raw_params" -failifempty $true).Trim()
|
||||
$chdir = Get-AnsibleParam $parsed_args "chdir" -type "path"
|
||||
$creates = Get-AnsibleParam $parsed_args "creates" -type "path"
|
||||
$removes = Get-AnsibleParam $parsed_args "removes" -type "path"
|
||||
|
||||
$result = @{changed=$true; warnings=@(); cmd=$raw_command_line}
|
||||
|
||||
If($creates -and $(Test-Path $creates)) {
|
||||
Exit-Json @{cmd=$raw_command_line; msg="skipped, since $creates exists"; changed=$false; skipped=$true; rc=0}
|
||||
}
|
||||
|
||||
If($removes -and -not $(Test-Path $removes)) {
|
||||
Exit-Json @{cmd=$raw_command_line; msg="skipped, since $removes does not exist"; changed=$false; skipped=$true; rc=0}
|
||||
}
|
||||
|
||||
$util_def = @'
|
||||
$helper_def = @'
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
|
@ -101,12 +84,34 @@ namespace Ansible.Command
|
|||
}
|
||||
'@
|
||||
|
||||
$util_type = Add-Type -TypeDefinition $util_def
|
||||
$params = Parse-Args $args -supports_check_mode $false
|
||||
|
||||
# FUTURE: extract this code to separate module_utils as Windows module API version of run_command
|
||||
$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true
|
||||
$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path"
|
||||
$creates = Get-AnsibleParam -obj $params -name "creates" -type "path"
|
||||
$removes = Get-AnsibleParam -obj $params -name "removes" -type "path"
|
||||
|
||||
$raw_command_line = $raw_command_line.Trim()
|
||||
|
||||
$result = @{
|
||||
changed = $true
|
||||
cmd = $raw_command_line
|
||||
}
|
||||
|
||||
If($creates -and $(Test-Path -Path $creates)) {
|
||||
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||
}
|
||||
|
||||
If($removes -and -not $(Test-Path -Path $removes)) {
|
||||
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||
}
|
||||
|
||||
Add-Type -TypeDefinition $helper_def
|
||||
|
||||
$exec_args = $null
|
||||
|
||||
# FUTURE: extract this code to separate module_utils as Windows module API version of run_command
|
||||
|
||||
# Parse the command-line with the Win32 parser to get the application name to run. The Win32 parser
|
||||
# will deal with quoting/escaping for us...
|
||||
# FUTURE: no longer necessary once we switch to raw Win32 CreateProcess
|
||||
|
@ -138,7 +143,7 @@ Catch [System.ComponentModel.Win32Exception] {
|
|||
# fail nicely for "normal" error conditions
|
||||
# FUTURE: this probably won't work on Nano Server
|
||||
$excep = $_
|
||||
Exit-Json @{failed=$true;changed=$false;cmd=$raw_command_line;rc=$excep.Exception.NativeErrorCode;msg=$excep.Exception.Message}
|
||||
Exit-Json @{msg = $excep.Exception.Message; cmd = $raw_command_line; changed = $false; rc = $excep.Exception.NativeErrorCode}
|
||||
}
|
||||
|
||||
$stdout = $stderr = [string] $null
|
||||
|
@ -148,6 +153,8 @@ $stdout = $stderr = [string] $null
|
|||
$result.stdout = $stdout
|
||||
$result.stderr = $stderr
|
||||
|
||||
# TODO: decode CLIXML stderr output (and other streams?)
|
||||
|
||||
$proc.WaitForExit() | Out-Null
|
||||
|
||||
$result.rc = $proc.ExitCode
|
||||
|
|
|
@ -60,22 +60,27 @@ namespace Ansible.Shell
|
|||
}
|
||||
"@
|
||||
|
||||
$parsed_args = Parse-Args $args $false
|
||||
$params = Parse-Args $args -supports_check_mode $false
|
||||
|
||||
$raw_command_line = $(Get-AnsibleParam $parsed_args "_raw_params" -failifempty $true).Trim()
|
||||
$chdir = Get-AnsibleParam $parsed_args "chdir" -type "path"
|
||||
$executable = Get-AnsibleParam $parsed_args "executable" -type "path"
|
||||
$creates = Get-AnsibleParam $parsed_args "creates" -type "path"
|
||||
$removes = Get-AnsibleParam $parsed_args "removes" -type "path"
|
||||
$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true
|
||||
$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path"
|
||||
$executable = Get-AnsibleParam -obj $params -name "executable" -type "path"
|
||||
$creates = Get-AnsibleParam -obj $params -name "creates" -type "path"
|
||||
$removes = Get-AnsibleParam -obj $params -name "removes" -type "path"
|
||||
|
||||
$result = @{changed=$true; warnings=@(); cmd=$raw_command_line}
|
||||
$raw_command_line = $raw_command_line.Trim()
|
||||
|
||||
$result = @{
|
||||
changed = $true
|
||||
cmd = $raw_command_line
|
||||
}
|
||||
|
||||
If($creates -and $(Test-Path $creates)) {
|
||||
Exit-Json @{cmd=$raw_command_line; msg="skipped, since $creates exists"; changed=$false; skipped=$true; rc=0}
|
||||
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||
}
|
||||
|
||||
If($removes -and -not $(Test-Path $removes)) {
|
||||
Exit-Json @{cmd=$raw_command_line; msg="skipped, since $removes does not exist"; changed=$false; skipped=$true; rc=0}
|
||||
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
|
||||
}
|
||||
|
||||
Add-Type -TypeDefinition $helper_def
|
||||
|
@ -117,7 +122,7 @@ Catch [System.ComponentModel.Win32Exception] {
|
|||
# fail nicely for "normal" error conditions
|
||||
# FUTURE: this probably won't work on Nano Server
|
||||
$excep = $_
|
||||
Exit-Json @{failed=$true;changed=$false;cmd=$raw_command_line;rc=$excep.Exception.NativeErrorCode;msg=$excep.Exception.Message}
|
||||
Exit-Json @{msg = $excep.Exception.Message; cmd = $raw_command_line; changed = $false; rc = $excep.Exception.NativeErrorCode}
|
||||
}
|
||||
|
||||
$stdout = $stderr = [string] $null
|
||||
|
|
Loading…
Reference in a new issue