Skip to content

Conversation

@kindermax
Copy link
Collaborator

@kindermax kindermax commented Jan 9, 2026

  • Use slices to find max command name len
  • Add changelog entry for slices for max cmd name

Summary by Sourcery

Refine command help formatting by correctly calculating the longest command name and document the fix in the changelog.

Bug Fixes:

  • Correct maxCommandNameLen to return the length of the longest command name when using slices.MaxFunc.

Documentation:

  • Add changelog entry describing the fix to maxCommandNameLen and its use of slices.MaxFunc.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 9, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the computation of the maximum command-name length to use Go's slices.MaxFunc utility and documents the fix in the changelog.

Flow diagram for updated maxCommandNameLen logic

flowchart TD
    A[Start maxCommandNameLen] --> B[Get commands list from cmd.Commands]
    B --> C[Call slices.MaxFunc on commands]
    C --> D["Comparator: cmp.Compare(len(a.Name), len(b.Name))"]
    D --> E[slices.MaxFunc returns command with longest name]
    E --> F[Compute len of maxCmd.Name]
    F --> G[Return length]
    G --> H[End]
Loading

File-Level Changes

Change Details Files
Refactor maxCommandNameLen to use slices.MaxFunc for determining the longest command name.
  • Replace manual for-loop accumulation of maximum command name length with slices.MaxFunc over the slice of commands.
  • Use cmp.Compare on the lengths of command names to select the command with the longest name.
  • Return the length of the selected command’s name instead of the command itself to match the function’s int return type.
cmd/root.go
Document the fix to maxCommandNameLen in the changelog.
  • Add an Unreleased changelog entry describing the corrected use of slices.MaxFunc in maxCommandNameLen.
docs/docs/changelog.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new maxCommandNameLen implementation will panic if cmd.Commands() returns an empty slice, whereas the previous version safely returned 0; consider handling the empty-slice case before calling slices.MaxFunc.
  • To reduce indirection and avoid relying on cmp.Compare for simple length comparison, you could replace the comparator with direct integer comparison (e.g., return len(a.Name()) - len(b.Name())) for slightly clearer code.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new maxCommandNameLen implementation will panic if cmd.Commands() returns an empty slice, whereas the previous version safely returned 0; consider handling the empty-slice case before calling slices.MaxFunc.
- To reduce indirection and avoid relying on cmp.Compare for simple length comparison, you could replace the comparator with direct integer comparison (e.g., return len(a.Name()) - len(b.Name())) for slightly clearer code.

## Individual Comments

### Comment 1
<location> `cmd/root.go:66-69` </location>
<code_context>
-	}
-
-	return maxLen
+	maxCmd := slices.MaxFunc(cmd.Commands(), func(a, b *cobra.Command) int {
+		return cmp.Compare(len(a.Name()), len(b.Name()))
+	})
+	return len(maxCmd.Name())
 }

</code_context>

<issue_to_address>
**issue (bug_risk):** Handle the case where `cmd.Commands()` is empty to avoid `slices.MaxFunc` panicking.

Previously this code returned 0 when there were no subcommands; now `slices.MaxFunc` will panic on an empty slice. If `cmd.Commands()` can ever be empty (e.g., for leaf commands), this introduces a runtime crash instead of the prior safe default. Please guard for `len(cmd.Commands()) == 0` and return 0, or otherwise ensure empty slices are handled safely before calling `slices.MaxFunc`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@kindermax kindermax force-pushed the slices-for-max-cmd-name branch 2 times, most recently from be29731 to 98556bf Compare January 9, 2026 18:10
@kindermax kindermax force-pushed the slices-for-max-cmd-name branch from 98556bf to 25ad348 Compare January 9, 2026 18:11
@kindermax kindermax merged commit ebb4cde into master Jan 9, 2026
5 checks passed
@kindermax kindermax deleted the slices-for-max-cmd-name branch January 9, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants