|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package completion |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 24 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + completionNoDesc bool //Disable completion description for shells that support it |
| 30 | +) |
| 31 | + |
| 32 | +// NewCommand created a new `version` command |
| 33 | +func NewCommand() *cobra.Command { |
| 34 | + command := &cobra.Command{ |
| 35 | + Use: "completion [bash|zsh|fish] [--no-descriptions]", |
| 36 | + ValidArgs: []string{"bash", "zsh", "fish"}, |
| 37 | + Args: cobra.ExactArgs(1), |
| 38 | + Short: "Generates completion scripts", |
| 39 | + Long: "Generates completion scripts for various shells", |
| 40 | + Example: " " + os.Args[0] + " completion bash > completion.sh\n" + |
| 41 | + " " + "source completion.sh", |
| 42 | + Run: run, |
| 43 | + } |
| 44 | + command.Flags().BoolVar(&completionNoDesc, "no-descriptions", false, "Disable completion description for shells that support it") |
| 45 | + |
| 46 | + return command |
| 47 | +} |
| 48 | + |
| 49 | +func run(cmd *cobra.Command, args []string) { |
| 50 | + if completionNoDesc && (args[0] == "bash" || args[0] == "zsh") { |
| 51 | + feedback.Errorf("Error: command description is not supported by %v", args[0]) |
| 52 | + os.Exit(errorcodes.ErrGeneric) |
| 53 | + } |
| 54 | + switch args[0] { |
| 55 | + case "bash": |
| 56 | + cmd.Root().GenBashCompletion(os.Stdout) |
| 57 | + break |
| 58 | + case "zsh": |
| 59 | + buf := new(bytes.Buffer) |
| 60 | + cmd.Root().GenZshCompletion(buf) |
| 61 | + // Next 3 lines are Hack, we'll wait new version of cobra with ZshV2Completion https://github.com/spf13/cobra/pull/1070 |
| 62 | + //insert escaping before [ and ] |
| 63 | + s := strings.ReplaceAll(buf.String(), "[", "\\[") |
| 64 | + s = strings.ReplaceAll(s, "]", "\\]") |
| 65 | + s = strings.ReplaceAll(s, "\\[1\\]", "[1]") // revert the case |
| 66 | + os.Stdout.WriteString(s) |
| 67 | + break |
| 68 | + case "fish": |
| 69 | + buf := new(bytes.Buffer) |
| 70 | + cmd.Root().GenFishCompletion(buf, !completionNoDesc) |
| 71 | + // Next 2 lines are Hack, fixed here https://github.com/spf13/cobra/pull/1122 |
| 72 | + s := strings.ReplaceAll(buf.String(), "arduino-cli_comp", "arduino_cli_comp") //required because fish does not support env variables with "-" in the name |
| 73 | + os.Stdout.WriteString(s) |
| 74 | + break |
| 75 | + } |
| 76 | +} |
0 commit comments