Skip to content

Commit efb08dd

Browse files
authored
perf(core): lazy load usage annotations allocation (#2701)
1 parent d999184 commit efb08dd

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

internal/core/cobra_builder.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
9696

9797
cobraCmd.SetUsageTemplate(usageTemplate)
9898

99-
if cobraCmd.Annotations == nil {
100-
cobraCmd.Annotations = make(map[string]string)
101-
}
102-
10399
// Use a custom function to print usage
104100
// This function will build usage to avoid building it for each commands
105101
cobraCmd.SetUsageFunc(usageFuncBuilder(cobraCmd, func() {
102+
if cobraCmd.Annotations == nil {
103+
cobraCmd.Annotations = make(map[string]string)
104+
}
105+
106106
if cmd.ArgsType != nil {
107107
cobraCmd.Annotations["UsageArgs"] = buildUsageArgs(b.ctx, cmd, false)
108108
}
@@ -118,6 +118,8 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
118118
if cmd.SeeAlsos != nil {
119119
cobraCmd.Annotations["SeeAlsos"] = cmd.seeAlsosAsStr()
120120
}
121+
122+
cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands)
121123
}))
122124

123125
if cmd.Run != nil {
@@ -137,8 +139,6 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
137139
if cmd.WaitFunc != nil {
138140
cobraCmd.PersistentFlags().BoolP("wait", "w", false, "wait until the "+cmd.Resource+" is ready")
139141
}
140-
141-
cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands)
142142
}
143143

144144
const usageTemplate = `USAGE:

0 commit comments

Comments
 (0)