From 6577d63534787b95d286c362a97ac324b4fea4db Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 24 Nov 2024 11:40:57 +0100 Subject: [PATCH] Refactor Get-GitHubContext to streamline context retrieval and enhance argument completion --- .../public/Auth/Context/Get-GitHubContext.ps1 | 9 ++++----- .../public/Auth/Context/Remove-GitHubContext.ps1 | 11 +++++++++++ .../public/Auth/Context/Set-GitHubContext.ps1 | 1 + .../public/Auth/Context/Set-GitHubDefaultContext.ps1 | 7 ++++--- .../Auth/ContextSetting/Get-GitHubContextSetting.ps1 | 10 ++++++++++ .../Auth/ContextSetting/Set-GitHubContextSetting.ps1 | 10 ++++++++++ .../public/Auth/Disconnect-GitHubAccount.ps1 | 9 +++++---- 7 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 index 928c43cf..e7676525 100644 --- a/src/functions/public/Auth/Context/Get-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Get-GitHubContext.ps1 @@ -66,9 +66,8 @@ Register-ArgumentCompleter -CommandName Get-GitHubContext -ParameterName Context param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - $defaultContext = Get-GitHubConfig -Name 'DefaultContext' - - Get-Context -ID "$wordToComplete*" -Verbose:$false | Where-Object { $_.ContextID -ne $defaultContext } | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) - } + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } } diff --git a/src/functions/public/Auth/Context/Remove-GitHubContext.ps1 b/src/functions/public/Auth/Context/Remove-GitHubContext.ps1 index a1600713..d00d0816 100644 --- a/src/functions/public/Auth/Context/Remove-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Remove-GitHubContext.ps1 @@ -40,3 +40,14 @@ filter Remove-GitHubContext { Write-Verbose "[$commandName] - End" } + +Register-ArgumentCompleter -CommandName Get-GitHubContext -ParameterName Context -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } +} + diff --git a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 index 64e7a543..b4183154 100644 --- a/src/functions/public/Auth/Context/Set-GitHubContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubContext.ps1 @@ -156,6 +156,7 @@ function Set-GitHubContext { Write-Verbose "Setting the GitHub context [$newContextID]" Write-Verbose (Get-SecretInfo | Out-String) Write-Verbose (Get-SecretInfo | Get-Secret -AsPlainText | Out-String) + Remove-Context -ID $tempContextID Set-Context -ID "$($script:Config.Name)/$newContextID" -Context $context if ($Default) { Set-GitHubDefaultContext -Context $newContextID diff --git a/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 b/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 index 67eaae4b..6916fdba 100644 --- a/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 +++ b/src/functions/public/Auth/Context/Set-GitHubDefaultContext.ps1 @@ -33,7 +33,8 @@ Register-ArgumentCompleter -CommandName Set-GitHubDefaultContext -ParameterName $defaultContext = Get-GitHubConfig -Name 'DefaultContext' - Get-Context -ID "$wordToComplete*" -Verbose:$false | Where-Object { $_.ContextID -ne $defaultContext } | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) - } + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" -and $_.ContextID -ne $defaultContext } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } } diff --git a/src/functions/public/Auth/ContextSetting/Get-GitHubContextSetting.ps1 b/src/functions/public/Auth/ContextSetting/Get-GitHubContextSetting.ps1 index 1aa40fc0..d4bda7c3 100644 --- a/src/functions/public/Auth/ContextSetting/Get-GitHubContextSetting.ps1 +++ b/src/functions/public/Auth/ContextSetting/Get-GitHubContextSetting.ps1 @@ -38,3 +38,13 @@ function Get-GitHubContextSetting { Write-Verbose "[$commandName] - End" } + +Register-ArgumentCompleter -CommandName Get-GitHubContext -ParameterName Context -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } +} diff --git a/src/functions/public/Auth/ContextSetting/Set-GitHubContextSetting.ps1 b/src/functions/public/Auth/ContextSetting/Set-GitHubContextSetting.ps1 index 68c11cfe..57ae8980 100644 --- a/src/functions/public/Auth/ContextSetting/Set-GitHubContextSetting.ps1 +++ b/src/functions/public/Auth/ContextSetting/Set-GitHubContextSetting.ps1 @@ -109,3 +109,13 @@ function Set-GitHubContextSetting { Write-Verbose "[$commandName] - End" } + +Register-ArgumentCompleter -CommandName Get-GitHubContext -ParameterName Context -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } +} diff --git a/src/functions/public/Auth/Disconnect-GitHubAccount.ps1 b/src/functions/public/Auth/Disconnect-GitHubAccount.ps1 index 4b489b86..07a95dcd 100644 --- a/src/functions/public/Auth/Disconnect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Disconnect-GitHubAccount.ps1 @@ -52,11 +52,12 @@ Write-Verbose "[$commandName] - End" } -Register-ArgumentCompleter -CommandName Disconnect-GitHubAccount -ParameterName Context -ScriptBlock { +Register-ArgumentCompleter -CommandName Get-GitHubContext -ParameterName Context -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter - Get-Context -ID "$wordToComplete*" -Verbose:$false | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) - } + Get-GitHubContext -ListAvailable | Where-Object { $_.ContextID -like "$wordToComplete*" } -Verbose:$false | + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.ContextID, $_.ContextID, 'ParameterValue', $_.ContextID) + } }