From 4b65a3fbcf862da1ec671529701c8a233c6d5b11 Mon Sep 17 00:00:00 2001 From: umbynos Date: Mon, 10 Aug 2020 12:12:54 +0200 Subject: [PATCH 1/5] 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 --- go.mod | 4 ++-- go.sum | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index a07ab1e2cfd..a325cf6ec01 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/schollz/closestmatch v2.1.0+incompatible github.com/segmentio/stats/v4 v4.5.3 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/viper v1.6.2 github.com/stretchr/testify v1.6.1 @@ -55,5 +55,5 @@ require ( google.golang.org/grpc v1.27.0 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // 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 ) diff --git a/go.sum b/go.sum index 30933947a33..6f89d9aa42f 100644 --- a/go.sum +++ b/go.sum @@ -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/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.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/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 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= 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/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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From c4b26f1d41b1b29c17af7d6c6ddcd0b9d9ce0111 Mon Sep 17 00:00:00 2001 From: umbynos Date: Mon, 10 Aug 2020 12:13:36 +0200 Subject: [PATCH 2/5] add --no-descriptions also for zsh --- cli/completion/completion.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cli/completion/completion.go b/cli/completion/completion.go index c5412baadbc..9194c001570 100644 --- a/cli/completion/completion.go +++ b/cli/completion/completion.go @@ -29,7 +29,7 @@ var ( 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 { command := &cobra.Command{ Use: "completion [bash|zsh|fish] [--no-descriptions]", @@ -47,7 +47,7 @@ func NewCommand() *cobra.Command { } 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]) os.Exit(errorcodes.ErrGeneric) } @@ -56,7 +56,11 @@ func run(cmd *cobra.Command, args []string) { cmd.Root().GenBashCompletion(os.Stdout) break case "zsh": - cmd.Root().GenZshCompletion(os.Stdout) + if completionNoDesc { + cmd.Root().GenZshCompletionNoDesc(os.Stdout) + } else { + cmd.Root().GenZshCompletion(os.Stdout) + } break case "fish": buf := new(bytes.Buffer) From 288f8011aa5e4cdbb4300b7ece7fd09716be6386 Mon Sep 17 00:00:00 2001 From: umbynos Date: Mon, 10 Aug 2020 12:19:44 +0200 Subject: [PATCH 3/5] update docs --- docs/command-line-completion.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/command-line-completion.md b/docs/command-line-completion.md index d7e8a2a8b1e..24dc30e55aa 100644 --- a/docs/command-line-completion.md +++ b/docs/command-line-completion.md @@ -46,11 +46,11 @@ Remember to open a new shell to test the functionality. #### Disabling command and flag descriptions -By default fish completion has command and flag description enabled by default. If you want to disable this behaviour -you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have -descriptions +By default fish and zsh completion have command and flag description enabled by default. If you want to disable this +behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will +not have descriptions -_N.B._ This flag is not compatible with bash or zsh +_N.B._ This flag is not compatible with bash ### Brew From 6a3554b5140801ef72d8a701c2f3cc6db7b4b1b9 Mon Sep 17 00:00:00 2001 From: umbynos Date: Mon, 10 Aug 2020 12:30:42 +0200 Subject: [PATCH 4/5] update tests --- test/test_completion.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/test_completion.py b/test/test_completion.py index 120110ec7c4..3d7b02eafef 100644 --- a/test/test_completion.py +++ b/test/test_completion.py @@ -34,7 +34,7 @@ def test_completion_zsh(run_command): assert result.ok assert result.stderr == "" 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): @@ -42,7 +42,7 @@ def test_completion_fish(run_command): assert result.ok assert result.stderr == "" 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): @@ -54,9 +54,11 @@ def test_completion_bash_no_desc(run_command): def test_completion_zsh_no_desc(run_command): result = run_command("completion zsh --no-descriptions") - assert not result.ok - assert result.stdout == "" - assert "Error: command description is not supported by zsh" in result.stderr + assert result.ok + assert 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): @@ -64,5 +66,5 @@ def test_completion_fish_no_desc(run_command): assert result.ok assert result.stderr == "" 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 From 98d6387076b92f79b2a4d80b18eafe13ea4c57a4 Mon Sep 17 00:00:00 2001 From: umbynos Date: Mon, 10 Aug 2020 13:03:24 +0200 Subject: [PATCH 5/5] removed hack https://github.com/spf13/cobra/issues/1121 --- cli/completion/completion.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cli/completion/completion.go b/cli/completion/completion.go index 9194c001570..0514c74e592 100644 --- a/cli/completion/completion.go +++ b/cli/completion/completion.go @@ -16,9 +16,7 @@ package completion import ( - "bytes" "os" - "strings" "github.com/arduino/arduino-cli/cli/errorcodes" "github.com/arduino/arduino-cli/cli/feedback" @@ -63,11 +61,7 @@ func run(cmd *cobra.Command, args []string) { } break case "fish": - buf := new(bytes.Buffer) - 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) + cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc) break } }