|
| 1 | + |
| 2 | +// Copyright (c) 2024 Parseable, Inc |
| 3 | +// |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package cmd |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | +) |
| 26 | + |
| 27 | +// AutocompleteCmd represents the autocomplete command |
| 28 | +var AutocompleteCmd = &cobra.Command{ |
| 29 | + Use: "autocomplete [bash|zsh|powershell]", |
| 30 | + Short: "Generate autocomplete script", |
| 31 | + Long: `Generate autocomplete script for bash, zsh, or powershell`, |
| 32 | + Args: cobra.ExactArgs(1), |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + var err error |
| 35 | + switch args[0] { |
| 36 | + case "bash": |
| 37 | + err = cmd.Root().GenBashCompletion(os.Stdout) |
| 38 | + case "zsh": |
| 39 | + err = cmd.Root().GenZshCompletion(os.Stdout) |
| 40 | + case "powershell": |
| 41 | + err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) |
| 42 | + default: |
| 43 | + err = fmt.Errorf("unsupported shell type: %s. Only bash, zsh, and powershell are supported", args[0]) |
| 44 | + } |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("error generating autocomplete script: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + return nil |
| 51 | + }, |
| 52 | +} |
0 commit comments