Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "UTF8BOM" and "UTF8NoBOM" to the acceptable values for Encoding parameter. #59

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
coverage - Fixes [Issue #50](https://github.com/dsccommunity/FileContentDsc/issues/50).
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #51](https://github.com/dsccommunity/FileContentDsc/issues/51).
- Renamed `master` branch to `main` - Fixes [Issue #53](https://github.com/dsccommunity/FileContentDsc/issues/53).
- Added `UTF8BOM` and `UTF8NoBOM` for Encoding parameter of the KeyValuePairFile and ReplaceText - Fixes [Issue #56](https://github.com/dsccommunity/FileContentDsc/issues/56).
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #57](https://github.com/dsccommunity/FileContentDsc/issues/57).
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #60](https://github.com/dsccommunity/FileContentDsc/issues/60).
- Added support for publishing code coverage to `CodeCov.io` and
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ stages:
vmImage: 'windows-latest'
steps:
- pwsh: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
$gitVersionObject.PSObject.Properties.ForEach{
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
Expand Down
81 changes: 65 additions & 16 deletions source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@

if (Test-Path -Path $Path)
{
$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'

Check warning on line 57 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L57

Added line #L57 was not covered by tests
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

if ($null -ne $fileContent)
{
Expand Down Expand Up @@ -180,15 +191,26 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding -Path $Path -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue

Check warning on line 204 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L204

Added line #L204 was not covered by tests
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8' -ErrorAction SilentlyContinue

Check warning on line 208 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L208

Added line #L208 was not covered by tests
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding -ErrorAction SilentlyContinue
}

$fileProperties = @{
Path = $Path
Expand Down Expand Up @@ -248,10 +270,13 @@
{
if ($results.Count -eq 0)
{
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
if ($PSBoundParameters.ContainsKey('Encoding') -and `
(($Encoding -eq $fileEncoding) -or `
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')))
{
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
}
else
{
Expand Down Expand Up @@ -281,7 +306,7 @@
$fileProperties.Add('Encoding', $Encoding)
}

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -362,7 +387,7 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -380,7 +405,19 @@
return ($Ensure -eq 'Absent')
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

if ($null -eq $fileContent)
{
Expand All @@ -390,7 +427,6 @@
}

$desiredConfigurationMatch = $true
$fileEncoding = Get-FileEncoding -Path $Path
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)
Expand Down Expand Up @@ -456,10 +492,23 @@

if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
if ($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*')
{
#If the Encoding specified as UTF8, Either UTF8NoBOM or UTF8BOM is acceptable
$desiredConfigurationMatch = $true

Check warning on line 498 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L498

Added line #L498 was not covered by tests
}
elseif ($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')
{
#If the Encoding specified as UTF8NoBOM, Either UTF8NoBOM or ASCII is acceptable
$desiredConfigurationMatch = $true

Check warning on line 503 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L503

Added line #L503 was not covered by tests
}
else
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)

$desiredConfigurationMatch = $false
$desiredConfigurationMatch = $false
}
}

return $desiredConfigurationMatch
Expand Down Expand Up @@ -543,7 +592,7 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DSC_KeyValuePairFile : OMI_BaseResource
[write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase;
[Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
91 changes: 59 additions & 32 deletions source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw

Check warning on line 47 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L47

Added line #L47 was not covered by tests
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

$text = ''

Expand All @@ -56,14 +67,14 @@
{
# No matches found - already in state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
$text = ($results.Value -join ',')

Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
$Path, $Search, $text)
$Path, $Search, $text)
} # if

return @{
Expand Down Expand Up @@ -136,15 +147,26 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw

Check warning on line 160 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L160

Added line #L160 was not covered by tests
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'

Check warning on line 164 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L164

Added line #L164 was not covered by tests
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

$fileProperties = @{
Path = $Path
Expand All @@ -155,25 +177,17 @@
if ($Type -eq 'Secret')
{
Write-Verbose -Message ($script:localizedData.StringReplaceSecretMessage -f `
$Path)
$Path)

$Text = $Secret.GetNetworkCredential().Password
}
elseif ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `

Check warning on line 186 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L186

Added line #L186 was not covered by tests
$Path, $Text)

# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}
# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)

Check warning on line 190 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L190

Added line #L190 was not covered by tests
}

if ($null -eq $fileContent)
Expand All @@ -194,7 +208,7 @@

$fileProperties.Add('Value', $fileContent)

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -259,7 +273,7 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -272,11 +286,22 @@
return $false
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

# Search the file content for any matches
$results = [regex]::Matches($fileContent, $Search)
Expand All @@ -293,19 +318,21 @@
}
if ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
if (($Encoding -eq $fileEncoding) -or `
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII'))
{
# No matches found and encoding is in desired state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)

return $true
}
else
{
# No matches found but encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
$fileEncoding, $Encoding)

return $false
}
Expand All @@ -331,12 +358,12 @@
if ($desiredConfigurationMatch)
{
Write-Verbose -Message ($script:localizedData.StringNoReplacementMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplacementRequiredMessage -f `
$Path, $Search)
$Path, $Search)
} # if

return $desiredConfigurationMatch
Expand Down Expand Up @@ -401,7 +428,7 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class DSC_ReplaceText : OMI_BaseResource
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
[Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
Loading
Loading