Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $kubectlAutoCompleteScriptBlock = {
  2.   param(
  3.     $WordToComplete,
  4.     $CommandAst,
  5.     $CursorPosition
  6.   )
  7.  
  8.   # Get the current command line and convert into a string
  9.   $Command = $CommandAst.CommandElements
  10.   $Command = "$Command"
  11.  
  12.   __kubectl_debug ""
  13.   __kubectl_debug "========= starting completion logic =========="
  14.   __kubectl_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
  15.  
  16.   # The user could have moved the cursor backwards on the command-line.
  17.   # We need to trigger completion from the $CursorPosition location, so we need
  18.   # to truncate the command-line ($Command) up to the $CursorPosition location.
  19.   # Make sure the $Command is longer then the $CursorPosition before we truncate.
  20.   # This happens because the $Command does not include the last space.
  21.   if ($Command.Length -gt $CursorPosition) {
  22.     $Command = $Command.Substring(0, $CursorPosition)
  23.   }
  24.   __kubectl_debug "Truncated command: $Command"
  25.  
  26.   $ShellCompDirectiveError = 1
  27.   $ShellCompDirectiveNoSpace = 2
  28.   $ShellCompDirectiveNoFileComp = 4
  29.   $ShellCompDirectiveFilterFileExt = 8
  30.   $ShellCompDirectiveFilterDirs = 16
  31.  
  32.   # Prepare the command to request completions for the program.
  33.   # Split the command at the first space to separate the program and arguments.
  34.   $Program, $Arguments = $Command.Split(" ", 2)
  35.   # $Program = 'kubectl.exe'
  36.   # if(((Get-Alias kubectl).Definition) -eq 'kubecolor'){
  37.   #   $Program = $Program + ' --plain'
  38.   # }
  39.   $RequestComp = "$Program __complete $Arguments"
  40.   __kubectl_debug "RequestComp: $RequestComp"
  41.  
  42.   # we cannot use $WordToComplete because it
  43.   # has the wrong values if the cursor was moved
  44.   # so use the last argument
  45.   if ($WordToComplete -ne "" ) {
  46.     $WordToComplete = $Arguments.Split(" ")[-1]
  47.   }
  48.   __kubectl_debug "Program: $Program"
  49.   __kubectl_debug "New WordToComplete: $WordToComplete"
  50.  
  51.  
  52.   # Check for flag with equal sign
  53.   $IsEqualFlag = ($WordToComplete -Like "--*=*" )
  54.   if ( $IsEqualFlag ) {
  55.     __kubectl_debug "Completing equal sign flag"
  56.     # Remove the flag part
  57.     $Flag, $WordToComplete = $WordToComplete.Split("=", 2)
  58.   }
  59.  
  60.   if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
  61.     # If the last parameter is complete (there is a space following it)
  62.     # We add an extra empty parameter so we can indicate this to the go method.
  63.     __kubectl_debug "Adding extra empty parameter"
  64.     # We need to use `"`" to pass an empty argument a "" or '' does not work!!!
  65.     $RequestComp = "$RequestComp" + ' ""'
  66.   }
  67.  
  68.   __kubectl_debug "Calling $RequestComp"
  69.   # First disable ActiveHelp which is not supported for Powershell
  70.   $env:KUBECTL_ACTIVE_HELP = 0
  71.   #call the command store the output in $out and redirect stderr and stdout to null
  72.   # $Out is an array contains each line per element
  73.   Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
  74.  
  75.  
  76.   # get directive from last line
  77.   [int]$Directive = $Out[-1].TrimStart(':')
  78.   if ($Directive -eq "") {
  79.     # There is no directive specified
  80.     $Directive = 0
  81.   }
  82.   __kubectl_debug "The completion directive is: $Directive"
  83.  
  84.   # remove directive (last element) from out
  85.   $Out = $Out | Where-Object { $_ -ne $Out[-1] }
  86.   __kubectl_debug "The completions are: $Out"
  87.  
  88.   if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
  89.     # Error code.  No completion.
  90.     __kubectl_debug "Received error from custom completion go code"
  91.     return
  92.   }
  93.  
  94.   $Longest = 0
  95.   $Values = $Out | ForEach-Object {
  96.     #Split the output in name and description
  97.     $Name, $Description = $_.Split("`t", 2)
  98.     __kubectl_debug "Name: $Name Description: $Description"
  99.  
  100.     # Look for the longest completion so that we can format things nicely
  101.     if ($Longest -lt $Name.Length) {
  102.       $Longest = $Name.Length
  103.     }
  104.  
  105.     # Set the description to a one space string if there is none set.
  106.     # This is needed because the CompletionResult does not accept an empty string as argument
  107.     if (-Not $Description) {
  108.       $Description = " "
  109.     }
  110.     @{Name = "$Name"; Description = "$Description" }
  111.   }
  112.  
  113.  
  114.   $Space = " "
  115.   if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
  116.     # remove the space here
  117.     __kubectl_debug "ShellCompDirectiveNoSpace is called"
  118.     $Space = ""
  119.   }
  120.  
  121.   if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
  122.      (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
  123.     __kubectl_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
  124.  
  125.     # return here to prevent the completion of the extensions
  126.     return
  127.   }
  128.  
  129.   $Values = $Values | Where-Object {
  130.     # filter the result
  131.     $_.Name -like "$WordToComplete*"
  132.  
  133.     # Join the flag back if we have an equal sign flag
  134.     if ( $IsEqualFlag ) {
  135.       __kubectl_debug "Join the equal sign flag back to the completion value"
  136.       $_.Name = $Flag + "=" + $_.Name
  137.     }
  138.   }
  139.  
  140.   if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
  141.     __kubectl_debug "ShellCompDirectiveNoFileComp is called"
  142.  
  143.     if ($Values.Length -eq 0) {
  144.       # Just print an empty string here so the
  145.       # shell does not start to complete paths.
  146.       # We cannot use CompletionResult here because
  147.       # it does not accept an empty string as argument.
  148.       ""
  149.       return
  150.     }
  151.   }
  152.  
  153.   # Get the current mode
  154.   $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
  155.   __kubectl_debug "Mode: $Mode"
  156.  
  157.   $Values | ForEach-Object {
  158.  
  159.     # store temporary because switch will overwrite $_
  160.     $comp = $_
  161.  
  162.     # PowerShell supports three different completion modes
  163.     # - TabCompleteNext (default windows style - on each key press the next option is displayed)
  164.     # - Complete (works like bash)
  165.     # - MenuComplete (works like zsh)
  166.     # You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode>
  167.  
  168.     # CompletionResult Arguments:
  169.     # 1) CompletionText text to be used as the auto completion result
  170.     # 2) ListItemText   text to be displayed in the suggestion list
  171.     # 3) ResultType     type of completion result
  172.     # 4) ToolTip        text for the tooltip with details about the object
  173.  
  174.     switch ($Mode) {
  175.  
  176.       # bash like
  177.       "Complete" {
  178.  
  179.         if ($Values.Length -eq 1) {
  180.           __kubectl_debug "Only one completion left"
  181.  
  182.           # insert space after value
  183.           [System.Management.Automation.CompletionResult]::new($($comp.Name | __kubectl_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
  184.  
  185.         }
  186.         else {
  187.           # Add the proper number of spaces to align the descriptions
  188.           while ($comp.Name.Length -lt $Longest) {
  189.             $comp.Name = $comp.Name + " "
  190.           }
  191.  
  192.           # Check for empty description and only add parentheses if needed
  193.           if ($($comp.Description) -eq " " ) {
  194.             $Description = ""
  195.           }
  196.           else {
  197.             $Description = "  ($($comp.Description))"
  198.           }
  199.  
  200.           [System.Management.Automation.CompletionResult]::new("$($comp.Name)$Description", "$($comp.Name)$Description", 'ParameterValue', "$($comp.Description)")
  201.         }
  202.       }
  203.  
  204.       # zsh like
  205.       "MenuComplete" {
  206.         # insert space after value
  207.         # MenuComplete will automatically show the ToolTip of
  208.         # the highlighted value at the bottom of the suggestions.
  209.         [System.Management.Automation.CompletionResult]::new($($comp.Name | __kubectl_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
  210.       }
  211.  
  212.       # TabCompleteNext and in case we get something unknown
  213.       Default {
  214.         # Like MenuComplete but we don't want to add a space here because
  215.         # the user need to press space anyway to get the completion.
  216.         # Description will not be shown because thats not possible with TabCompleteNext
  217.         [System.Management.Automation.CompletionResult]::new($($comp.Name | __kubectl_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
  218.       }
  219.     }
  220.  
  221.   }
  222. }
  223.  
  224. Register-ArgumentCompleter -CommandName 'cilium', 'hubble', 'k', 'kubectl', 'kubecolor', 't', 'talosctl', 'helm', 'argocd' -ScriptBlock $kubectlAutoCompleteScriptBlock
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement