From e1beebbeefaf85107e93ada7b04434bc56deaa3e Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 2 Nov 2016 17:43:32 -0700 Subject: [PATCH] always add but hide experimental cmds and flags Signed-off-by: Victor Vieux update cobra and use Tags Signed-off-by: Victor Vieux allow client to talk to an older server Signed-off-by: Victor Vieux --- docker.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/docker.go b/docker.go index 56c5a89895..18cd0e833c 100644 --- a/docker.go +++ b/docker.go @@ -5,6 +5,7 @@ import ( "os" "github.com/Sirupsen/logrus" + "github.com/docker/docker/api/types/versions" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command/commands" @@ -47,16 +48,15 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { cli.SetupRootCommand(cmd) cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) { - var err error if dockerCli.Client() == nil { // flags must be the top-level command flags, not cmd.Flags() opts.Common.SetDefaultOptions(flags) dockerPreRun(opts) - err = dockerCli.Initialize(opts) - } - if err != nil || !dockerCli.HasExperimental() { - hideExperimentalFeatures(ccmd) + dockerCli.Initialize(opts) } + + hideUnsupportedFeatures(ccmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental()) + if err := ccmd.Help(); err != nil { ccmd.Println(err) } @@ -123,18 +123,29 @@ func dockerPreRun(opts *cliflags.ClientOptions) { } } -func hideExperimentalFeatures(cmd *cobra.Command) { - // hide flags +func hideUnsupportedFeatures(cmd *cobra.Command, clientVersion string, hasExperimental bool) { cmd.Flags().VisitAll(func(f *pflag.Flag) { + // hide experimental flags if _, ok := f.Annotations["experimental"]; ok { f.Hidden = true } + + // hide flags not supported by the server + if flagVersion, ok := f.Annotations["version"]; ok && len(flagVersion) == 1 && versions.LessThan(clientVersion, flagVersion[0]) { + f.Hidden = true + } + }) for _, subcmd := range cmd.Commands() { - // hide subcommands + // hide experimental subcommands if _, ok := subcmd.Tags["experimental"]; ok { subcmd.Hidden = true } + + // hide subcommands not supported by the server + if subcmdVersion, ok := subcmd.Tags["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) { + subcmd.Hidden = true + } } }