Skip to content

Commit 1baa2f0

Browse files
🪲 [Fix]: Add -PassThru parameter to Set-Context and Set-ContextVault functions for pipeline support (#93)
## Description This pull request introduces a new `-PassThru` parameter to the `Set-Context` and `Set-ContextVault` functions, allowing users to pass objects through the pipeline. Corresponding updates have been made to the logic and tests to support this functionality. ### Enhancements to `Set-Context` and `Set-ContextVault` functions: * **Added `-PassThru` parameter to `Set-Context`:** This optional parameter enables the function to return the context object through the pipeline if specified. (`src/functions/public/Set-Context.ps1`, [[1]](diffhunk://#diff-d12895be2e58b33d275f1d10ec54bd8ee0b555bef81f53d6e39ca1722ea58f44L70-R74) [[2]](diffhunk://#diff-d12895be2e58b33d275f1d10ec54bd8ee0b555bef81f53d6e39ca1722ea58f44R126-R129) * **Modified `Set-ContextVault` calls within `Set-Context`:** Updated to include the `-PassThru` parameter when invoking `Set-ContextVault`, ensuring consistent behavior. (`src/functions/public/Set-Context.ps1`, [src/functions/public/Set-Context.ps1L79-R83](diffhunk://#diff-d12895be2e58b33d275f1d10ec54bd8ee0b555bef81f53d6e39ca1722ea58f44L79-R83)) ### Enhancements to `Set-ContextVault` function: * **Added `-PassThru` parameter to `Set-ContextVault`:** This optional parameter allows the function to return the created or updated vault object through the pipeline if specified. (`src/functions/public/Vault/Set-ContextVault.ps1`, [[1]](diffhunk://#diff-c61e62f0db098ac07ec081939fe582f191491d491337e1829c95e64ec28e6e8bL28-R32) [[2]](diffhunk://#diff-c61e62f0db098ac07ec081939fe582f191491d491337e1829c95e64ec28e6e8bR59-R63) ### Test updates: * **Updated tests for `Set-ContextVault`:** Modified test cases to include the `-PassThru` parameter, ensuring the returned objects meet the expected type and content. (`tests/ContextVaults.Tests.ps1`, [tests/ContextVaults.Tests.ps1L41-R57](diffhunk://#diff-0894d6e7273e8c5b7b17bea72b02ec2ce764fbe3478fd1a83e09b3f4ea3dd636L41-R57)) ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [x] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 818b0e8 commit 1baa2f0

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

‎src/functions/private/Get-ContextVaultKeyPair.ps1‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333

3434
process {
35-
$vaultObject = Set-ContextVault -Name $Vault
35+
$vaultObject = Set-ContextVault -Name $Vault -PassThru
3636
$shardPath = Join-Path -Path $vaultObject.Path -ChildPath $script:Config.ShardFileName
3737
$fileShard = Get-Content -Path $shardPath
3838
$machineShard = [System.Environment]::MachineName

‎src/functions/public/Rename-Context.ps1‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@
6161
# The name of the vault containing the context.
6262
[Parameter()]
6363
[ArgumentCompleter({ Complete-ContextVaultName @args })]
64-
[string] $Vault
64+
[string] $Vault,
65+
66+
# Pass the context through the pipeline.
67+
[Parameter()]
68+
[switch] $PassThru
6569
)
6670

6771
begin {
@@ -81,7 +85,7 @@
8185
}
8286

8387
if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID' in vault '$Vault'")) {
84-
$context | Set-Context -ID $NewID -Vault $Vault
88+
$context | Set-Context -ID $NewID -Vault $Vault -PassThru:$PassThru
8589
Remove-Context -ID $ID -Vault $Vault
8690
}
8791
}

‎src/functions/public/Set-Context.ps1‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ function Set-Context {
6767
# The name of the vault to store the context in.
6868
[Parameter(Mandatory)]
6969
[ArgumentCompleter({ Complete-ContextVaultName @args })]
70-
[string] $Vault
70+
[string] $Vault,
71+
72+
# Pass the context through the pipeline.
73+
[Parameter()]
74+
[switch] $PassThru
7175
)
7276

7377
begin {
@@ -76,7 +80,7 @@ function Set-Context {
7680
}
7781

7882
process {
79-
$vaultObject = Set-ContextVault -Name $Vault
83+
$vaultObject = Set-ContextVault -Name $Vault -PassThru
8084
$vaultObject | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }
8185

8286
if ($context -is [System.Collections.IDictionary]) {
@@ -119,7 +123,9 @@ function Set-Context {
119123
Set-Content -Path $contextPath -Value $content
120124
}
121125

122-
Get-Context -ID $ID -Vault $Vault
126+
if ($PassThru) {
127+
Get-Context -ID $ID -Vault $Vault
128+
}
123129
}
124130

125131
end {

‎src/functions/public/Vault/Reset-ContextVault.ps1‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929

3030
# The vault object to reset.
3131
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'As ContextVault')]
32-
[ContextVault[]] $InputObject
32+
[ContextVault[]] $InputObject,
33+
34+
# Pass the context through the pipeline.
35+
[Parameter()]
36+
[switch] $PassThru
3337
)
3438

3539
begin {
@@ -45,7 +49,7 @@
4549
Write-Verbose "Resetting ContextVault [$($vault.Name)] at path [$($vault.Path)]"
4650
if ($PSCmdlet.ShouldProcess("ContextVault: [$($vault.Name)]", 'Reset')) {
4751
Remove-ContextVault -Name $($vault.Name) -Confirm:$false
48-
Set-ContextVault -Name $($vault.Name)
52+
Set-ContextVault -Name $($vault.Name) -PassThru:$PassThru
4953
Write-Verbose "ContextVault [$($vault.Name)] reset successfully."
5054
}
5155
}

‎src/functions/public/Vault/Set-ContextVault.ps1‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function Set-ContextVault {
2525
# The name of the vault to create or update.
2626
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
2727
[ArgumentCompleter({ Complete-ContextVaultName @args })]
28-
[string[]] $Name
28+
[string[]] $Name,
29+
30+
# Pass the context through the pipeline.
31+
[Parameter()]
32+
[switch] $PassThru
2933
)
3034

3135
begin {
@@ -52,7 +56,9 @@ function Set-ContextVault {
5256
}
5357
}
5458

55-
[ContextVault]::new($vaultName, $vaultPath)
59+
if ($PassThru) {
60+
[ContextVault]::new($vaultName, $vaultPath)
61+
}
5662
}
5763
}
5864

‎tests/ContextVaults.Tests.ps1‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ Describe 'ContextVault' {
3838
}
3939

4040
It 'Should create a new vault with a single name parameter' {
41-
$result = Set-ContextVault -Name 'test-vault1'
41+
$result = Set-ContextVault -Name 'test-vault1' -PassThru
4242
$result | Should -Not -BeNullOrEmpty
4343
$result | Should -BeOfType [ContextVault]
4444
$result.Name | Should -Be 'test-vault1'
4545
$result.Path | Should -Not -BeNullOrEmpty
4646
}
4747

4848
It 'Should create multiple vaults from array parameter' {
49-
$results = Set-ContextVault -Name 'test-vault2', 'test-vault3'
49+
$results = Set-ContextVault -Name 'test-vault2', 'test-vault3' -PassThru
5050
$results | Should -HaveCount 2
5151
$results | ForEach-Object { $_ | Should -BeOfType [ContextVault] }
5252
$results[0].Name | Should -Be 'test-vault2'
5353
$results[1].Name | Should -Be 'test-vault3'
5454
}
5555

5656
It 'Should accept pipeline input for vault creation' {
57-
$results = 'test-pipeline1', 'test-pipeline2' | Set-ContextVault
57+
$results = 'test-pipeline1', 'test-pipeline2' | Set-ContextVault -PassThru
5858
$results | Should -HaveCount 2
5959
$results | ForEach-Object { $_ | Should -BeOfType [ContextVault] }
6060
$results.Name | Should -Contain 'test-pipeline1'
@@ -208,7 +208,7 @@ Describe 'ContextVault' {
208208

209209
It 'Should support pipeline operations with variables' {
210210
$testVaults = @('pipeline-var1', 'pipeline-var2')
211-
$results = $testVaults | Set-ContextVault
211+
$results = $testVaults | Set-ContextVault -PassThru
212212
$results | Should -HaveCount 2
213213

214214
$getResults = Get-ContextVault -Name $testVaults

0 commit comments

Comments
 (0)