KubePrompt.psm1

#Region '.\Private\KubeInfo.ps1' 0
Set-StrictMode -Version 2

function Test-KubeBinary {
    try {
        [bool](Get-Command -Name 'kubectl')
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}

function Get-KubeContext {
    try {
        kubectl config current-context
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}

function Get-KubeNamespace {
    try {
        $namespace = kubectl config view --minify --output 'jsonpath={..namespace}'

        if (-not $namespace) {
            $namespace = "default"
        }

        $namespace
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
#EndRegion '.\Private\KubeInfo.ps1' 34
#Region '.\Private\Prompt.ps1' 0

function Write-ToHost {
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        $Text,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        $ForegroundColor
    )

    try {
        $params = @{
            Object = $Text
            NoNewLine = $true
        }

        if ($PSBoundParameters.ContainsKey('ForegroundColor')) {
            $params.ForegroundColor = $ForegroundColor
        }

        Write-Host @params
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
#EndRegion '.\Private\Prompt.ps1' 27
#Region '.\Private\PromptOverride.ps1' 0
# Original idea copied from https://github.com/dahlbyk/posh-git

function Get-DefaultPrompt {
    # Get the default prompt definition.
    $initialSessionState = [Runspace]::DefaultRunspace.InitialSessionState

    if (!$initialSessionState.Commands -or !$initialSessionState.Commands['prompt']) {
        "`$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + `$(Get-Location) + `$(if (`$nestedpromptlevel -ge 1) { '>>' }) + '> '"
    }
    else {
        $initialSessionState.Commands['prompt'].Definition
    }
}

function Install-PromptOverrideIfDefault {
    $defaultPromptDef = Get-DefaultPrompt
    $currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition }

    if (-not $currentPromptDef) {
        # HACK: If prompt is missing, create a global one we can overwrite with Set-Item
        function global:prompt { ' ' }
    }

    # If there is no prompt function or the prompt function is the default, replace the current prompt function definition
    if (-not $currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) {
        # Set the posh-git prompt as the default prompt
        Set-Item Function:\prompt -Value {
            Write-KubePrompt
            return "> "
        }
    }
}
#EndRegion '.\Private\PromptOverride.ps1' 32
#Region '.\Public\Write-KubePrompt.ps1' 0
function Write-KubePrompt {
    <#
    .SYNOPSIS
    Write-KubePrompt cmdlet will add the current context and namespace in Kubernetes
    configured in `kubectl` to your PowerShell prompt.
 
    .DESCRIPTION
    In your `$Profile` file, import KubePrompt module and add to your prompt function a call to `Write-KubePrompt`
 
    ```powershell
    Import-Module -Name KubePrompt
 
    function prompt {
        Write-KubePrompt
        ... Rest of your prompt
    }
    ```
 
    Write-KubePrompt cmldet supports two ways for customizations. Either by specifying a parameter
    in the `Write-KubePrompt` cmdlet invocation or through modification of the `KubePromptSettings`
    global variable.
 
    .PARAMETER ContextColor
    Sets the foreground color for the context string. Value can be either of [ConsoleColor] type or string
 
    .PARAMETER NamespaceColor
    Sets the foreground color for the namespace string. Value can be either of [ConsoleColor] type or string
 
    .PARAMETER Symbol
    Sets the string/unicode character to be used as kubernetes symbol
 
    .PARAMETER SymbolColor
    Sets the foreground color for the symbol
 
    .PARAMETER DisableSymbol
    If specified, removes the symbol from the generated prompt
 
    .EXAMPLE
    Write-KubePrompt -DisableSymbol
 
    Hides the k8s symbol from the prompt
 
    .EXAMPLE
    $global:KubePromptSettings.Symbol.Enabled = $false
    PS > Write-KubePrompt
 
    Hides the k8s symbol from the prompt by setting the global KubePromptSettings variable
 
    .EXAMPLE
    Write-KubePrompt -Symbol '*'
 
    Overrides the symbol value with a string
 
    .EXAMPLE
    $global:KubePromptSettings.Symbol.Value = '*'
    PS > Write-KubePrompt
 
    Overrides the symbol value through the global KubePromptSettings variable
 
    .EXAMPLE
    Write-KubePrompt -Symbol 0x1011 # infinity symbol
 
    Overrides the symbol value by passing a Unicode character
 
    .EXAMPLE
    Write-KubePrompt -ContextColor ([ConsoleColor]::Green)
 
    Overrides the context string foreground color using a `ConsoleColor` type
 
    .EXAMPLE
    $global:KubePromptSettings.ContextColor = 'Green'
    PS > Write-KubePrompt
 
    Overrides the context string foreground color using a string
 
    .EXAMPLE
    Write-KubePrompt -NamespaceColor ([ConsoleColor]::Yellow)
 
    Overrides the namespace string foreground color using a `ConsoleColor` type
 
    .EXAMPLE
    $global:KubePromptSettings.NamespaceColor = 'Yellow'
    PS > Write-KubePrompt
 
    Overrides the namespace string foreground color using a string
    #>


    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'EnableSymbol')]
        [ValidateNotNullOrEmpty()]
        $ContextColor,

        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'EnableSymbol')]
        [ValidateNotNullOrEmpty()]
        $NamespaceColor,

        [Parameter(ParameterSetName = 'EnableSymbol')]
        [ValidateNotNullOrEmpty()]
        $Symbol,

        [Parameter(ParameterSetName = 'EnableSymbol')]
        [ValidateNotNullOrEmpty()]
        $SymbolColor,

        [Parameter(ParameterSetName = 'DisableSymbol')]
        [switch]
        $DisableSymbol
    )

    try {
        $settings = $KubePromptSettings

        $ctxColor = $settings.ContextColor
        $nsColor = $settings.NamespaceColor
        $symColor = $settings.SymbolColor
        $sym = $settings.Symbol.Value
        $noSymbol = -not $settings.Symbol.Enabled

        if ($ContextColor) {
            $ctxColor = $ContextColor
        }

        if ($NamespaceColor) {
            $nsColor = $NamespaceColor
        }

        if ($SymbolColor) {
            $symColor = $SymbolColor
        }

        if ($Symbol) {
            $sym = $Symbol
        }

        if ($DisableSymbol) {
            $noSymbol = $DisableSymbol
        }

        Write-ToHost -Text $settings.Prefix

        if (-not $noSymbol) {
            Write-ToHost -Text ("$([char]$sym) ") -ForegroundColor $symColor
            Write-ToHost -Text $settings.Separator
        }

        Write-ToHost -Text (Get-KubeContext) -ForegroundColor $ctxColor

        Write-ToHost -Text $settings.Divider

        Write-ToHost -Text (Get-KubeNamespace) -ForegroundColor $nsColor

        Write-ToHost -Text $settings.Suffix
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}
#EndRegion '.\Public\Write-KubePrompt.ps1' 160
#Region '.\Init\Init.ps1' 0
$global:KubePromptSettings = @{
    SymbolColor = ([ConsoleColor]::DarkBlue)
    ContextColor = ([ConsoleColor]::DarkRed)
    NamespaceColor = ([ConsoleColor]::DarkCyan)
    Prefix = '['
    Suffix = ']'
    Symbol = @{
        Enabled = $true
        Value = 0x2388
    }
    Divider = ':'
    Separator = '|'
}

Install-PromptOverrideIfDefault
#EndRegion '.\Init\Init.ps1' 15