Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor completions to use cobras default completion command #141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added support for all shell completions provided by `cobra`.

### Changed
- Remove custom bash completion logic and replace it with `completion` command provided by `cobra`.

## [1.5.0] - 2022-07-05
### Added
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

replace github.com/spf13/cobra => github.com/kangasta/cobra v1.5.1-0.20220623204649-b02e41b5a36d

require (
github.com/ansel1/merry v1.5.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kangasta/cobra v1.5.1-0.20220623204649-b02e41b5a36d h1:ivoDZoNixM9e9+L8+KA/v7eJL9et6yB1KszdhmhDdSU=
github.com/kangasta/cobra v1.5.1-0.20220623204649-b02e41b5a36d/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -200,8 +202,6 @@ github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
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.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
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/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
9 changes: 0 additions & 9 deletions internal/commands/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(loadbalancer.ShowCommand(), loadbalancerCommand.Cobra(), conf)

// Misc
commands.BuildCommand(
&root.CompletionCommand{
BaseCommand: commands.New(
"completion",
"Generates shell completion",
"upctl completion bash",
),
}, rootCmd, conf,
)
commands.BuildCommand(
&root.VersionCommand{
BaseCommand: commands.New(
Expand Down
79 changes: 0 additions & 79 deletions internal/commands/bash_completion.go

This file was deleted.

31 changes: 0 additions & 31 deletions internal/commands/root/completion.go

This file was deleted.

13 changes: 1 addition & 12 deletions internal/completion/helpers.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
package completion

import (
"fmt"
"strings"
)

// MatchStringPrefix returns a list of string in vals which have a prefix as specified in key. Quotes are removed from key and output strings are escaped according to completion rules
func MatchStringPrefix(vals []string, key string, caseSensitive bool) []string {
var r []string
key = strings.Trim(key, "'\"")

for _, v := range vals {
if (caseSensitive && strings.HasPrefix(v, key)) ||
(!caseSensitive && strings.HasPrefix(strings.ToLower(v), strings.ToLower(key))) ||
key == "" {
r = append(r, Escape(v))
r = append(r, v)
}
}
return r
}

// Escape escapes a string according to completion rules (?)
// in effect, this means that the string will be quoted with double quotes if it contains a space or parentheses.
func Escape(s string) string {
if strings.ContainsAny(s, ` ()`) {
return fmt.Sprintf(`"%s"`, s)
}
return s
}
42 changes: 2 additions & 40 deletions internal/completion/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func TestMatchStringPrefix(t *testing.T) {
expected: []string{"aba", "aBa", "Aba"},
},
{
name: "escaped output",
name: "output with special characters",
vals: []string{"a a ", "a(0)", "aab", "a;<!`'", "bbb"},
key: "a",
caseSensitive: false,
expected: []string{"\"a a \"", "\"a(0)\"", "aab", "a;<!`'"},
expected: []string{"a a ", "a(0)", "aab", "a;<!`'"},
},
} {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -90,41 +90,3 @@ func TestMatchStringPrefix(t *testing.T) {
})
}
}

func TestEscape(t *testing.T) {
for _, test := range []struct {
name string
in string
expected string
}{
{
name: "no escape",
in: "asdasdasd",
expected: "asdasdasd",
},
{
name: "escape spaces",
in: "asdas dasd",
expected: "\"asdas dasd\"",
},
{
name: "escape open parentheses",
in: "asdas(",
expected: "\"asdas(\"",
},
{
name: "escape closed parentheses",
in: "asdas()",
expected: "\"asdas()\"",
},
{
name: "special chars not escaped",
in: "a;<!`'",
expected: "a;<!`'",
},
} {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, completion.Escape(test.in))
})
}
}
3 changes: 0 additions & 3 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"

"github.com/UpCloudLtd/upcloud-cli/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/internal/commands/all"
"github.com/UpCloudLtd/upcloud-cli/internal/config"
"github.com/UpCloudLtd/upcloud-cli/internal/terminal"
Expand Down Expand Up @@ -78,8 +77,6 @@ func BuildRootCmd(conf *config.Config) cobra.Command {
},
}

rootCmd.BashCompletionFunction = commands.CustomBashCompletionFunc(rootCmd.Use)

flags := &pflag.FlagSet{}
flags.StringVarP(
&conf.GlobalFlags.ConfigFile, "config", "", "", "Config file",
Expand Down