Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 2a4e612

Browse files
Anca Iordachendeloof
authored andcommitted
Disable experimental cmds and flags when not in experimental mode
Signed-off-by: Anca Iordache <[email protected]>
1 parent d7d8312 commit 2a4e612

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

internal/commands/root.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,31 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
6363
)
6464

6565
if !dockerCli.ClientInfo().HasExperimental {
66-
hideExperimentalCLI(cmd)
66+
removeExperimentalCmdsAndFlags(cmd)
6767
}
6868
}
6969

70-
func hideExperimentalCLI(cmd *cobra.Command) {
71-
if _, ok := cmd.Annotations["experimentalCLI"]; ok {
72-
cmd.Hidden = true
73-
}
70+
func removeExperimentalCmdsAndFlags(cmd *cobra.Command) {
71+
enabledFlags := []*pflag.Flag{}
7472
cmd.Flags().VisitAll(func(f *pflag.Flag) {
75-
if _, ok := f.Annotations["experimentalCLI"]; ok {
76-
f.Hidden = true
73+
if _, disabled := f.Annotations["experimentalCLI"]; !disabled {
74+
enabledFlags = append(enabledFlags, f)
7775
}
7876
})
77+
78+
if len(enabledFlags) != cmd.Flags().NFlag() {
79+
cmd.ResetFlags()
80+
for _, f := range enabledFlags {
81+
cmd.Flags().AddFlag(f)
82+
}
83+
}
84+
7985
for _, subcmd := range cmd.Commands() {
80-
hideExperimentalCLI(subcmd)
86+
if _, ok := subcmd.Annotations["experimentalCLI"]; ok {
87+
cmd.RemoveCommand(subcmd)
88+
} else {
89+
removeExperimentalCmdsAndFlags(subcmd)
90+
}
8191
}
8292
}
8393

0 commit comments

Comments
 (0)