Skip to content

Commit

Permalink
Refactor Get-GitHubContext for improved structure and verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Dec 8, 2024
1 parent 6f8224a commit c33f075
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/functions/public/Auth/Context/Get-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,50 @@ function Get-GitHubContext {
[switch] $ListAvailable
)

$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"

switch ($PSCmdlet.ParameterSetName) {
'NamedContext' {
$ID = "$($script:GitHub.Config.ID)/$Context"
Write-Verbose "Getting available contexts for [$ID]"
}
'ListAvailableContexts' {
Write-Debug "ListAvailable: [$ListAvailable]"
$ID = "$($script:GitHub.Config.ID)/*"
Write-Verbose "Getting available contexts for [$ID]"
}
'__AllParameterSets' {
$ID = "$($script:GitHub.Config.ID)/$($script:GitHub.Config.DefaultContext)"
if ([string]::IsNullOrEmpty($ID)) {
throw "No default GitHub context found. Please run 'Set-GitHubDefaultContext' or 'Connect-GitHub' to configure a GitHub context."
}
Write-Verbose "Getting the default context: [$ID]"
}
begin {
$commandName = $MyInvocation.MyCommand.Name
Write-Verbose "[$commandName] - Start"
}

Get-Context -ID $ID | ForEach-Object {
switch ($_.Type) {
'User' {
[UserGitHubContext]$_
process {

switch ($PSCmdlet.ParameterSetName) {
'NamedContext' {
Write-Verbose "NamedContext: [$Context]"
$ID = "$($script:GitHub.Config.ID)/$Context"
Write-Verbose "Getting available contexts for [$ID]"
}
'App' {
[AppGitHubContext]$_
'ListAvailableContexts' {
Write-Verbose "ListAvailable: [$ListAvailable]"
$ID = "$($script:GitHub.Config.ID)/*"
Write-Verbose "Getting available contexts for [$ID]"
}
'Installation' {
[InstallationGitHubContext]$_
'__AllParameterSets' {
Write-Verbose "Getting default context."
$ID = "$($script:GitHub.Config.ID)/$($script:GitHub.Config.DefaultContext)"
if ([string]::IsNullOrEmpty($ID)) {
throw "No default GitHub context found. Please run 'Set-GitHubDefaultContext' or 'Connect-GitHub' to configure a GitHub context."
}
Write-Verbose "Getting the default context: [$ID]"
}
}

Get-Context -ID $ID | ForEach-Object {
switch ($_.Type) {
'User' {
[UserGitHubContext]$_
}
'App' {
[AppGitHubContext]$_
}
'Installation' {
[InstallationGitHubContext]$_
}
}
}
}

Write-Verbose "[$commandName] - End"
end {
Write-Verbose "[$commandName] - End"
}
}

0 comments on commit c33f075

Please sign in to comment.