Skip to content

Commit ae9d123

Browse files
committed
feat(cli): support cobra GroupID and set error prefix
Commander layout now reads both cobra's native GroupID and the legacy Annotations["group"] for command grouping. Projects can use either approach while keeping salt's custom help formatting. cli.Init sets SetErrPrefix(name + ":") for consistent error messages across all raystack CLIs (e.g. "Error: frontier: unknown command").
1 parent 71c0949 commit ae9d123

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

cli/cli.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func Init(rootCmd *cobra.Command, opts ...Option) {
4343
opt(cfg)
4444
}
4545

46+
// Set error prefix for consistent error messages.
47+
rootCmd.SetErrPrefix(rootCmd.Name() + ":")
48+
4649
// Inject shared output and prompter into command context.
4750
existing := rootCmd.PersistentPreRun
4851
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {

cli/commander/layout.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,22 @@ func buildHelpEntries(cmd *cobra.Command) []helpEntry {
9595
}
9696

9797
entry := fmt.Sprintf("%s%s", rpad(c.Name(), c.NamePadding()+3), c.Short)
98-
if group, ok := c.Annotations["group"]; ok {
99-
switch group {
100-
case "core":
101-
coreCommands = append(coreCommands, entry)
102-
case "help":
103-
helpCommands = append(helpCommands, entry)
104-
default:
105-
groupCommands[group] = append(groupCommands[group], entry)
106-
}
107-
} else {
98+
99+
// Support both cobra's native GroupID and legacy Annotations["group"].
100+
group := c.GroupID
101+
if group == "" {
102+
group = c.Annotations["group"]
103+
}
104+
105+
switch group {
106+
case "core":
107+
coreCommands = append(coreCommands, entry)
108+
case "help":
109+
helpCommands = append(helpCommands, entry)
110+
case "":
108111
otherCommands = append(otherCommands, entry)
112+
default:
113+
groupCommands[group] = append(groupCommands[group], entry)
109114
}
110115
}
111116

0 commit comments

Comments
 (0)