Skip to content

Commit

Permalink
Refactor Get-GitHubContext to streamline context retrieval and enhanc…
Browse files Browse the repository at this point in the history
…e argument completion
  • Loading branch information
MariusStorhaug committed Nov 24, 2024
1 parent 2bec003 commit 6577d63
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/functions/public/Auth/Context/Get-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions src/functions/public/Auth/Context/Remove-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

1 change: 1 addition & 0 deletions src/functions/public/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
9 changes: 5 additions & 4 deletions src/functions/public/Auth/Disconnect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6577d63

Please sign in to comment.