github.com/reds/docker@v1.11.2-rc1/contrib/completion/powershell/posh-docker.psm1 (about)

     1  # Powershell completion for docker
     2  
     3  ### Prerequisite
     4  # Docker.exe needs to be in your PATH.
     5  # If the command is not found, you will need to add a docker alias or add the docker installation folder (e.g. `%ProgramFiles%\Docker Toolbox`) to your PATH environment variable.
     6  
     7  ### Installation (Latest stable)
     8  # Windows 10 / Windows Server 2016: 
     9  # 1. Open a powershell prompt
    10  # 2. Run `Install-Module -Scope CurrentUser posh-docker`
    11  #
    12  # Earlier Windows versions:
    13  # 1. Install [PackageManagement PowerShell Modules Preview](https://www.microsoft.com/en-us/download/details.aspx?id=49186)
    14  # 2. Open a powershell prompt
    15  # 3. Run `Install-Module -Scope CurrentUser posh-docker`
    16  
    17  ### Installation (From source)
    18  # Copy this file to the %userprofile%\Documents\WindowsPowerShell\Modules\posh-docker directory (create directories as needed)
    19  
    20  ### Usage
    21  # After installation, execute the following line to enable autocompletion for the current powershell session:
    22  #
    23  # Import-Module posh-docker
    24  #
    25  # To make it persistent, add the above line to your profile. For example, run `notepad $PROFILE` and insert the line above.
    26  
    27  $global:DockerCompletion = @{}
    28  
    29  $script:flagRegex = "^  (-[^, =]+),? ?(--[^= ]+)?"
    30  
    31  function script:Get-Containers($filter)
    32  {
    33      if ($filter -eq $null)
    34      {
    35         docker ps -a --no-trunc --format "{{.Names}}"
    36      } else {
    37         docker ps -a --no-trunc --format "{{.Names}}" --filter $filter
    38      }
    39  }
    40  
    41  function script:Get-AutoCompleteResult
    42  {
    43      param([Parameter(ValueFromPipeline=$true)] $value)
    44      
    45      Process
    46      {
    47          New-Object System.Management.Automation.CompletionResult $value
    48      }
    49  }
    50  
    51  filter script:MatchingCommand($commandName)
    52  {
    53      if ($_.StartsWith($commandName))
    54      {
    55          $_
    56      }
    57  }
    58  
    59  $completion_Docker = {
    60      param($commandName, $commandAst, $cursorPosition)
    61  
    62      $command = $null
    63      $commandParameters = @{}
    64      $state = "Unknown"
    65      $wordToComplete = $commandAst.CommandElements | Where-Object { $_.ToString() -eq $commandName } | Foreach-Object { $commandAst.CommandElements.IndexOf($_) }
    66  
    67      for ($i=1; $i -lt $commandAst.CommandElements.Count; $i++)
    68      {
    69          $p = $commandAst.CommandElements[$i].ToString()
    70  
    71          if ($p.StartsWith("-"))
    72          {
    73              if ($state -eq "Unknown" -or $state -eq "Options")
    74              {
    75                  $commandParameters[$i] = "Option"
    76                  $state = "Options"
    77              }
    78              else
    79              {
    80                  $commandParameters[$i] = "CommandOption"
    81                  $state = "CommandOptions"
    82              }
    83          } 
    84          else 
    85          {
    86              if ($state -ne "CommandOptions")
    87              {
    88                  $commandParameters[$i] = "Command"
    89                  $command = $p
    90                  $state = "CommandOptions"
    91              } 
    92              else 
    93              {
    94                  $commandParameters[$i] = "CommandOther"
    95              }
    96          }
    97      }
    98  
    99      if ($global:DockerCompletion.Count -eq 0)
   100      {
   101          $global:DockerCompletion["commands"] = @{}
   102          $global:DockerCompletion["options"] = @()
   103          
   104          docker --help | ForEach-Object {
   105              Write-Output $_
   106              if ($_ -match "^    (\w+)\s+(.+)")
   107              {
   108                  $global:DockerCompletion["commands"][$Matches[1]] = @{}
   109                  
   110                  $currentCommand = $global:DockerCompletion["commands"][$Matches[1]]
   111                  $currentCommand["options"] = @()
   112              }
   113              elseif ($_ -match $flagRegex)
   114              {
   115                  $global:DockerCompletion["options"] += $Matches[1]
   116                  if ($Matches[2] -ne $null)
   117                  {
   118                      $global:DockerCompletion["options"] += $Matches[2]
   119                   }
   120              }
   121          }
   122  
   123      }
   124      
   125      if ($wordToComplete -eq $null)
   126      {
   127          $commandToComplete = "Command"
   128          if ($commandParameters.Count -gt 0)
   129          {
   130              if ($commandParameters[$commandParameters.Count] -eq "Command")
   131              {
   132                  $commandToComplete = "CommandOther"
   133              }
   134          } 
   135      } else {
   136          $commandToComplete = $commandParameters[$wordToComplete]
   137      }
   138  
   139      switch ($commandToComplete)
   140      {
   141          "Command" { $global:DockerCompletion["commands"].Keys | MatchingCommand -Command $commandName | Sort-Object | Get-AutoCompleteResult }
   142          "Option" { $global:DockerCompletion["options"] | MatchingCommand -Command $commandName | Sort-Object | Get-AutoCompleteResult }
   143          "CommandOption" { 
   144              $options = $global:DockerCompletion["commands"][$command]["options"]
   145              if ($options.Count -eq 0)
   146              {
   147                  docker $command --help | % {
   148                  if ($_ -match $flagRegex)
   149                      {
   150                          $options += $Matches[1]
   151                          if ($Matches[2] -ne $null)
   152                          {
   153                              $options += $Matches[2]
   154                          }
   155                      }
   156                  }
   157              }
   158  
   159              $global:DockerCompletion["commands"][$command]["options"] = $options
   160              $options | MatchingCommand -Command $commandName | Sort-Object | Get-AutoCompleteResult
   161          }
   162          "CommandOther" {
   163              $filter = $null 
   164              switch ($command)
   165              {
   166                  "start" { $filter = "status=exited" }
   167                  "stop" { $filter = "status=running" }
   168              }
   169              Get-Containers $filter | MatchingCommand -Command $commandName | Sort-Object | Get-AutoCompleteResult
   170          }
   171          default { $global:DockerCompletion["commands"].Keys | MatchingCommand -Command $commandName }
   172      }
   173  }
   174  
   175  # Register the TabExpension2 function
   176  if (-not $global:options) { $global:options = @{CustomArgumentCompleters = @{};NativeArgumentCompleters = @{}}}
   177  $global:options['NativeArgumentCompleters']['docker'] = $Completion_Docker
   178  
   179  $function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'