Skip to content
2 changes: 1 addition & 1 deletion src/functions/public/Get-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function Get-Context {

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
}

process {
Expand Down
27 changes: 10 additions & 17 deletions src/functions/public/Get-ContextInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,29 @@
)

begin {
$debug = $DebugPreference -eq 'Continue'
if ($debug) {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
}
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Begin"
}

process {
$vaults = foreach ($vaultName in $Vault) {
Get-ContextVault -Name $vaultName -ErrorAction Stop
}
if ($debug) {
Write-Debug "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'."
}
Write-Verbose "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'."

$files = foreach ($vaultObject in $vaults) {
Get-ChildItem -Path $vaultObject.Path -Filter *.json -File
}
if ($debug) {
Write-Debug "[$stackPath] - Found $($files.Count) context file(s) in vault(s)."
}
Write-Verbose "[$stackPath] - Found $($files.Count) context file(s) in vault(s)."

foreach ($file in $files) {
$contextInfo = Get-Content -Path $file.FullName | ConvertFrom-Json
if ($debug) {
Write-Debug "[$stackPath] - Processing file: $($file.FullName)"
$contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug "[$stackPath] $_" }
}
if ($contextInfo.ID -like $ID) {
$contextInfo
Write-Verbose "[$stackPath] - Processing file: $($file.FullName)"
$contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }
foreach ($IDItem in $ID) {
Comment thread
MariusStorhaug marked this conversation as resolved.
if ($contextInfo.ID -like $IDItem) {
$contextInfo
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions/public/Remove-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
$contextId = $contextInfo.ID

if ($PSCmdlet.ShouldProcess("Context '$contextId'", 'Remove')) {
Write-Debug "[$stackPath] - Removing context [$contextId]"
Write-Verbose "[$stackPath] - Removing context [$contextId]"
$contextInfo.Path | Remove-Item -Force -ErrorAction Stop
Write-Output "Removed item: $contextId"
Write-Verbose "[$stackPath] - Removed item: $contextId"
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/functions/public/Rename-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
Renames the context 'PSModule.GitHub' to 'PSModule.GitHub2' using pipeline input.

.OUTPUTS
[System.String]
object

.NOTES
The confirmation message indicating the successful renaming of the context.

.LINK
https://psmodule.io/Context/Functions/Rename-Context/
#>

[OutputType([object])]
[CmdletBinding(SupportsShouldProcess)]
param (
# The ID of the context to rename.
Expand Down Expand Up @@ -66,21 +66,21 @@

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
}

process {
$context = Get-Context -ID $ID -Vault $Vault
if (-not $context) {
throw "Context with ID '$ID' not found$(if ($Vault) { " in vault '$Vault'" })."
throw "Context with ID '$ID' not found in vault '$Vault'"
}

$existingContext = Get-Context -ID $NewID -Vault $Vault
if ($existingContext -and -not $Force) {
throw "Context with ID '$NewID' already exists$(if ($Vault) { " in vault '$Vault'" })."
throw "Context with ID '$NewID' already exists in vault '$Vault'"
}

if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID'$(if ($Vault) { " in vault '$Vault'" })")) {
if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID' in vault '$Vault'")) {
$context | Set-Context -ID $NewID -Vault $Vault
Remove-Context -ID $ID -Vault $Vault
}
Expand Down
8 changes: 4 additions & 4 deletions src/functions/public/Set-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ function Set-Context {

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
}

process {
$vaultObject = Set-ContextVault -Name $Vault
Write-Verbose "$($vaultObject | Format-List | Out-String)"
$vaultObject | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }

if ($context -is [System.Collections.IDictionary]) {
$Context = [PSCustomObject]$Context
Expand All @@ -92,7 +92,7 @@ function Set-Context {

$contextInfo = Get-ContextInfo -ID $ID -Vault $Vault
Write-Verbose 'Context info:'
$contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }
if (-not $contextInfo) {
Write-Verbose "[$stackPath] - Creating context [$ID] in [$Vault]"
$guid = [Guid]::NewGuid().Guid
Expand All @@ -112,7 +112,7 @@ function Set-Context {
Context = ConvertTo-SodiumSealedBox -Message $contextJson -PublicKey $keys.PublicKey
} | ConvertTo-Json -Depth 5
Write-Verbose 'Content:'
$content | ConvertTo-Json -Depth 5 | Out-String -Stream | ForEach-Object { Write-Verbose $_ }
$content | ConvertTo-Json -Depth 5 | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }

if ($PSCmdlet.ShouldProcess("file: [$contextPath]", 'Set content')) {
Write-Verbose "[$stackPath] - Setting context [$ID] in vault [$Vault]"
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Vault/Get-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
if (-not (Test-Path -Path $script:Config.RootPath)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Vault/Remove-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
$vaults = Get-ContextVault
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Vault/Reset-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
}

process {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/public/Vault/Set-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Set-ContextVault {

begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Write-Debug "[$stackPath] - Begin"
}

process {
Expand Down
44 changes: 44 additions & 0 deletions tests/Context.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ Describe 'Context' {
{ Get-Context -ID $null -Vault 'VaultA' } | Should -Not -Throw
Get-Context -ID $null -Vault 'VaultA' | Should -BeNullOrEmpty
}
It 'Get-Context -ID array - Should return only specified contexts in VaultA' {
$ids = @('TestID1', 'TestID2')
$results = Get-Context -ID $ids -Vault 'VaultA'
$results | Should -Not -BeNullOrEmpty
$results.Count | Should -Be 2
$results.ID | Should -Contain 'TestID1'
$results.ID | Should -Contain 'TestID2'
$results.ID | Should -Not -Contain 'TestID3'
}
It 'Get-Context -ID single - Should not have output leakage (returns only one context)' {
$id = 'TestID1'
$results = Get-Context -ID $id -Vault 'VaultA'
$results | Should -Not -BeNullOrEmpty
$results.Count | Should -Be 1
$results.ID | Should -Be $id
}
It 'Get-Context -ID single (nonexistent) - Should not have output leakage (returns nothing)' {
$id = 'NonExistentContext'
$results = Get-Context -ID $id -Vault 'VaultA'
$results | Should -BeNullOrEmpty
}
It 'Get-Context -ID array with one valid and one invalid - Should not have output leakage (returns only valid)' {
$ids = @('TestID1', 'NonExistentContext')
$results = Get-Context -ID $ids -Vault 'VaultA'
$results | Should -Not -BeNullOrEmpty
$results.Count | Should -Be 1
$results.ID | Should -Be 'TestID1'
}
It 'Get-Context -ID with whitespace or null - Should not have output leakage (returns nothing)' {
$results = Get-Context -ID ' ' -Vault 'VaultA'
$results | Should -BeNullOrEmpty
$results = Get-Context -ID $null -Vault 'VaultA'
$results | Should -BeNullOrEmpty
}
}

Context 'Remove-Context' {
Expand Down Expand Up @@ -304,5 +338,15 @@ Describe 'Context' {
$result = Get-ContextInfo -ID 'NonExistentContext' -Vault 'VaultA'
$result | Should -BeNullOrEmpty
}

It 'Should return only specified contexts for multiple IDs in VaultA' {
$ids = @('TestID1', 'TestID2')
$results = Get-ContextInfo -ID $ids -Vault 'VaultA'
$results | Should -Not -BeNullOrEmpty
$results.Count | Should -Be 2
$results.ID | Should -Contain 'TestID1'
$results.ID | Should -Contain 'TestID2'
$results.ID | Should -Not -Contain 'TestID3'
}
}
}
Loading