completion.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "os"
  16. "github.com/spf13/cobra"
  17. )
  18. func completionCommand() *cobra.Command {
  19. return &cobra.Command{
  20. Use: "completion [bash|zsh|fish|powershell]",
  21. Short: "Generate completion script",
  22. Long: `To load completions:
  23. Bash:
  24. $ source <(docker compose completion bash)
  25. # To load completions for each session, execute once:
  26. # Linux:
  27. $ docker compose completion bash > /etc/bash_completion.d/docker_compose
  28. # macOS:
  29. $ docker compose completion bash > /usr/local/etc/bash_completion.d/docker_compose
  30. Zsh:
  31. # If shell completion is not already enabled in your environment,
  32. # you will need to enable it. You can execute the following once:
  33. $ echo "autoload -U compinit; compinit" >> ~/.zshrc
  34. # To load completions for each session, execute once:
  35. $ docker compose completion zsh > "${fpath[1]}/_docker_compose"
  36. # You will need to start a new shell for this setup to take effect.
  37. fish:
  38. $ docker compose completion fish | source
  39. # To load completions for each session, execute once:
  40. $ docker compose completion fish > ~/.config/fish/completions/docker_compose.fish
  41. PowerShell:
  42. PS> docker compose completion powershell | Out-String | Invoke-Expression
  43. # To load completions for every new session, run:
  44. PS> docker compose completion powershell > docker_compose.ps1
  45. # and source this file from your PowerShell profile.
  46. `,
  47. DisableFlagsInUseLine: true,
  48. ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
  49. Args: cobra.ExactValidArgs(1),
  50. RunE: func(cmd *cobra.Command, args []string) error {
  51. var err error
  52. switch args[0] {
  53. case "bash":
  54. err = cmd.Root().GenBashCompletion(os.Stdout)
  55. case "zsh":
  56. err = cmd.Root().GenZshCompletion(os.Stdout)
  57. case "fish":
  58. err = cmd.Root().GenFishCompletion(os.Stdout, true)
  59. case "powershell":
  60. err = cmd.Root().GenPowerShellCompletion(os.Stdout)
  61. }
  62. return err
  63. },
  64. }
  65. }