@ -25,7 +25,7 @@ $params = Parse-Args $args;
# Initialize defaults for input parameters.
# Initialize defaults for input parameters.
$ dest= Get-Attr $params " dest " $FALSE ;
$ path= Get-Attr $params " path " $FALSE ;
$regexp = Get-Attr $params " regexp " $FALSE ;
$regexp = Get-Attr $params " regexp " $FALSE ;
$state = Get-Attr $params " state " " present " ;
$state = Get-Attr $params " state " " present " ;
$line = Get-Attr $params " line " $FALSE ;
$line = Get-Attr $params " line " $FALSE ;
@ -39,32 +39,34 @@ $encoding = Get-Attr $params "encoding" "auto";
$newline = Get-Attr $params " newline " " windows " ;
$newline = Get-Attr $params " newline " " windows " ;
# Parse dest / name /destfil e param aliases for compatibility with lineinfile
# Parse path / dest / destfile / nam e param aliases for compatibility with lineinfile
# and fail if at least one spelling of the parameter is not provided.
# and fail if at least one spelling of the parameter is not provided.
$dest = Get-Attr $params " dest " $FALSE ;
If ( $path -eq $FALSE ) {
If ( $dest -eq $FALSE ) {
$path = Get-Attr $params " dest " $FALSE ;
$dest = Get-Attr $params " name " $FALSE ;
If ( $path -eq $FALSE ) {
If ( $dest -eq $FALSE ) {
$path = Get-Attr $params " destfile " $FALSE ;
$dest = Get-Attr $params " destfile " $FALSE ;
If ( $path -eq $FALSE ) {
If ( $dest -eq $FALSE ) {
$path = Get-Attr $params " name " $FALSE ;
Fail-Json ( New-Object psobject ) " missing required argument: dest " ;
If ( $path -eq $FALSE ) {
Fail-Json ( New-Object psobject ) " missing required argument: path " ;
}
}
}
}
}
}
}
# Fail if the destination is not a file
# Fail if the path is not a file
If ( Test-Path $ dest -pathType container ) {
If ( Test-Path $ path -pathType container ) {
Fail-Json ( New-Object psobject ) " destination is a directory" ;
Fail-Json ( New-Object psobject ) " Path $path is a directory" ;
}
}
# Write lines to a file using the specified line separator and encoding,
# Write lines to a file using the specified line separator and encoding,
# performing validation if a validation command was specified.
# performing validation if a validation command was specified.
function WriteLines($outlines , $ dest , $linesep , $encodingobj , $validate ) {
function WriteLines($outlines , $ path , $linesep , $encodingobj , $validate ) {
$temppath = [ System.IO.Path ] :: GetTempFileName ( ) ;
$temppath = [ System.IO.Path ] :: GetTempFileName ( ) ;
$joined = $outlines -join $linesep ;
$joined = $outlines -join $linesep ;
[ System.IO.File ] :: WriteAllText ( $temppath , $joined , $encodingobj ) ;
[ System.IO.File ] :: WriteAllText ( $temppath , $joined , $encodingobj ) ;
@ -94,9 +96,9 @@ function WriteLines($outlines, $dest, $linesep, $encodingobj, $validate) {
}
}
# Commit changes to the destination file
# Commit changes to the path
$clean dest = $dest . Replace ( " / " , " \ " ) ;
$clean path = $path . Replace ( " / " , " \ " ) ;
Copy-Item $temppath $clean dest -force ;
Copy-Item $temppath $clean path -force ;
Remove-Item $temppath -force ;
Remove-Item $temppath -force ;
}
}
@ -113,24 +115,24 @@ function BackupFile($path) {
# Implement the functionality for state == 'present'
# Implement the functionality for state == 'present'
function Present($ dest , $regexp , $line , $insertafter , $insertbefore , $create , $backup , $backrefs , $validate , $encodingobj , $linesep ) {
function Present($ path , $regexp , $line , $insertafter , $insertbefore , $create , $backup , $backrefs , $validate , $encodingobj , $linesep ) {
# Note that we have to clean up the dest path because ansible wants to treat / and \ as
# Note that we have to clean up the path because ansible wants to treat / and \ as
# interchangeable in windows pathnames, but .NET framework internals do not support that.
# interchangeable in windows pathnames, but .NET framework internals do not support that.
$clean dest = $dest . Replace ( " / " , " \ " ) ;
$clean path = $path . Replace ( " / " , " \ " ) ;
# Check if destination exists. If it does not exist, either create it if create == "yes"
# Check if path exists. If it does not exist, either create it if create == "yes"
# was specified or fail with a reasonable error message.
# was specified or fail with a reasonable error message.
If ( ! ( Test-Path $ dest ) ) {
If ( ! ( Test-Path $ path ) ) {
If ( $create -eq " no " ) {
If ( $create -eq " no " ) {
Fail-Json ( New-Object psobject ) " Destination $dest does not exist ! " ;
Fail-Json ( New-Object psobject ) " Path $path does not exist ! " ;
}
}
# Create new empty file, using the specified encoding to write correct BOM
# Create new empty file, using the specified encoding to write correct BOM
[ System.IO.File ] :: WriteAllLines ( $clean dest , " " , $encodingobj ) ;
[ System.IO.File ] :: WriteAllLines ( $clean path , " " , $encodingobj ) ;
}
}
# Read the dest file lines using the indicated encoding into a mutable ArrayList.
# Read the dest file lines using the indicated encoding into a mutable ArrayList.
$content = [ System.IO.File ] :: ReadAllLines ( $clean dest , $encodingobj ) ;
$content = [ System.IO.File ] :: ReadAllLines ( $clean path , $encodingobj ) ;
If ( $content -eq $null ) {
If ( $content -eq $null ) {
$lines = New-Object System . Collections . ArrayList ;
$lines = New-Object System . Collections . ArrayList ;
}
}
@ -225,15 +227,15 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
}
}
# Write backup file if backup == "yes"
# Write backup file if backup == "yes"
$backup dest = " " ;
$backup path = " " ;
If ( $changed -eq $TRUE -and $backup -eq " yes " ) {
If ( $changed -eq $TRUE -and $backup -eq " yes " ) {
$backup dest = BackupFile $dest ;
$backup path = BackupFile $path ;
}
}
# Write changes to the destination file if changes were made
# Write changes to the path if changes were made
If ( $changed ) {
If ( $changed ) {
WriteLines $lines $ dest $linesep $encodingobj $validate ;
WriteLines $lines $ path $linesep $encodingobj $validate ;
}
}
$encodingstr = $encodingobj . WebName ;
$encodingstr = $encodingobj . WebName ;
@ -242,7 +244,7 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
$result = New-Object psobject @ {
$result = New-Object psobject @ {
changed = $changed
changed = $changed
msg = $msg
msg = $msg
backup = $backup dest
backup = $backup path
encoding = $encodingstr
encoding = $encodingstr
}
}
@ -252,19 +254,19 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
# Implement the functionality for state == 'absent'
# Implement the functionality for state == 'absent'
function Absent($ dest , $regexp , $line , $backup , $validate , $encodingobj , $linesep ) {
function Absent($ path , $regexp , $line , $backup , $validate , $encodingobj , $linesep ) {
# Check if destination exists. If it does not exist, fail with a reasonable error message.
# Check if path exists. If it does not exist, fail with a reasonable error message.
If ( ! ( Test-Path $ dest ) ) {
If ( ! ( Test-Path $ path ) ) {
Fail-Json ( New-Object psobject ) " Destination $dest does not exist ! " ;
Fail-Json ( New-Object psobject ) " Path $path does not exist ! " ;
}
}
# Read the dest file lines using the indicated encoding into a mutable ArrayList. Note
# Read the dest file lines using the indicated encoding into a mutable ArrayList. Note
# that we have to clean up the dest path because ansible wants to treat / and \ as
# that we have to clean up the path because ansible wants to treat / and \ as
# interchangeable in windows pathnames, but .NET framework internals do not support that.
# interchangeable in windows pathnames, but .NET framework internals do not support that.
$clean dest = $dest . Replace ( " / " , " \ " ) ;
$clean path = $path . Replace ( " / " , " \ " ) ;
$content = [ System.IO.File ] :: ReadAllLines ( $clean dest , $encodingobj ) ;
$content = [ System.IO.File ] :: ReadAllLines ( $clean path , $encodingobj ) ;
If ( $content -eq $null ) {
If ( $content -eq $null ) {
$lines = New-Object System . Collections . ArrayList ;
$lines = New-Object System . Collections . ArrayList ;
}
}
@ -303,15 +305,15 @@ function Absent($dest, $regexp, $line, $backup, $validate, $encodingobj, $linese
}
}
# Write backup file if backup == "yes"
# Write backup file if backup == "yes"
$backup dest = " " ;
$backup path = " " ;
If ( $changed -eq $TRUE -and $backup -eq " yes " ) {
If ( $changed -eq $TRUE -and $backup -eq " yes " ) {
$backup dest = BackupFile $dest ;
$backup path = BackupFile $path ;
}
}
# Write changes to the destination file if changes were made
# Write changes to the path if changes were made
If ( $changed ) {
If ( $changed ) {
WriteLines $left $ dest $linesep $encodingobj $validate ;
WriteLines $left $ path $linesep $encodingobj $validate ;
}
}
# Return result information
# Return result information
@ -322,7 +324,7 @@ function Absent($dest, $regexp, $line, $backup, $validate, $encodingobj, $linese
$result = New-Object psobject @ {
$result = New-Object psobject @ {
changed = $changed
changed = $changed
msg = $msg
msg = $msg
backup = $backup dest
backup = $backup path
found = $fcount
found = $fcount
encoding = $encodingstr
encoding = $encodingstr
}
}
@ -362,7 +364,7 @@ If ($encoding -ne "auto") {
# Otherwise see if we can determine the current encoding of the target file.
# Otherwise see if we can determine the current encoding of the target file.
# If the file doesn't exist yet (create == 'yes') we use the default or
# If the file doesn't exist yet (create == 'yes') we use the default or
# explicitly specified encoding set above.
# explicitly specified encoding set above.
Elseif ( Test-Path $ dest ) {
Elseif ( Test-Path $ path ) {
# Get a sorted list of encodings with preambles, longest first
# Get a sorted list of encodings with preambles, longest first
@ -381,7 +383,7 @@ Elseif (Test-Path $dest) {
# Get the first N bytes from the file, where N is the max preamble length we saw
# Get the first N bytes from the file, where N is the max preamble length we saw
[ Byte[] ] $bom = Get-Content -Encoding Byte -ReadCount $max_preamble_len -TotalCount $max_preamble_len -Path $ dest ;
[ Byte[] ] $bom = Get-Content -Encoding Byte -ReadCount $max_preamble_len -TotalCount $max_preamble_len -Path $ path ;
# Iterate through the sorted encodings, looking for a full match.
# Iterate through the sorted encodings, looking for a full match.
@ -426,7 +428,7 @@ If ($state -eq "present") {
$insertafter = " EOF " ;
$insertafter = " EOF " ;
}
}
Present $ dest $regexp $line $insertafter $insertbefore $create $backup $backrefs $validate $encodingobj $linesep ;
Present $ path $regexp $line $insertafter $insertbefore $create $backup $backrefs $validate $encodingobj $linesep ;
}
}
Else {
Else {
@ -435,22 +437,5 @@ Else {
Fail-Json ( New-Object psobject ) " one of line= or regexp= is required with state=absent " ;
Fail-Json ( New-Object psobject ) " one of line= or regexp= is required with state=absent " ;
}
}
Absent $ dest $regexp $line $backup $validate $encodingobj $linesep ;
Absent $ path $regexp $line $backup $validate $encodingobj $linesep ;
}
}