specialK.psm1

<#
    Code in this file will be added to the beginning of the .psm1. For example,
    you should place any using statements here.
#>

# https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-pwsh/
# but we are going to switch the completer to 'k' from 'kubectl'
Function Add-specialKAutoCompletion {
    [cmdletbinding()]
    param (
        [switch]$ToProfile
    )
    $toReplace = "-CommandName 'kubectl'"
    $argCompleter = (kubectl completion powershell) -replace $toReplace, "-CommandName 'k'"
    if ($argCompleter -like 'error*') {
        throw $argCompleter
    }

    if ($null -ne $argCompleter) {
        $argCompleter | Out-String | Invoke-Expression
    }

    if ($ToProfile.IsPresent) {
        $argCompleter | Out-File $PROFILE -Append
    }
}
function k {
    if ($objectCommands.Keys -contains $args[0] -and $args[1] -ne '--help' -and $args.Count -eq 2) {
        if ($objectCommands[$args[0]] -contains $args[1]) {
            # select the format type name
            $typeName = "$($args[0])-$($args[1])"

            # get the output
            $out = (& kubectl $args)

            # locate all positions to place commas
            # we are using the headers since some values may be null in the data
            if ($null -ne $out) {
                $m = $out[0] | Select-String -Pattern ' \S' -AllMatches
            }

            # place semicolons
            $out = foreach ($line in $out) {
                foreach ($index in ($m.Matches.Index | Sort-Object -Descending)) {
                    $line = $line.Insert($index + 2, ';')
                }
                $line
            }

            # convert from csv (since we added commas)
            $out -replace ' +;', ';' | ForEach-Object { $_.Trim() } | ConvertFrom-Csv -Delimiter ';' | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $typeName); $_ }
            return
        }
    }
    & kubectl $args
}
<#
    Code in this file will be added to the end of the .psm1. For example,
    you should set variables or other environment settings here.
#>

$script:objectCommands = Get-Content $PSScriptRoot\formats.json | ConvertFrom-Json -AsHashtable