Skip to content

Commit

Permalink
perf(core): lazy load usage annotations allocation (#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Dec 27, 2022
1 parent d999184 commit efb08dd
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {

cobraCmd.SetUsageTemplate(usageTemplate)

if cobraCmd.Annotations == nil {
cobraCmd.Annotations = make(map[string]string)
}

// Use a custom function to print usage
// This function will build usage to avoid building it for each commands
cobraCmd.SetUsageFunc(usageFuncBuilder(cobraCmd, func() {
if cobraCmd.Annotations == nil {
cobraCmd.Annotations = make(map[string]string)
}

if cmd.ArgsType != nil {
cobraCmd.Annotations["UsageArgs"] = buildUsageArgs(b.ctx, cmd, false)
}
Expand All @@ -118,6 +118,8 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
if cmd.SeeAlsos != nil {
cobraCmd.Annotations["SeeAlsos"] = cmd.seeAlsosAsStr()
}

cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands)
}))

if cmd.Run != nil {
Expand All @@ -137,8 +139,6 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
if cmd.WaitFunc != nil {
cobraCmd.PersistentFlags().BoolP("wait", "w", false, "wait until the "+cmd.Resource+" is ready")
}

cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands)
}

const usageTemplate = `USAGE:
Expand Down

0 comments on commit efb08dd

Please sign in to comment.