Skip to content

Commit

Permalink
Handle 's3' section in cli config
Browse files Browse the repository at this point in the history
  • Loading branch information
fireflycons committed Jan 12, 2020
1 parent 1660cee commit 146dba6
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 53 deletions.
116 changes: 90 additions & 26 deletions aws-toolbox/Private/Utils/Read-CliConfigurationFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ Function Read-CliConfigurationFile
[switch]$Credentials
)

function Read-SubSection
{
param
(
[System.IO.StreamReader]$StreamReader
)

$retval = @{}

$line = [String]::Empty
$finished = $false

while (-not $finished -and $null -ne ($line = $StreamReader.ReadLine()))
{
switch -regex ($line)
{
"\s+(.+?)\s*=\s*(.*)"
{
$name, $value = $matches[1..2]
$retval.Add($name, $value)
}

"^[^\s]"
{
# End of indented section
$finished = $true
}
}
}

$retval
}

$FilePath = $(

if ($Config -and $null -ne $env:AWS_CONFIG_FILE)
Expand Down Expand Up @@ -36,41 +69,72 @@ Function Read-CliConfigurationFile

if (Test-Path -Path $FilePath)
{
switch -regex -file $FilePath
try
{
"^\[(.+)\]$"
{
# Section
$section = $matches[1]
$configuration[$section] = @{ }
$CommentCount = 0
}
"^(;.*)$"
$sr = [System.IO.StreamReader]([System.IO.File]::OpenRead($FilePath))

$line = [String]::Empty

while ($null -ne ($line = $sr.ReadLine()))
{
# Comment
if (!($section))
switch -regex ($line)
{
$section = "No-Section"
$configuration[$section] = @{ }
"^\[(.+)\]$"
{
# Section
$section = $matches[1]
$configuration[$section] = @{ }
$CommentCount = 0
}
"^(;.*)$"
{
# Comment
if (!($section))
{
$section = "No-Section"
$configuration[$section] = @{ }
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$configuration[$section][$name] = $value
}
'^(.+?)\s*=\s*$'
{
# Start of indented section
# Key
if (!($section))
{
$section = "No-Section"
$configuration[$section] = @{ }
}
$name = $matches.1
$configuration[$section][$name] = Read-SubSection -StreamReader $sr
break
}
"(.+?)\s*=\s*(.*)"
{
# Key
if (!($section))
{
$section = "No-Section"
$configuration[$section] = @{ }
}
$name, $value = $matches[1..2]
$configuration[$section][$name] = $value
}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$configuration[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)"
}
finally
{
if ($sr)
{
# Key
if (!($section))
{
$section = "No-Section"
$configuration[$section] = @{ }
}
$name, $value = $matches[1..2]
$configuration[$section][$name] = $value
$sr.Dispose()
}
}
}

else
{
Write-Warning "No AWS $($PSCmdlet.ParameterSetName) file found."
Expand Down
55 changes: 38 additions & 17 deletions aws-toolbox/Private/Utils/Write-CliConfigurationFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,37 @@ Function Write-CliConfigurationFile

Begin
{
$Encoding = 'ASCII'
function Add-HashContent
{
param
(
[string]$FilePath,
[System.Collections.IDictionary]$Hash,
[int]$Indent = 0
)

$pad = " " * $Indent

Foreach ($j in $($Hash.keys | Sort-Object))
{
$value = $Hash[$j]

if ($j -match "^Comment[\d]+")
{
Add-Content -Path $FilePath -Value "$($pad)$($value)" -Encoding ascii
}
elseif ($value -is [System.Collections.IDictionary])
{
Add-Content -Path $FilePath -Value "$($pad)$j =" -Encoding ascii
Add-HashContent -Hash $value -Indent ($Indent + 2) -FilePath $FilePath
}
else
{
Add-Content -Path $FilePath -Value "$($pad)$j = $value" -Encoding ascii
}
}
}

$FilePath = $(

if ($Config -and $null -ne $env:AWS_CONFIG_FILE)
Expand Down Expand Up @@ -54,29 +84,20 @@ Function Write-CliConfigurationFile
if (-not ($($InputObject[$i].GetType().Name) -eq "Hashtable"))
{
#No Sections
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding ascii
}
else
{
#Sections
Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding
Foreach ($j in $($InputObject[$i].keys | Sort-Object))
{
if ($j -match "^Comment[\d]+")
{
Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding
}
else
{
Add-Content -Path $outFile -Value "$j = $($InputObject[$i][$j])" -Encoding $Encoding
}

}
Add-Content -Path $outFile -Value "" -Encoding $Encoding
Add-Content -Path $outFile -Value "[$i]" -Encoding ascii
Add-HashContent -Hash $InputObject[$i] -FilePath $outFile
Add-Content -Path $outFile -Value "" -Encoding ascii
}
}
}

End
{ }
{
$x = 1
}
}
45 changes: 35 additions & 10 deletions tests/aws-toolbox.Private.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ InModuleScope $ModuleName {
$newFileSize | Should -BeGreaterThan $fileSize -Because "the file should have been appended."
}


It 'Reads the config file created above' {

$script:storedData = Read-CliConfigurationFile -Config
Expand All @@ -420,22 +419,48 @@ InModuleScope $ModuleName {
It 'Has a [default] section' {

$script:storedData.ContainsKey('default') | Should -BeTrue
$script:sectionData = $script:storedData['default']
}

if ($script:storedData.ContainsKey('default'))
{
$script:sectionData = $script:storedData['default']
It 'Should have stored correct default region' {

$script:sectionData.ContainsKey('region') | Should -BeTrue
$script:sectionData['region'] | Should -Be 'eu-west-1'
}

It 'Should have stored correct output format' {

It 'Should have stored correct default region' {
$script:sectionData.ContainsKey('output') | Should -BeTrue
$script:sectionData['output'] | Should -Be 'json'
}

$script:sectionData.ContainsKey('region') | Should -BeTrue
$script:sectionData['region'] | Should -Be 'eu-west-1'
$s3Options = @{
max_concurrent_requests = 20
max_queue_size = 10000
multipart_threshold = "64MB"
multipart_chunksize = "16MB"
max_bandwidth = "50MB/s"
use_accelerate_endpoint = "true"
addressing_style = "path"
}

It 'Should have stored correct output format' {
It 'Writes S3 options' {

$initialData['default'].Add('s3', $s3Options)
$initialData | Write-CliConfigurationFile -Config
# Test it doesn't get borked reading then writing
Read-CliConfigurationFile -Config | Write-CliConfigurationFile -Config
$script:newConfig = Read-CliConfigurationFile -Config
}

$s3Options.Keys |
ForEach-Object {

$opt = $_

It "Should corrently set s3 option '$opt'" {

$script:sectionData.ContainsKey('output') | Should -BeTrue
$script:sectionData['output'] | Should -Be 'json'
$script:newConfig['default']['s3'][$opt] | Should -Be $s3Options[$opt]
}
}
}
Expand Down

0 comments on commit 146dba6

Please sign in to comment.