Skip to content

Commit 0d0b600

Browse files
🚀[Feature]: Add a config for how to match argument completers (#512)
This pull request introduces a new configurable completion mode for argument completers, allowing users to choose between 'StartsWith' and 'Contains' matching for tab completion throughout the module. It also improves the config initialization logic to better synchronize stored configuration objects with the current class definition, ensuring obsolete properties are removed and new ones are added automatically. - Fixes #509 **Configurable Completion Mode:** * Added a new `CompletionMode` property to the `GitHubConfig` class, allowing users to select between 'StartsWith' and 'Contains' matching for argument completion. This property is now included in the config display format. * Updated all argument completer scripts to use the `CompletionMode` from config, switching between prefix and substring matching as specified. This affects completers for contexts, organizations, environments, gitignore, licenses, permissions, and repository permissions. * Added a value completer for the `CompletionMode` property itself, suggesting 'StartsWith' and 'Contains' when setting the config. **Configuration Management Improvements:** * Enhanced the `Initialize-GitHubConfig` function to automatically add new properties and remove obsolete ones from stored configs, ensuring the config object stays in sync with the class definition. **Minor Usability Improvement:** * Made the `Context` parameter in `Get-GitHubContext` positional for easier use. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MariusStorhaug <[email protected]> Co-authored-by: Marius Storhaug <[email protected]>
1 parent 25832f9 commit 0d0b600

File tree

19 files changed

+114
-51
lines changed

19 files changed

+114
-51
lines changed

src/classes/public/Config/GitHubConfig.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
# The environment type, which is used to determine the context of the GitHub API calls.
3939
[string] $EnvironmentType
4040

41+
# The completion mode for argument completers. Options: 'StartsWith', 'Contains'.
42+
[string] $CompletionMode
43+
4144
# Simple parameterless constructor
4245
GitHubConfig() {}
4346

src/completers.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName User -ScriptBlock {
22
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
33
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
4-
4+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
55
$params = @{
66
Context = $fakeBoundParameters.Context
77
Verbose = $false
88
Debug = $false
99
}
10-
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
10+
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'User' -and $_.Target.Name -like $pattern } | ForEach-Object {
1111
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
1212
}
1313
}
1414
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Organization -ScriptBlock {
1515
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
1616
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
17-
17+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
1818
$params = @{
1919
Context = $fakeBoundParameters.Context
2020
Verbose = $false
2121
Debug = $false
2222
}
23-
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
23+
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Organization' -and $_.Target.Name -like $pattern } | ForEach-Object {
2424
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
2525
}
2626
}
2727
Register-ArgumentCompleter -CommandName Connect-GitHubApp -ParameterName Enterprise -ScriptBlock {
2828
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
2929
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
30-
30+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
3131
$params = @{
3232
Context = $fakeBoundParameters.Context
3333
Verbose = $false
3434
Debug = $false
3535
}
36-
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like "$wordToComplete*" } | ForEach-Object {
36+
Get-GitHubAppInstallation @params | Where-Object { $_.Type -eq 'Enterprise' -and $_.Target.Name -like $pattern } | ForEach-Object {
3737
[System.Management.Automation.CompletionResult]::new($_.Target.Name, $_.Target.Name, 'ParameterValue', $_.Target.Name)
3838
}
3939
}

src/formats/GitHubConfig.Format.ps1xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<ListItem>
4747
<PropertyName>RetryInterval</PropertyName>
4848
</ListItem>
49+
<ListItem>
50+
<PropertyName>CompletionMode</PropertyName>
51+
</ListItem>
4952
</ListItems>
5053
</ListEntry>
5154
</ListEntries>

src/functions/private/Config/Initialize-GitHubConfig.ps1

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,68 @@
3232
Write-Debug "Force: [$Force]"
3333
if ($Force) {
3434
Write-Debug 'Forcing initialization of GitHubConfig.'
35-
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
36-
$script:GitHub.Config = [GitHubConfig]$context
35+
$config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
36+
$script:GitHub.Config = [GitHubConfig]$config
3737
return
3838
}
3939

40-
Write-Debug "GitHubConfig ID: [$($script:GitHub.Config.ID)]"
4140
if ($null -ne $script:GitHub.Config) {
4241
Write-Debug 'GitHubConfig already initialized and available in memory.'
4342
return
4443
}
4544

4645
Write-Debug 'Attempt to load the stored GitHubConfig from ContextVault'
47-
$context = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault
48-
if ($context) {
46+
$config = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault
47+
if ($config) {
4948
Write-Debug 'GitHubConfig loaded into memory.'
5049

51-
Write-Debug 'Checking if new default properties are available in the stored context.'
50+
Write-Debug 'Synchronizing stored context with GitHubConfig class definition.'
5251
$needsUpdate = $false
53-
$defaultProperties = $script:GitHub.DefaultConfig.PSObject.Properties.Name
54-
foreach ($propName in $defaultProperties) {
55-
if (-not $context.PSObject.Properties.Name.Contains($propName)) {
52+
$validProperties = [GitHubConfig].GetProperties().Name
53+
$storedProperties = $config.PSObject.Properties.Name
54+
55+
# Add missing properties from DefaultConfig
56+
foreach ($propName in $validProperties) {
57+
Write-Debug "Validating property [$propName]"
58+
if (-not $storedProperties.Contains($propName)) {
5659
Write-Debug "Adding missing property [$propName] from DefaultConfig"
57-
$context | Add-Member -MemberType NoteProperty -Name $propName -Value $script:GitHub.DefaultConfig.$propName
60+
$defaultValue = $script:GitHub.DefaultConfig.$propName
61+
$config | Add-Member -MemberType NoteProperty -Name $propName -Value $defaultValue
62+
$needsUpdate = $true
63+
}
64+
}
65+
66+
# Remove obsolete properties that are no longer supported
67+
$propertiesToRemove = @()
68+
foreach ($propName in $storedProperties) {
69+
Write-Debug "Checking property [$propName] for obsolescence"
70+
if (-not $validProperties.Contains($propName)) {
71+
Write-Debug "Removing obsolete property [$propName] from stored context"
72+
$propertiesToRemove += $propName
5873
$needsUpdate = $true
5974
}
6075
}
76+
77+
# Remove the obsolete properties
78+
foreach ($propName in $propertiesToRemove) {
79+
$config.PSObject.Properties.Remove($propName)
80+
}
81+
6182
if ($needsUpdate) {
62-
Write-Debug 'Updating stored context with new default properties'
63-
$context = Set-Context -Context $context -Vault $script:GitHub.ContextVault -PassThru
83+
Write-Debug 'Updating stored context with synchronized properties'
84+
$config = Set-Context -Context $config -Vault $script:GitHub.ContextVault -PassThru
6485
}
6586

66-
$script:GitHub.Config = [GitHubConfig]$context
87+
$script:GitHub.Config = [GitHubConfig]$config
6788
return
6889
}
6990
Write-Debug 'Initializing GitHubConfig from defaults'
70-
$context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
71-
$script:GitHub.Config = [GitHubConfig]$context
91+
$config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru
92+
$script:GitHub.Config = [GitHubConfig]$config
7293
}
7394

7495
end {
7596
Write-Debug "[$stackPath] - End"
7697
}
7798
}
7899
#Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.1.3' }
79-

src/functions/public/Auth/Context/Get-GitHubContext.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
# The name of the context.
2525
[Parameter(
2626
Mandatory,
27-
ParameterSetName = 'Get a named context'
27+
ParameterSetName = 'Get a named context',
28+
Position = 0
2829
)]
2930
[Alias('Name')]
3031
[string] $Context,

src/functions/public/Auth/Context/completers.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Context -ScriptBlock {
22
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
33
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
4-
4+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
55
$contexts = @()
66
$hasAnonymousParameter = $false
77
$command = Get-Command -Name $commandName -ErrorAction SilentlyContinue
@@ -14,7 +14,7 @@
1414

1515
$contexts += Get-GitHubContext -ListAvailable -Verbose:$false -Debug:$false
1616
$contexts = $contexts | Sort-Object -Property Name
17-
$contexts | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
17+
$contexts | Where-Object { $_.Name -like $pattern } | ForEach-Object {
1818
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
1919
}
2020
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
Register-ArgumentCompleter -CommandName Set-GitHubConfig, Get-GitHubConfig, Remove-GitHubConfig -ParameterName Name -ScriptBlock {
22
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
33
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
4-
([GitHubConfig]).GetProperties().Name | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
4+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
5+
([GitHubConfig]).GetProperties().Name | Where-Object { $_ -like $pattern } | ForEach-Object {
56
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_ )
67
}
78
}
9+
10+
Register-ArgumentCompleter -CommandName Set-GitHubConfig -ParameterName Value -ScriptBlock {
11+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
12+
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
13+
if ($fakeBoundParameters.Name -eq 'CompletionMode') {
14+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
15+
@('StartsWith', 'Contains') | Where-Object { $_ -like $pattern } | ForEach-Object {
16+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
17+
}
18+
}
19+
}

src/functions/public/Environments/completers.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Where-Object { $_ -like '*GitHubEnvironment' }) -ParameterName Name -ScriptBlock {
33
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
44
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
5+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
56
$params = @{
67
Owner = $fakeBoundParameters.Owner
78
Repository = $fakeBoundParameters.Repository
@@ -10,14 +11,15 @@
1011
Debug = $false
1112
}
1213
$params | Remove-HashtableEntry -NullOrEmptyValues
13-
Get-GitHubEnvironment @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
14+
Get-GitHubEnvironment @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
1415
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
1516
}
1617
}
1718

1819
Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport) -ParameterName Environment -ScriptBlock {
1920
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
2021
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
22+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
2123
$params = @{
2224
Owner = $fakeBoundParameters.Owner
2325
Repository = $fakeBoundParameters.Repository
@@ -26,7 +28,7 @@ Register-ArgumentCompleter -CommandName ($script:PSModuleInfo.FunctionsToExport)
2628
Debug = $false
2729
}
2830
$params | Remove-HashtableEntry -NullOrEmptyValues
29-
Get-GitHubEnvironment @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
31+
Get-GitHubEnvironment @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
3032
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
3133
}
3234
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Register-ArgumentCompleter -CommandName Get-GitHubGitignore -ParameterName Name -ScriptBlock {
22
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
33
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
4+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
45
$params = @{
56
Context = $fakeBoundParameters.Context
67
Verbose = $false
78
Debug = $false
89
}
9-
Get-GitHubGitignore @params | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
10+
Get-GitHubGitignore @params | Where-Object { $_ -like $pattern } | ForEach-Object {
1011
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
1112
}
1213
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Register-ArgumentCompleter -CommandName Get-GitHubLicense -ParameterName Name -ScriptBlock {
22
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
33
$null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
4+
$pattern = switch (Get-GitHubConfig -Name CompletionMode) { 'Contains' { "*$wordToComplete*" } default { "$wordToComplete*" } }
45
$params = @{
56
Context = $fakeBoundParameters.Context
67
Verbose = $false
78
Debug = $false
89
}
9-
Get-GitHubLicense @params | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
10+
Get-GitHubLicense @params | Where-Object { $_.Name -like $pattern } | ForEach-Object {
1011
[System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
1112
}
1213
}

0 commit comments

Comments
 (0)