(cmd *cobra.Command, details versionDetails)
| 594 | } |
| 595 | |
| 596 | func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) { |
| 597 | var ( |
| 598 | notExperimental = func(_ string) bool { return !details.ServerInfo().HasExperimental } |
| 599 | notOSType = func(v string) bool { return details.ServerInfo().OSType != "" && v != details.ServerInfo().OSType } |
| 600 | notSwarmStatus = func(v string) bool { |
| 601 | s := details.ServerInfo().SwarmStatus |
| 602 | if s == nil { |
| 603 | // engine did not return swarm status header |
| 604 | return false |
| 605 | } |
| 606 | switch v { |
| 607 | case "manager": |
| 608 | // requires the node to be a manager |
| 609 | return !s.ControlAvailable |
| 610 | case "active": |
| 611 | // requires swarm to be active on the node (e.g. for swarm leave) |
| 612 | // only hide the command if we're sure the node is "inactive" |
| 613 | // for any other status, assume the "leave" command can still |
| 614 | // be used. |
| 615 | return s.NodeState == "inactive" |
| 616 | case "": |
| 617 | // some swarm commands, such as "swarm init" and "swarm join" |
| 618 | // are swarm-related, but do not require swarm to be active |
| 619 | return false |
| 620 | default: |
| 621 | // ignore any other value for the "swarm" annotation |
| 622 | return false |
| 623 | } |
| 624 | } |
| 625 | versionOlderThan = func(v string) bool { return versions.LessThan(details.CurrentVersion(), v) } |
| 626 | ) |
| 627 | |
| 628 | cmd.Flags().VisitAll(func(f *pflag.Flag) { |
| 629 | // hide flags not supported by the server |
| 630 | // root command shows all top-level flags |
| 631 | if cmd.Parent() != nil { |
| 632 | if cmds, ok := f.Annotations["top-level"]; ok { |
| 633 | f.Hidden = !findCommand(cmd, cmds) |
| 634 | } |
| 635 | if f.Hidden { |
| 636 | return |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | hideFlagIf(f, notExperimental, "experimental") |
| 641 | hideFlagIf(f, notOSType, "ostype") |
| 642 | hideFlagIf(f, notSwarmStatus, "swarm") |
| 643 | hideFlagIf(f, versionOlderThan, "version") |
| 644 | }) |
| 645 | |
| 646 | for _, subcmd := range cmd.Commands() { |
| 647 | hideSubcommandIf(subcmd, notExperimental, "experimental") |
| 648 | hideSubcommandIf(subcmd, notOSType, "ostype") |
| 649 | hideSubcommandIf(subcmd, notSwarmStatus, "swarm") |
| 650 | hideSubcommandIf(subcmd, versionOlderThan, "version") |
| 651 | } |
| 652 | } |
| 653 |
no test coverage detected
searching dependent graphs…