hide swarm-related commands based on the current swarm status and role

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-03-29 11:04:50 +02:00
parent 374d0f88cd
commit db141c21e9
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
15 changed files with 67 additions and 9 deletions

View File

@ -16,7 +16,7 @@ func NewConfigCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.30",
"swarm": "",
"swarm": "manager",
},
}
cmd.AddCommand(

View File

@ -20,7 +20,7 @@ func NewNodeCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.24",
"swarm": "",
"swarm": "manager",
},
}
cmd.AddCommand(

View File

@ -16,7 +16,7 @@ func NewSecretCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.25",
"swarm": "",
"swarm": "manager",
},
}
cmd.AddCommand(

View File

@ -16,7 +16,7 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.24",
"swarm": "",
"swarm": "manager",
},
}
cmd.AddCommand(

View File

@ -17,7 +17,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.25",
"swarm": "",
"swarm": "manager",
},
}
defaultHelpFunc := cmd.HelpFunc()

View File

@ -35,7 +35,10 @@ func newCACommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runCA(dockerCli, cmd.Flags(), opts)
},
Annotations: map[string]string{"version": "1.30"},
Annotations: map[string]string{
"version": "1.30",
"swarm": "manager",
},
}
flags := cmd.Flags()

View File

@ -16,7 +16,7 @@ func NewSwarmCommand(dockerCli command.Cli) *cobra.Command {
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{
"version": "1.24",
"swarm": "",
"swarm": "", // swarm command itself does not require swarm to be enabled (so swarm init and join is always available on API 1.24 and up)
},
}
cmd.AddCommand(

View File

@ -39,6 +39,10 @@ func newInitCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runInit(dockerCli, cmd.Flags(), opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "", // swarm init does not require swarm to be active, and is always available on API 1.24 and up
},
}
flags := cmd.Flags()

View File

@ -36,6 +36,10 @@ func newJoinCommand(dockerCli command.Cli) *cobra.Command {
opts.remote = args[0]
return runJoin(dockerCli, cmd.Flags(), opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "", // swarm join does not require swarm to be active, and is always available on API 1.24 and up
},
}
flags := cmd.Flags()

View File

@ -28,6 +28,10 @@ func newJoinTokenCommand(dockerCli command.Cli) *cobra.Command {
opts.role = args[0]
return runJoinToken(dockerCli, opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "manager",
},
}
flags := cmd.Flags()

View File

@ -23,6 +23,10 @@ func newLeaveCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runLeave(dockerCli, opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "active",
},
}
flags := cmd.Flags()

View File

@ -24,6 +24,10 @@ func newUnlockCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runUnlock(dockerCli)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "manager",
},
}
return cmd

View File

@ -27,6 +27,10 @@ func newUnlockKeyCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runUnlockKey(dockerCli, opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "manager",
},
}
flags := cmd.Flags()

View File

@ -28,6 +28,10 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
}
return nil
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "manager",
},
}
cmd.Flags().BoolVar(&opts.autolock, flagAutolock, false, "Change manager autolocking setting (true|false)")

View File

@ -309,8 +309,33 @@ func hideSubcommandIf(subcmd *cobra.Command, condition func(string) bool, annota
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error {
var (
notExperimental = func(_ string) bool { return !details.ServerInfo().HasExperimental }
notOSType = func(v string) bool { return v != details.ServerInfo().OSType }
notExperimental = func(_ string) bool { return !details.ServerInfo().HasExperimental }
notOSType = func(v string) bool { return v != details.ServerInfo().OSType }
notSwarmStatus = func(v string) bool {
s := details.ServerInfo().SwarmStatus
if s == nil {
// engine did not return swarm status header
return false
}
switch v {
case "manager":
// requires the node to be a manager
return !s.ControlAvailable
case "active":
// requires swarm to be active on the node (e.g. for swarm leave)
// only hide the command if we're sure the node is "inactive"
// for any other status, assume the "leave" command can still
// be used.
return s.NodeState == "inactive"
case "":
// some swarm commands, such as "swarm init" and "swarm join"
// are swarm-related, but do not require swarm to be active
return false
default:
// ignore any other value for the "swarm" annotation
return false
}
}
versionOlderThan = func(v string) bool { return versions.LessThan(details.Client().ClientVersion(), v) }
)
@ -328,12 +353,14 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error {
hideFlagIf(f, notExperimental, "experimental")
hideFlagIf(f, notOSType, "ostype")
hideFlagIf(f, notSwarmStatus, "swarm")
hideFlagIf(f, versionOlderThan, "version")
})
for _, subcmd := range cmd.Commands() {
hideSubcommandIf(subcmd, notExperimental, "experimental")
hideSubcommandIf(subcmd, notOSType, "ostype")
hideSubcommandIf(subcmd, notSwarmStatus, "swarm")
hideSubcommandIf(subcmd, versionOlderThan, "version")
}
return nil