Skip to content

Commit

Permalink
#23 Work in pogress
Browse files Browse the repository at this point in the history
  • Loading branch information
fireflycons committed Jan 11, 2020
1 parent b45832c commit c01bc7a
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 1 deletion.
40 changes: 40 additions & 0 deletions aws-toolbox/Private/IAM/Get-CredentialProcess.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Get-CredentialProcess
{
$edition = 'Desktop'

if (Get-Variable -Name PSEdition)
{
$edition = $PSEdition
}

$process = @{
PowerShell = $(
if ($edition -eq 'Desktop')
{
(Get-Command 'PowerShell.exe').Source
}
else
{
(Get-Command 'pwsh').Source
}
)
Module = (Get-PSCallStack)[0].InvocationInfo.MyCommand.Module.Name
}

$sb = New-Object System.Text.StringBuilder

if ($process.PowerShell -match '\s')
{
$sb.Append("`"$($process.PowerShell)`"") | Out-Null
}
else
{
$sb.Append($process.PowerShell) | Out-Null
}

$sb.Append(" -Command `"Import-Module $($process.Module); Set-AwsCredentail {0}; Get-ATIAMSessionCredentials -AwsCli`"") | Out-Null

$process['CredentialProcess'] = $sb.ToString()

$process
}
78 changes: 78 additions & 0 deletions aws-toolbox/Private/Utils/Read-CliConfigurationFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Function Read-CliConfigurationFile
{
param
(
[Parameter(Mandatory, ParameterSetName = 'config')]
[switch]$Config,

[Parameter(Mandatory, ParameterSetName = 'credentials')]
[switch]$Credentials,

[string]$AlternateDirectory
)

$FilePath = $(

if (-not [string]::IsNullOrEmpty($AlternateDirectory))
{
Join-Path $AlternateDirectory $PSCmdlet.ParameterSetName
}
else
{
if ((Get-OperatingSystem) -eq 'Windows')
{
Join-Path $env:USERPROFILE ".aws\$($PSCmdlet.ParameterSetName)"
}
else
{
"~/.aws/$($PSCmdlet.ParameterSetName)"
}
}
)

$configuration = @{ }

if (Test-Path -Path $FilePath)
{
switch -regex -file $FilePath
{
"^\[(.+)\]$"
{
# 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*(.*)"
{
# Key
if (!($section))
{
$section = "No-Section"
$configuration[$section] = @{ }
}
$name, $value = $matches[1..2]
$configuration[$section][$name] = $value
}
}
}
else
{
Write-Warning "No AWS $($PSCmdlet.ParameterSetName) file found."
}

return $configuration
}
79 changes: 79 additions & 0 deletions aws-toolbox/Private/Utils/Write-CliConfigurationFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Function Write-CliConfigurationFile
{
Param
(
[Parameter(Mandatory, ParameterSetName = 'config')]
[switch]$Config,

[Parameter(Mandatory, ParameterSetName = 'credentials')]
[switch]$Credentials,

[ValidateNotNullOrEmpty()]
[Parameter(ValueFromPipeline = $True, Mandatory = $True)]
[Hashtable]$InputObject,

[string]$AlternateDirectory
)

Begin
{
$Encoding = 'ASCII'
$FilePath = $(
if (-not [string]::IsNullOrEmpty($AlternateDirectory))
{
Join-Path $AlternateDirectory $PSCmdlet.ParameterSetName
}
else
{
if ((Get-OperatingSystem) -eq 'Windows')
{
Join-Path $env:USERPROFILE ".aws\$($PSCmdlet.ParameterSetName)"
}
else
{
"~/.aws/$($PSCmdlet.ParameterSetName)"
}
}
)
}

Process
{
$outFile = New-Item -ItemType file -Path $Filepath -Force

if (-not ($outFile))
{
Throw "Could not create File"
}

foreach ($i in $InputObject.keys)
{
if (-not ($($InputObject[$i].GetType().Name) -eq "Hashtable"))
{
#No Sections
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding
}
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
}
}
}

End
{ }
}
1 change: 1 addition & 0 deletions aws-toolbox/aws-toolbox.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if (-not ((Test-ATEC2IsRunningInEC2) -or $null -ne (Get-Item variable:StoredAWSC

$script:PluginConfig = Import-PluginConfiguration
$script:moduleConfig = Import-AwsToolboxConfiguration
$script:isPester = $null -ne (Get-PSCallStack | Where-Object Command -eq 'Invoke-Pester')

# Export public functions
Export-ModuleMember -Function $Public.Basename
Loading

0 comments on commit c01bc7a

Please sign in to comment.