Skip to content

Commit 2bb6321

Browse files
🩹 [Patch]: Update Remove-Context to retrieve context names from Get-SecretInfo (#44)
## Description - Update `Remove-Context` to retrieve context names from `Get-SecretInfo`. ## 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 2c2a019 commit 2bb6321

File tree

2 files changed

+8
-38
lines changed

2 files changed

+8
-38
lines changed

src/functions/public/Context/Remove-Context.ps1

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,20 @@ filter Remove-Context {
4040
[CmdletBinding(SupportsShouldProcess)]
4141
param(
4242
# The name of the context to remove from the vault. Supports wildcard patterns.
43-
[Parameter(
44-
ValueFromPipeline,
45-
ValueFromPipelineByPropertyName
46-
)]
43+
[Parameter()]
4744
[SupportsWildcards()]
4845
[Alias('Context', 'ContextName')]
49-
[string] $Name = '*'
46+
[object] $Name = '*'
5047
)
5148

5249
$contextVault = Get-ContextVault
53-
$contextNames = Get-Context -Name $Name -AsPlainText | Select-Object -ExpandProperty Name
50+
$Name = "$($script:Config.Name)$Name"
51+
$contextNames = Get-SecretInfo -Vault $script:Config.Context.VaultName | Where-Object { $_.Name -like "$Name" } |
52+
Select-Object -ExpandProperty Name
5453

55-
Write-Verbose "Removing [$($contextNames.count)] contexts from vault [$($contextVault.Name)]"
54+
Write-Verbose "Removing [$($contextNames.count)] contexts from vault [$($contextVault.Name)] using filter [$Name]"
5655
foreach ($contextName in $contextNames) {
5756
Write-Verbose "Removing context [$contextName]"
58-
$contextName = $($script:Config.Name) + $contextName
5957
if ($PSCmdlet.ShouldProcess('Remove-Secret', $contextName)) {
6058
Write-Verbose "Removing secret [$contextName]"
6159
Remove-Secret -Name $contextName -Vault $contextVault.Name

tests/Context.Tests.ps1

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ Describe 'Context' {
5757
}
5858

5959
It 'Get-Context - Should return all contexts' {
60-
{ Get-Context } | Should -Not -Throw
60+
(Get-Context).Count | Should -Be 2
6161
}
6262

6363
It "Get-Context -Name '*' - Should return all contexts" {
64-
{ Get-Context -Name '*' } | Should -Not -Throw
64+
(Get-Context -Name '*').Count | Should -Be 2
6565
}
6666

6767
It "Get-Context -Name '' - Should return no contexts" {
@@ -125,34 +125,6 @@ Describe 'Context' {
125125
It 'Can delete using a wildcard' {
126126
{ Remove-Context -Name 'Test*' } | Should -Not -Throw
127127
}
128-
It 'Can delete contexts using pipeline' {
129-
$Context = @{
130-
Name = 'Test6'
131-
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
132-
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
133-
}
134-
135-
{ Set-Context -Context $Context } | Should -Not -Throw
136-
137-
$Context = @{
138-
Name = 'Test7'
139-
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
140-
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
141-
}
142-
143-
{ Set-Context -Context $Context } | Should -Not -Throw
144-
145-
$Context = @{
146-
Name = 'Test8'
147-
AccessToken = 'MySecret' | ConvertTo-SecureString -AsPlainText -Force
148-
RefreshToken = 'MyRefreshedSecret' | ConvertTo-SecureString -AsPlainText -Force
149-
}
150-
151-
{ Set-Context -Context $Context } | Should -Not -Throw
152-
153-
Get-Context -Name 'Test*' | Should -Not -BeNullOrEmpty
154-
{ Get-Context -Name 'Test*' | Remove-Context } | Should -Not -Throw
155-
}
156128
}
157129

158130
Context 'Set-ContextSetting' {

0 commit comments

Comments
 (0)