Unverified Commit 82df01f3 authored by Umberto Baldi's avatar Umberto Baldi Committed by GitHub

Fix zsh completion (#892)

* update cobra to get new zsh completion https://github.com/spf13/cobra/commit/2c5a0d300f8ba1cd142776508519aba999b5e77f and to fix this https://github.com/spf13/cobra/commit/675ae5f5a98cc705a6d54f4c487ab81230604137

* add --no-descriptions also for zsh

* update docs

* update tests

* removed hack https://github.com/spf13/cobra/issues/1121
parent bd6c8618
...@@ -16,9 +16,7 @@ ...@@ -16,9 +16,7 @@
package completion package completion
import ( import (
"bytes"
"os" "os"
"strings"
"github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback" "github.com/arduino/arduino-cli/cli/feedback"
...@@ -29,7 +27,7 @@ var ( ...@@ -29,7 +27,7 @@ var (
completionNoDesc bool //Disable completion description for shells that support it completionNoDesc bool //Disable completion description for shells that support it
) )
// NewCommand created a new `version` command // NewCommand created a new `completion` command
func NewCommand() *cobra.Command { func NewCommand() *cobra.Command {
command := &cobra.Command{ command := &cobra.Command{
Use: "completion [bash|zsh|fish] [--no-descriptions]", Use: "completion [bash|zsh|fish] [--no-descriptions]",
...@@ -47,7 +45,7 @@ func NewCommand() *cobra.Command { ...@@ -47,7 +45,7 @@ func NewCommand() *cobra.Command {
} }
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
if completionNoDesc && (args[0] == "bash" || args[0] == "zsh") { if completionNoDesc && (args[0] == "bash") {
feedback.Errorf("Error: command description is not supported by %v", args[0]) feedback.Errorf("Error: command description is not supported by %v", args[0])
os.Exit(errorcodes.ErrGeneric) os.Exit(errorcodes.ErrGeneric)
} }
...@@ -56,14 +54,14 @@ func run(cmd *cobra.Command, args []string) { ...@@ -56,14 +54,14 @@ func run(cmd *cobra.Command, args []string) {
cmd.Root().GenBashCompletion(os.Stdout) cmd.Root().GenBashCompletion(os.Stdout)
break break
case "zsh": case "zsh":
if completionNoDesc {
cmd.Root().GenZshCompletionNoDesc(os.Stdout)
} else {
cmd.Root().GenZshCompletion(os.Stdout) cmd.Root().GenZshCompletion(os.Stdout)
}
break break
case "fish": case "fish":
buf := new(bytes.Buffer) cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc)
cmd.Root().GenFishCompletion(buf, !completionNoDesc)
// Next 2 lines are Hack, fixed here https://github.com/spf13/cobra/pull/1122
s := strings.ReplaceAll(buf.String(), "arduino-cli_comp", "arduino_cli_comp") //required because fish does not support env variables with "-" in the name
os.Stdout.WriteString(s)
break break
} }
} }
...@@ -46,11 +46,11 @@ Remember to open a new shell to test the functionality. ...@@ -46,11 +46,11 @@ Remember to open a new shell to test the functionality.
#### Disabling command and flag descriptions #### Disabling command and flag descriptions
By default fish completion has command and flag description enabled by default. If you want to disable this behaviour By default fish and zsh completion have command and flag description enabled by default. If you want to disable this
you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will
descriptions not have descriptions
_N.B._ This flag is not compatible with bash or zsh _N.B._ This flag is not compatible with bash
### Brew ### Brew
......
...@@ -38,7 +38,7 @@ require ( ...@@ -38,7 +38,7 @@ require (
github.com/schollz/closestmatch v2.1.0+incompatible github.com/schollz/closestmatch v2.1.0+incompatible
github.com/segmentio/stats/v4 v4.5.3 github.com/segmentio/stats/v4 v4.5.3
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/jwalterweatherman v1.0.0
github.com/spf13/viper v1.6.2 github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.6.1
...@@ -55,5 +55,5 @@ require ( ...@@ -55,5 +55,5 @@ require (
google.golang.org/grpc v1.27.0 google.golang.org/grpc v1.27.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.4 gopkg.in/yaml.v2 v2.3.0
) )
...@@ -199,6 +199,10 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= ...@@ -199,6 +199,10 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b h1:grM+VdcoRu+xbzmCXM1KuH5UQGk9Lc8yCiwZZ2PKVdU=
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c h1:/dP/1GnfVIlWnB0YDImenSmneUCw3wjyq2RMgAG1e2o=
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c/go.mod h1:aeNIJzz/GSSVlS+gpCpQWZ83BKbsoW57mr90+YthtkQ=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
...@@ -310,6 +314,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= ...@@ -310,6 +314,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
......
...@@ -34,7 +34,7 @@ def test_completion_zsh(run_command): ...@@ -34,7 +34,7 @@ def test_completion_zsh(run_command):
assert result.ok assert result.ok
assert result.stderr == "" assert result.stderr == ""
assert "#compdef _arduino-cli arduino-cli" in result.stdout assert "#compdef _arduino-cli arduino-cli" in result.stdout
assert "function _arduino-cli" in result.stdout assert "_arduino-cli()" in result.stdout
def test_completion_fish(run_command): def test_completion_fish(run_command):
...@@ -42,7 +42,7 @@ def test_completion_fish(run_command): ...@@ -42,7 +42,7 @@ def test_completion_fish(run_command):
assert result.ok assert result.ok
assert result.stderr == "" assert result.stderr == ""
assert "# fish completion for arduino-cli" in result.stdout assert "# fish completion for arduino-cli" in result.stdout
assert "function __arduino-cli_perform_completion" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout
def test_completion_bash_no_desc(run_command): def test_completion_bash_no_desc(run_command):
...@@ -54,9 +54,11 @@ def test_completion_bash_no_desc(run_command): ...@@ -54,9 +54,11 @@ def test_completion_bash_no_desc(run_command):
def test_completion_zsh_no_desc(run_command): def test_completion_zsh_no_desc(run_command):
result = run_command("completion zsh --no-descriptions") result = run_command("completion zsh --no-descriptions")
assert not result.ok assert result.ok
assert result.stdout == "" assert result.stderr == ""
assert "Error: command description is not supported by zsh" in result.stderr assert "#compdef _arduino-cli arduino-cli" in result.stdout
assert "_arduino-cli()" in result.stdout
assert "__completeNoDesc" in result.stdout
def test_completion_fish_no_desc(run_command): def test_completion_fish_no_desc(run_command):
...@@ -64,5 +66,5 @@ def test_completion_fish_no_desc(run_command): ...@@ -64,5 +66,5 @@ def test_completion_fish_no_desc(run_command):
assert result.ok assert result.ok
assert result.stderr == "" assert result.stderr == ""
assert "# fish completion for arduino-cli" in result.stdout assert "# fish completion for arduino-cli" in result.stdout
assert "function __arduino-cli_perform_completion" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout
assert "__completeNoDesc" in result.stdout assert "__completeNoDesc" in result.stdout
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment