code cleanup: Abstract repetitive codes in cmd as a function

This commit is contained in:
BinacsLee
2021-04-22 22:34:50 +08:00
parent 972ee2d425
commit 75dde4dce4
12 changed files with 586 additions and 32 deletions

View File

@@ -144,17 +144,9 @@ controller, and serviceaccounts controller.`,
for _, f := range namedFlagSets.FlagSets {
fs.AddFlagSet(f)
}
usageFmt := "Usage:\n %s\n"
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStderr(), namedFlagSets, cols)
return nil
})
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStdout(), namedFlagSets, cols)
})
cliflag.SetUsageAndHelpFunc(cmd, namedFlagSets, cols)
return cmd
}