win_say: Clean up and check-mode support

Changes include:
- Clean up parameter handling
- Replace $result PSObject with a hash
- Added check-mode support
pull/4420/head
Dag Wieers 2017-02-14 03:05:49 +01:00 committed by Brian Coca
parent 576ff0728d
commit aae1a00d7e
1 changed files with 39 additions and 32 deletions

View File

@ -19,14 +19,20 @@
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
$params = Parse-Args $args; $params = Parse-Args $args -supports_check_mode $true
$result = New-Object PSObject; $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$msg = Get-AnsibleParam -obj $params -name "msg"
$msg = Get-AnsibleParam -obj $params -name "msg" -type "str"
$msg_file = Get-AnsibleParam -obj $params -name "msg_file" -type "path" $msg_file = Get-AnsibleParam -obj $params -name "msg_file" -type "path"
$start_sound_path = Get-AnsibleParam -obj $params -name "start_sound_path" -type "path" $start_sound_path = Get-AnsibleParam -obj $params -name "start_sound_path" -type "path"
$end_sound_path = Get-AnsibleParam -obj $params -name "end_sound_path" -type "path" $end_sound_path = Get-AnsibleParam -obj $params -name "end_sound_path" -type "path"
$voice = Get-AnsibleParam -obj $params -name "voice" $voice = Get-AnsibleParam -obj $params -name "voice" -type "str"
$speech_speed = Get-AnsibleParam -obj $params -name "speech_speed" $speech_speed = Get-AnsibleParam -obj $params -name "speech_speed" -type "str"
$result = @{
changed = $false
}
$speed = 0 $speed = 0
$words = $null $words = $null
@ -35,24 +41,20 @@ if ($speech_speed -ne $null) {
$speed = [convert]::ToInt32($speech_speed, 10) $speed = [convert]::ToInt32($speech_speed, 10)
} catch { } catch {
Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed could not be converted to an integer." Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed could not be converted to an integer."
} }
if ($speed -lt -10 -or $speed -gt 10) { if ($speed -lt -10 -or $speed -gt 10) {
Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed is outside this range." Fail-Json $result "speech_speed needs to a integer in the range -10 to 10. The value $speech_speed is outside this range."
} }
} }
if ($msg_file -ne $null -and $msg -ne $null) { if ($msg_file -ne $null -and $msg -ne $null) {
Fail-Json $result "Please specify either msg_file or msg parameters, not both" Fail-Json $result "Please specify either msg_file or msg parameters, not both"
} }
if ($msg_file -eq $null -and $msg -eq $null -and $start_sound_path -eq $null -and $end_sound_path -eq $null) { if ($msg_file -eq $null -and $msg -eq $null -and $start_sound_path -eq $null -and $end_sound_path -eq $null) {
Fail-Json $result "No msg_file, msg, start_sound_path, or end_sound_path parameters have been specified. Please specify at least one so the module has something to do" Fail-Json $result "No msg_file, msg, start_sound_path, or end_sound_path parameters have been specified. Please specify at least one so the module has something to do"
} }
if ($msg_file -ne $null) { if ($msg_file -ne $null) {
if (Test-Path $msg_file) { if (Test-Path $msg_file) {
$words = Get-Content $msg_file | Out-String $words = Get-Content $msg_file | Out-String
@ -63,7 +65,9 @@ if ($msg_file -ne $null) {
if ($start_sound_path -ne $null) { if ($start_sound_path -ne $null) {
if (Test-Path $start_sound_path) { if (Test-Path $start_sound_path) {
(new-object Media.SoundPlayer $start_sound_path).playSync(); if (-not $check_mode) {
(new-object Media.SoundPlayer $start_sound_path).playSync()
}
} else { } else {
Fail-Json $result "Start sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file." Fail-Json $result "Start sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file."
} }
@ -80,27 +84,30 @@ if ($words -ne $null) {
try { try {
$tts.SelectVoice($voice) $tts.SelectVoice($voice)
} catch [System.Management.Automation.MethodInvocationException] { } catch [System.Management.Automation.MethodInvocationException] {
Set-Attr $result "voice_info" "Could not load voice $voice, using system default voice." $result.voice_info = "Could not load voice $voice, using system default voice."
} }
} }
Set-Attr $result "voice" $tts.Voice.Name $result.voice = $tts.Voice.Name
if ($speed -ne 0) { if ($speed -ne 0) {
$tts.Rate = $speed $tts.Rate = $speed
} }
if (-not $check_mode) {
$tts.Speak($words) $tts.Speak($words)
}
$tts.Dispose() $tts.Dispose()
} }
if ($end_sound_path -ne $null) { if ($end_sound_path -ne $null) {
if (Test-Path $end_sound_path) { if (Test-Path $end_sound_path) {
(new-object Media.SoundPlayer $end_sound_path).playSync(); if (-not $check_mode) {
(new-object Media.SoundPlayer $end_sound_path).playSync()
}
} else { } else {
Fail-Json $result "End sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file." Fail-Json $result "End sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file."
} }
} }
Set-Attr $result "changed" $false; $result.message_text = $words
Set-Attr $result "message_text" $words;
Exit-Json $result; Exit-Json $result