always add but hide experimental cmds and flags

Signed-off-by: Victor Vieux <vieux@docker.com>

update cobra and use Tags

Signed-off-by: Victor Vieux <vieux@docker.com>

allow client to talk to an older server

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2016-11-02 17:43:32 -07:00
parent 46418414a2
commit e1beebbeef
1 changed files with 19 additions and 8 deletions

View File

@ -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
}
}
}