Skip to content

Commit

Permalink
refactor(completion): use the default completion command provided by …
Browse files Browse the repository at this point in the history
…cobra

This removes custom bash completion logic and custom completion escape logic as that is not supperted by completion scripts cobra generates.
  • Loading branch information
kangasta committed Jul 5, 2022
1 parent 9a25562 commit 8b53aaa
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 174 deletions.
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
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

0 comments on commit 8b53aaa

Please sign in to comment.