Private/Register-ArgCompleters.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function Register-ArgCompleters {
    [CmdletBinding()]
    param()

    # setup the argument completers
    # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-5.1

    # Plugin name
    $PluginNameCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # We can't use the normal ModuleBase method to get the plugin folder here because
        # the completer script block doesn't run in the module's context.
        $names = $script:Plugins.Keys | Sort-Object
        $names | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }
    $PluginCommands = 'New-PACertificate','New-PAOrder','Set-PAOrder','Get-PAPlugin','Publish-Challenge','Save-Challenge','Unpublish-Challenge'
    Register-ArgumentCompleter -CommandName $PluginCommands -ParameterName 'Plugin' -ScriptBlock $PluginNameCompleter

    # Account ID
    $IDCommands = 'Get-PAAccount','Set-PAAccount','Remove-PAAccount','Export-PAAccountKey'
    Register-ArgumentCompleter -CommandName $IDCommands -ParameterName 'ID' -ScriptBlock {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # nothing to auto complete if we don't have a server selected
        if ([String]::IsNullOrWhiteSpace($script:Dir.Folder)) { return }

        $ids = (Get-ChildItem -Path $script:Dir.Folder | Where-Object { $_ -is [IO.DirectoryInfo] }).BaseName
        $ids | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    # KeyLength
    $KeyLengthCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        $commonLengths = '2048','3072','4096','ec-256','ec-384'
        $commonLengths | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $KeyLengthCommands = 'Get-PAAccount','New-PAAccount','New-PAOrder','Set-PAAccount'
    Register-ArgumentCompleter -CommandName $KeyLengthCommands -ParameterName 'KeyLength' -ScriptBlock $KeyLengthCompleter
    Register-ArgumentCompleter -CommandName 'New-PACertificate' -ParameterName 'AccountKeyLength' -ScriptBlock $KeyLengthCompleter
    Register-ArgumentCompleter -CommandName 'New-PACertificate' -ParameterName 'CertKeyLength' -ScriptBlock $KeyLengthCompleter

    # MainDomain
    $MainDomainCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # nothing to auto complete if we don't have an account selected
        if ([String]::IsNullOrWhiteSpace($script:Acct.Folder)) { return }

        # grab the list of MainDomains in this account
        $jsonPaths = Join-Path $script:Acct.Folder '*\order.json'
        $names = Get-ChildItem $jsonPaths | Get-Content -Raw | ConvertFrom-Json | Select-Object -ExpandProperty MainDomain

        if ($wordToComplete -ne [String]::Empty) {
            $wordToComplete = "^$([regex]::Escape($wordToComplete))"
        }
        $names | Where-Object { $_ -match $wordToComplete } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $MainDomainCommands = 'Get-PAOrder','Set-PAOrder','Remove-PAOrder','Get-PACertificate','Revoke-PACertificate','Get-PAPluginArgs','Invoke-HttpChallengeListener','Submit-Renewal'
    Register-ArgumentCompleter -CommandName $MainDomainCommands -ParameterName 'MainDomain' -ScriptBlock $MainDomainCompleter

    # Order Name
    $OrderNameCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # nothing to auto complete if we don't have an account selected
        if ([String]::IsNullOrWhiteSpace($script:Acct.Folder)) { return }

        $names = (Get-ChildItem -Path $script:Acct.Folder -Directory).BaseName
        if ($wordToComplete -ne [String]::Empty) {
            $wordToComplete = "^$([regex]::Escape($wordToComplete))"
        }
        $names | Where-Object { $_ -match $wordToComplete } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $OrderNameCommands = 'Get-PAOrder','New-PAOrder','Set-PAOrder','Remove-PAOrder','Get-PACertificate','New-PACertificate','Revoke-PACertificate','Get-PAPluginArgs','Invoke-HttpChallengeListener','Submit-Renewal'
    Register-ArgumentCompleter -CommandName $OrderNameCommands -ParameterName 'Name' -ScriptBlock $OrderNameCompleter

    # DirectoryUrl
    $DirUrlCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # combine the existing servers with the available shortcuts to cycle through
        $dirJsonPaths = Join-Path (Get-ConfigRoot) '*\dir.json'
        $choices = $(
            Get-ChildItem $dirJsonPaths | Get-Content -Raw | ConvertFrom-Json | Select-Object -Expand location
            $script:WellKnownDirs.Keys
        )

        $choices | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $DirUrlCommands = 'Set-PAServer','New-PACertificate'
    Register-ArgumentCompleter -CommandName $DirUrlCommands -ParameterName 'DirectoryUrl' -ScriptBlock $DirUrlCompleter

    $DirUrlCompleterExisting = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # combine only the existing servers to cycle through
        $dirJsonPaths = Join-Path (Get-ConfigRoot) '*\dir.json'
        $choices = $(
            Get-ChildItem $dirJsonPaths | Get-Content -Raw | ConvertFrom-Json | Select-Object -Expand location
        )

        $choices | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $DirUrlExistingCommands = 'Get-PAServer','Remove-PAServer'
    Register-ArgumentCompleter -CommandName $DirUrlExistingCommands -ParameterName 'DirectoryUrl' -ScriptBlock $DirUrlCompleterExisting

    # PAServer Name
    $DirNameCompleter = {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        # grab the existing server folders to sort through
        $dirJsonPaths = Join-Path (Get-ConfigRoot) '*\dir.json'
        $choices = Get-ChildItem $dirJsonPaths | ForEach-Object { $_.Directory.BaseName }
        $choices | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }

    $DirNameCommands = 'Get-PAServer','Set-PAServer','Remove-PAServer'
    Register-ArgumentCompleter -CommandName $DirNameCommands -ParameterName 'Name' -ScriptBlock $DirNameCompleter

}