Skip to content

Commit f028ade

Browse files
authored
Merge pull request #656 from Icinga:feature/add_improved_document_writer
Feature: Adds new feature to write documents easier Adds new feature to write document content easier by storing it in memory first and then allowing to write it to disk at once with proper UTF8 encoding New cmdlets: * New-IcingaDocumentObject * Add-IcingaDocumentContent * Write-IcingaDocumentFile
2 parents e7dc4fe + 867c1cb commit f028ade

6 files changed

+154
-70
lines changed

doc/100-General/10-Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
3939
* [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well
4040
* [#644](https://github.com/Icinga/icinga-powershell-framework/pull/644) Adds progress bar output to repository interaction (sync, update, new) instead of plain text output
4141
* [#655](https://github.com/Icinga/icinga-powershell-framework/pull/655) Adds [IWKB](https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000016/) and test/manage Cmdlets for SCOM intercept counters
42+
* [#656](https://github.com/Icinga/icinga-powershell-framework/pull/656) Adds new feature to write document content easier by storing it in memory first and then allowing to write it to disk at once with proper UTF8 encoding
4243

4344
### Enhancements
4445

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function Add-IcingaDocumentContent()
2+
{
3+
param (
4+
[string]$Name = $null,
5+
[string]$Content = '',
6+
[switch]$NoNewLine = $FALSE
7+
);
8+
9+
if ([string]::IsNullOrEmpty($Name)) {
10+
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
11+
return;
12+
}
13+
14+
if ($Global:Icinga.Private.Documentation.ContainsKey($Name) -eq $false) {
15+
Write-IcingaConsoleError 'A documentation object with the name "{0}" does not exist' -Objects $Name;
16+
return;
17+
}
18+
19+
if ($NoNewLine) {
20+
$Global:Icinga.Private.Documentation[$Name]['Content'].Append($Content) | Out-Null;
21+
} else {
22+
$Global:Icinga.Private.Documentation[$Name]['Content'].AppendLine($Content) | Out-Null;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function New-IcingaDocumentObject()
2+
{
3+
param (
4+
[string]$Name = $null,
5+
[string]$Path = $null,
6+
[switch]$Force = $FALSE
7+
);
8+
9+
if ([string]::IsNullOrEmpty($Name)) {
10+
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
11+
return;
12+
}
13+
14+
if ([string]::IsNullOrEmpty($Path)) {
15+
Write-IcingaConsoleError 'You have to specify a path on where the document should be written to';
16+
return;
17+
}
18+
19+
if ($Global:Icinga.Private.Documentation.ContainsKey($Name)) {
20+
if ($Force -eq $FALSE) {
21+
Write-IcingaConsoleError 'A documentation object with the name "{0}" does already exist in memory. Use -Force to overwrite it' -Objects $Name;
22+
return;
23+
}
24+
25+
$Global:Icinga.Private.Documentation[$Name] = @{
26+
'Path' = $Path;
27+
'Content' = (New-Object -TypeName 'System.Text.StringBuilder');
28+
}
29+
} else {
30+
$Global:Icinga.Private.Documentation.Add(
31+
$Name,
32+
@{
33+
'Path' = $Path;
34+
'Content' = (New-Object -TypeName 'System.Text.StringBuilder');
35+
}
36+
);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function Write-IcingaDocumentFile()
2+
{
3+
param (
4+
[string]$Name = $null,
5+
[switch]$ClearCache = $FALSE
6+
);
7+
8+
if ([string]::IsNullOrEmpty($Name)) {
9+
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
10+
return;
11+
}
12+
13+
Write-IcingaFileSecure -File $Global:Icinga.Private.Documentation[$Name]['Path'] -Value $Global:Icinga.Private.Documentation[$Name]['Content'].ToString();
14+
15+
if ($ClearCache) {
16+
$Global:Icinga.Private.Documentation.Remove($Name);
17+
}
18+
}

lib/core/framework/New-IcingaEnvironmentVariable.psm1

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function New-IcingaEnvironmentVariable()
2424
$Global:Icinga.Add('Private', @{ });
2525

2626
$Global:Icinga.Private.Add('Daemons', @{ });
27+
$Global:Icinga.Private.Add('Documentation', @{ });
2728
$Global:Icinga.Private.Add('Timers', @{ });
2829
$Global:Icinga.Private.Add('ProgressStatus', @{ });
2930

0 commit comments

Comments
 (0)