posh-docker.psm1 5.8 KB

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