summaryrefslogtreecommitdiffstats
path: root/test/support/windows-integration/plugins/modules/win_lineinfile.ps1
blob: 38dd8b8bc09134e1fe269422c6d795fb2152e869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!powershell

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.Backup

function WriteLines($outlines, $path, $linesep, $encodingobj, $validate, $check_mode) {
	Try {
		$temppath = [System.IO.Path]::GetTempFileName();
	}
	Catch {
		Fail-Json @{} "Cannot create temporary file! ($($_.Exception.Message))";
	}
	$joined = $outlines -join $linesep;
	[System.IO.File]::WriteAllText($temppath, $joined, $encodingobj);

	If ($validate) {

		If (-not ($validate -like "*%s*")) {
			Fail-Json @{} "validate must contain %s: $validate";
		}

		$validate = $validate.Replace("%s", $temppath);

		$parts = [System.Collections.ArrayList] $validate.Split(" ");
		$cmdname = $parts[0];

		$cmdargs = $validate.Substring($cmdname.Length + 1);

		$process = [Diagnostics.Process]::Start($cmdname, $cmdargs);
		$process.WaitForExit();

		If ($process.ExitCode -ne 0) {
			[string] $output = $process.StandardOutput.ReadToEnd();
			[string] $error = $process.StandardError.ReadToEnd();
			Remove-Item $temppath -force;
			Fail-Json @{} "failed to validate $cmdname $cmdargs with error: $output $error";
		}

	}

	# Commit changes to the path
	$cleanpath = $path.Replace("/", "\");
	Try {
		Copy-Item -Path $temppath -Destination $cleanpath -Force -WhatIf:$check_mode;
	}
	Catch {
		Fail-Json @{} "Cannot write to: $cleanpath ($($_.Exception.Message))";
	}

	Try {
		Remove-Item -Path $temppath -Force -WhatIf:$check_mode;
	}
	Catch {
		Fail-Json @{} "Cannot remove temporary file: $temppath ($($_.Exception.Message))";
	}

	return $joined;

}


# Implement the functionality for state == 'present'
function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $backup, $backrefs, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {

	# 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.
	$cleanpath = $path.Replace("/", "\");

	# Check if path exists. If it does not exist, either create it if create == "yes"
	# was specified or fail with a reasonable error message.
	If (-not (Test-Path -LiteralPath $path)) {
		If (-not $create) {
			Fail-Json @{} "Path $path does not exist !";
		}
		# Create new empty file, using the specified encoding to write correct BOM
		[System.IO.File]::WriteAllLines($cleanpath, "", $encodingobj);
	}

	# Initialize result information
	$result = @{
		backup = "";
		changed = $false;
		msg = "";
	}

	# Read the dest file lines using the indicated encoding into a mutable ArrayList.
	$before = [System.IO.File]::ReadAllLines($cleanpath, $encodingobj)
	If ($null -eq $before) {
		$lines = New-Object System.Collections.ArrayList;
	}
	Else {
		$lines = [System.Collections.ArrayList] $before;
	}

	if ($diff_support) {
		$result.diff = @{
			before = $before -join $linesep;
		}
	}

	# Compile the regex specified, if provided
	$mre = $null;
	If ($regex) {
		$mre = New-Object Regex $regex, 'Compiled';
	}

	# Compile the regex for insertafter or insertbefore, if provided
	$insre = $null;
	If ($insertafter -and $insertafter -ne "BOF" -and $insertafter -ne "EOF") {
		$insre = New-Object Regex $insertafter, 'Compiled';
	}
	ElseIf ($insertbefore -and $insertbefore -ne "BOF") {
		$insre = New-Object Regex $insertbefore, 'Compiled';
	}

	# index[0] is the line num where regex has been found
	# index[1] is the line num where insertafter/insertbefore has been found
	$index = -1, -1;
	$lineno = 0;

	# The latest match object and matched line
	$matched_line = "";

	# Iterate through the lines in the file looking for matches
	Foreach ($cur_line in $lines) {
		If ($regex) {
			$m = $mre.Match($cur_line);
			$match_found = $m.Success;
			If ($match_found) {
				$matched_line = $cur_line;
			}
		}
		Else {
			$match_found = $line -ceq $cur_line;
		}
		If ($match_found) {
			$index[0] = $lineno;
		}
		ElseIf ($insre -and $insre.Match($cur_line).Success) {
			If ($insertafter) {
				$index[1] = $lineno + 1;
			}
			If ($insertbefore) {
				$index[1] = $lineno;
			}
		}
		$lineno = $lineno + 1;
	}

	If ($index[0] -ne -1) {
		If ($backrefs) {
		    $new_line = [regex]::Replace($matched_line, $regex, $line);
		}
		Else {
			$new_line = $line;
		}
		If ($lines[$index[0]] -cne $new_line) {
			$lines[$index[0]] = $new_line;
			$result.changed = $true;
			$result.msg = "line replaced";
		}
	}
	ElseIf ($backrefs) {
		# No matches - no-op
	}
	ElseIf ($insertbefore -eq "BOF" -or $insertafter -eq "BOF") {
		$lines.Insert(0, $line);
		$result.changed = $true;
		$result.msg = "line added";
	}
	ElseIf ($insertafter -eq "EOF" -or $index[1] -eq -1) {
		$lines.Add($line) > $null;
		$result.changed = $true;
		$result.msg = "line added";
	}
	Else {
		$lines.Insert($index[1], $line);
		$result.changed = $true;
		$result.msg = "line added";
	}

	# Write changes to the path if changes were made
	If ($result.changed) {

		# Write backup file if backup == "yes"
		If ($backup) {
			$result.backup_file = Backup-File -path $path -WhatIf:$check_mode
			# Ensure backward compatibility (deprecate in future)
			$result.backup = $result.backup_file
		}

		$writelines_params = @{
			outlines = $lines
			path = $path
			linesep = $linesep
			encodingobj = $encodingobj
			validate = $validate
			check_mode = $check_mode
		}
		$after = WriteLines @writelines_params;

		if ($diff_support) {
			$result.diff.after = $after;
		}
	}

	$result.encoding = $encodingobj.WebName;

	Exit-Json $result;
}


# Implement the functionality for state == 'absent'
function Absent($path, $regex, $line, $backup, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {

	# Check if path exists. If it does not exist, fail with a reasonable error message.
	If (-not (Test-Path -LiteralPath $path)) {
		Fail-Json @{} "Path $path does not exist !";
	}

	# Initialize result information
	$result = @{
		backup = "";
		changed = $false;
		msg = "";
	}

	# Read the dest file lines using the indicated encoding into a mutable ArrayList. 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.
	$cleanpath = $path.Replace("/", "\");
	$before = [System.IO.File]::ReadAllLines($cleanpath, $encodingobj);
	If ($null -eq $before) {
		$lines = New-Object System.Collections.ArrayList;
	}
	Else {
		$lines = [System.Collections.ArrayList] $before;
	}

	if ($diff_support) {
		$result.diff = @{
			before = $before -join $linesep;
		}
	}

	# Compile the regex specified, if provided
	$cre = $null;
	If ($regex) {
		$cre = New-Object Regex $regex, 'Compiled';
	}

	$found = New-Object System.Collections.ArrayList;
	$left = New-Object System.Collections.ArrayList;

	Foreach ($cur_line in $lines) {
		If ($regex) {
			$m = $cre.Match($cur_line);
			$match_found = $m.Success;
		}
		Else {
			$match_found = $line -ceq $cur_line;
		}
		If ($match_found) {
			$found.Add($cur_line) > $null;
			$result.changed = $true;
		}
		Else {
			$left.Add($cur_line) > $null;
		}
	}

	# Write changes to the path if changes were made
	If ($result.changed) {

		# Write backup file if backup == "yes"
		If ($backup) {
			$result.backup_file = Backup-File -path $path -WhatIf:$check_mode
			# Ensure backward compatibility (deprecate in future)
			$result.backup = $result.backup_file
		}

		$writelines_params = @{
			outlines = $left
			path = $path
			linesep = $linesep
			encodingobj = $encodingobj
			validate = $validate
			check_mode = $check_mode
		}
		$after = WriteLines @writelines_params;

		if ($diff_support) {
			$result.diff.after = $after;
		}
	}

	$result.encoding = $encodingobj.WebName;
	$result.found = $found.Count;
	$result.msg = "$($found.Count) line(s) removed";

	Exit-Json $result;
}


# Parse the parameters file dropped by the Ansible machinery
$params = Parse-Args $args -supports_check_mode $true;
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false;
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false;

# Initialize defaults for input parameters.
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","destfile","name";
$regex = Get-AnsibleParam -obj $params -name "regex" -type "str" -aliases "regexp";
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent";
$line = Get-AnsibleParam -obj $params -name "line" -type "str";
$backrefs = Get-AnsibleParam -obj $params -name "backrefs" -type "bool" -default $false;
$insertafter = Get-AnsibleParam -obj $params -name "insertafter" -type "str";
$insertbefore = Get-AnsibleParam -obj $params -name "insertbefore" -type "str";
$create = Get-AnsibleParam -obj $params -name "create" -type "bool" -default $false;
$backup = Get-AnsibleParam -obj $params -name "backup" -type "bool" -default $false;
$validate = Get-AnsibleParam -obj $params -name "validate" -type "str";
$encoding = Get-AnsibleParam -obj $params -name "encoding" -type "str" -default "auto";
$newline = Get-AnsibleParam -obj $params -name "newline" -type "str" -default "windows" -validateset "unix","windows";

# Fail if the path is not a file
If (Test-Path -LiteralPath $path -PathType "container") {
	Fail-Json @{} "Path $path is a directory";
}

# Default to windows line separator - probably most common
$linesep = "`r`n"
If ($newline -eq "unix") {
	$linesep = "`n";
}

# Figure out the proper encoding to use for reading / writing the target file.

# The default encoding is UTF-8 without BOM
$encodingobj = [System.Text.UTF8Encoding] $false;

# If an explicit encoding is specified, use that instead
If ($encoding -ne "auto") {
	$encodingobj = [System.Text.Encoding]::GetEncoding($encoding);
}

# 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
# explicitly specified encoding set above.
ElseIf (Test-Path -LiteralPath $path) {

	# Get a sorted list of encodings with preambles, longest first
	$max_preamble_len = 0;
	$sortedlist = New-Object System.Collections.SortedList;
	Foreach ($encodinginfo in [System.Text.Encoding]::GetEncodings()) {
		$encoding = $encodinginfo.GetEncoding();
		$plen = $encoding.GetPreamble().Length;
		If ($plen -gt $max_preamble_len) {
			$max_preamble_len = $plen;
		}
		If ($plen -gt 0) {
			$sortedlist.Add(-($plen * 1000000 + $encoding.CodePage), $encoding) > $null;
		}
	}

	# 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 -LiteralPath $path;

	# Iterate through the sorted encodings, looking for a full match.
	$found = $false;
	Foreach ($encoding in $sortedlist.GetValueList()) {
		$preamble = $encoding.GetPreamble();
		If ($preamble -and $bom) {
			Foreach ($i in 0..($preamble.Length - 1)) {
				If ($i -ge $bom.Length) {
					break;
				}
				If ($preamble[$i] -ne $bom[$i]) {
					break;
				}
				ElseIf ($i + 1 -eq $preamble.Length) {
					$encodingobj = $encoding;
					$found = $true;
				}
			}
			If ($found) {
				break;
			}
		}
	}
}


# Main dispatch - based on the value of 'state', perform argument validation and
# call the appropriate handler function.
If ($state -eq "present") {

	If ($backrefs -and -not $regex) {
	    Fail-Json @{} "regexp= is required with backrefs=true";
	}

	If (-not $line) {
		Fail-Json @{} "line= is required with state=present";
	}

	If ($insertbefore -and $insertafter) {
		Add-Warning $result "Both insertbefore and insertafter parameters found, ignoring `"insertafter=$insertafter`""
	}

	If (-not $insertbefore -and -not $insertafter) {
		$insertafter = "EOF";
	}

	$present_params = @{
		path = $path
		regex = $regex
		line = $line
		insertafter = $insertafter
		insertbefore = $insertbefore
		create = $create
		backup = $backup
		backrefs = $backrefs
		validate = $validate
		encodingobj = $encodingobj
		linesep = $linesep
		check_mode = $check_mode
		diff_support = $diff_support
	}
	Present @present_params;

}
ElseIf ($state -eq "absent") {

	If (-not $regex -and -not $line) {
		Fail-Json @{} "one of line= or regexp= is required with state=absent";
	}

	$absent_params = @{
		path = $path
		regex = $regex
		line = $line
		backup = $backup
		validate = $validate
		encodingobj = $encodingobj
		linesep = $linesep
		check_mode = $check_mode
		diff_support = $diff_support
	}
	Absent @absent_params;
}