gencompletion.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "os"
  17. "github.com/spf13/cobra"
  18. )
  19. var genCompletionCmd = &cobra.Command{
  20. Use: "completion [bash|zsh|fish|powershell]",
  21. Short: "Generate the autocompletion script for the specified shell",
  22. Long: `Generate the autocompletion script for sftpgo for the specified shell.
  23. See each sub-command's help for details on how to use the generated script.
  24. `,
  25. }
  26. var genCompletionBashCmd = &cobra.Command{
  27. Use: "bash",
  28. Short: "Generate the autocompletion script for bash",
  29. Long: `Generate the autocompletion script for the bash shell.
  30. This script depends on the 'bash-completion' package.
  31. If it is not installed already, you can install it via your OS's package
  32. manager.
  33. To load completions in your current shell session:
  34. $ source <(sftpgo gen completion bash)
  35. To load completions for every new session, execute once:
  36. Linux:
  37. $ sudo sftpgo gen completion bash > /usr/share/bash-completion/completions/sftpgo
  38. MacOS:
  39. $ sudo sftpgo gen completion bash > /usr/local/etc/bash_completion.d/sftpgo
  40. You will need to start a new shell for this setup to take effect.
  41. `,
  42. DisableFlagsInUseLine: true,
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. return cmd.Root().GenBashCompletionV2(os.Stdout, true)
  45. },
  46. }
  47. var genCompletionZshCmd = &cobra.Command{
  48. Use: "zsh",
  49. Short: "Generate the autocompletion script for zsh",
  50. Long: `Generate the autocompletion script for the zsh shell.
  51. If shell completion is not already enabled in your environment you will need
  52. to enable it. You can execute the following once:
  53. $ echo "autoload -U compinit; compinit" >> ~/.zshrc
  54. To load completions for every new session, execute once:
  55. Linux:
  56. $ sftpgo gen completion zsh > > "${fpath[1]}/_sftpgo"
  57. macOS:
  58. $ sudo sftpgo gen completion zsh > /usr/local/share/zsh/site-functions/_sftpgo
  59. You will need to start a new shell for this setup to take effect.
  60. `,
  61. DisableFlagsInUseLine: true,
  62. RunE: func(cmd *cobra.Command, args []string) error {
  63. return cmd.Root().GenZshCompletion(os.Stdout)
  64. },
  65. }
  66. var genCompletionFishCmd = &cobra.Command{
  67. Use: "fish",
  68. Short: "Generate the autocompletion script for fish",
  69. Long: `Generate the autocompletion script for the fish shell.
  70. To load completions in your current shell session:
  71. $ sftpgo gen completion fish | source
  72. To load completions for every new session, execute once:
  73. $ sftpgo gen completion fish > ~/.config/fish/completions/sftpgo.fish
  74. You will need to start a new shell for this setup to take effect.
  75. `,
  76. DisableFlagsInUseLine: true,
  77. RunE: func(cmd *cobra.Command, args []string) error {
  78. return cmd.Root().GenFishCompletion(os.Stdout, true)
  79. },
  80. }
  81. var genCompletionPowerShellCmd = &cobra.Command{
  82. Use: "powershell",
  83. Short: "Generate the autocompletion script for powershell",
  84. Long: `Generate the autocompletion script for powershell.
  85. To load completions in your current shell session:
  86. PS C:\> sftpgo gen completion powershell | Out-String | Invoke-Expression
  87. To load completions for every new session, add the output of the above command
  88. to your powershell profile.
  89. `,
  90. DisableFlagsInUseLine: true,
  91. RunE: func(cmd *cobra.Command, args []string) error {
  92. return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
  93. },
  94. }
  95. func init() {
  96. genCompletionCmd.AddCommand(genCompletionBashCmd)
  97. genCompletionCmd.AddCommand(genCompletionZshCmd)
  98. genCompletionCmd.AddCommand(genCompletionFishCmd)
  99. genCompletionCmd.AddCommand(genCompletionPowerShellCmd)
  100. genCmd.AddCommand(genCompletionCmd)
  101. }