Skip to content

Commit

Permalink
skeleton to adopt Cobra completion v2
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Feb 17, 2022
1 parent cf8c4ba commit df0647c
Show file tree
Hide file tree
Showing 160 changed files with 6,895 additions and 3,052 deletions.
13 changes: 13 additions & 0 deletions cli/context/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ func (s *store) List() ([]Metadata, error) {
return s.meta.list()
}

// Names return Metadata names for a Lister
func Names(s Lister) ([]string, error) {
list, err := s.List()
if err != nil {
return nil, err
}
var names []string
for _, item := range list {
names = append(names, item.Name)
}
return names, nil
}

func (s *store) CreateOrUpdate(meta Metadata) error {
return s.meta.createOrUpdate(meta)
}
Expand Down
40 changes: 40 additions & 0 deletions cmd/docker/completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"strings"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/store"
"github.com/spf13/cobra"
)

func registerCompletionFuncForGlobalFlags(dockerCli *command.DockerCli, cmd *cobra.Command) {
cmd.RegisterFlagCompletionFunc(
"context",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
names, err := store.Names(dockerCli.ContextStore())
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

return filterForCompletion(names, toComplete), cobra.ShellCompDirectiveDefault
},
)
cmd.RegisterFlagCompletionFunc(
"log-level",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
values := []string{"debug", "info", "warn", "error", "fatal"}
return filterForCompletion(values, toComplete), cobra.ShellCompDirectiveDefault
},
)
}

func filterForCompletion(values []string, toComplete string) []string {
var offer []string
for _, v := range values {
if strings.HasPrefix(v, toComplete) {
offer = append(offer, v)
}
}
return offer
}
5 changes: 5 additions & 0 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
},
Version: fmt.Sprintf("%s, build %s", version.Version, version.GitCommit),
DisableFlagsInUseLine: true,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: false,
HiddenDefaultCmd: true,
},
}
opts, flags, helpCmd = cli.SetupRootCommand(cmd)
registerCompletionFuncForGlobalFlags(dockerCli, cmd)
flags.BoolP("version", "v", false, "Print version information and quit")

setFlagErrorFunc(dockerCli, cmd)
Expand Down
14 changes: 5 additions & 9 deletions vendor.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ require (
github.com/docker/swarmkit v1.12.1-0.20210726173615-3629f50980f6
github.com/fvbommel/sortorder v1.0.2
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5
github.com/google/go-cmp v0.5.6
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/imdario/mergo v0.3.12
github.com/json-iterator/go v1.1.10 // indirect
github.com/mitchellh/mapstructure v1.3.2
github.com/mitchellh/mapstructure v1.4.3
github.com/moby/buildkit v0.8.2-0.20210615162540-9f254e18360a
github.com/moby/sys/signal v0.6.0
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
Expand All @@ -35,17 +33,15 @@ require (
github.com/opencontainers/image-spec v1.0.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/cobra v1.3.0
github.com/spf13/pflag v1.0.5
github.com/theupdateframework/notary v0.7.1-0.20210315103452-bf96a202a09a
github.com/tonistiigi/go-rosetta v0.0.0-20200727161949-f79598599c5d
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
golang.org/x/text v0.3.4
google.golang.org/grpc v1.38.0 // indirect
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
gotest.tools/v3 v3.0.3
k8s.io/api v0.20.6
Expand Down
220 changes: 157 additions & 63 deletions vendor.sum

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions vendor/github.com/cespare/xxhash/v2/.travis.yml

This file was deleted.

6 changes: 4 additions & 2 deletions vendor/github.com/cespare/xxhash/v2/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/github.com/cespare/xxhash/v2/xxhash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 31 additions & 31 deletions vendor/github.com/cespare/xxhash/v2/xxhash_amd64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 32 additions & 21 deletions vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df0647c

Please sign in to comment.