Skip to content

update version generation#346

Merged
kindermax merged 2 commits into
masterfrom
update-version-generation
Jun 6, 2026
Merged

update version generation#346
kindermax merged 2 commits into
masterfrom
update-version-generation

Conversation

@kindermax

@kindermax kindermax commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator
  • Fix help and version handling
  • Add changelog entry for help/version fix

Summary by Sourcery

Align help and version handling with Cobra defaults while simplifying version formatting and configuration checks.

Bug Fixes:

  • Ensure the root command and self subcommand delegate to Cobra's help handling when invoked without arguments.
  • Allow the --version flag to bypass configuration requirements so version can be queried without a config file.

Enhancements:

  • Set the Cobra root command version string at construction time, including optional build date, and simplify version printing to reuse this value.

Documentation:

  • Document the updated help and version behavior in the changelog.

Tests:

  • Add tests covering root and self help delegation, root command version formatting, and the relaxed config requirement for --version.

@sourcery-ai

sourcery-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Reviewer's Guide

Update root and self command help/version handling to delegate through Cobra’s built-in mechanisms, centralize version string construction, and relax config requirements for --version, with tests and changelog updated accordingly.

Sequence diagram for updated help handling via Cobra

sequenceDiagram
    actor User
    participant Main
    participant RootCommand
    participant Cobra

    User->>Main: Main(version, buildDate)
    Main->>RootCommand: newRootCmd(version, buildDate)
    RootCommand->>RootCommand: SetHelpFunc(customHelpFunc)
    Main->>Cobra: rootCmd.Execute()

    alt root command help
        User->>Cobra: lets
        Cobra->>RootCommand: RunE
        RootCommand->>RootCommand: cmd.Help()
        RootCommand->>RootCommand: customHelpFunc(rootCmd)
        RootCommand->>RootCommand: PrintRootHelpMessage(rootCmd)
    else subcommand help
        User->>Cobra: lets subcommand
        Cobra->>RootCommand: RunE
        RootCommand->>RootCommand: cmd.Help()
        RootCommand->>RootCommand: customHelpFunc(subcommand)
        RootCommand->>RootCommand: PrintHelpMessage(subcommand)
    end
Loading

File-Level Changes

Change Details Files
Refactor root command to delegate help via Cobra, embed build metadata in Version, and centralize version string formatting.
  • Change root command RunE to call cmd.Help() instead of custom PrintHelpMessage for top-level execution without args.
  • Set a custom help function that routes root help to PrintRootHelpMessage and subcommand help to PrintHelpMessage, printing any help errors to command output.
  • Embed build date into root command via Version field using a new buildVersion helper and store buildDate in annotations.
  • Add buildVersion helper that formats version as 'lets version ' with optional '(buildDate)' suffix.
internal/cmd/root.go
Update version printing and CLI flow to rely on cobra command Version and to allow --version even when config is missing.
  • Simplify PrintVersionMessage to just print cmd.Version rather than recomputing message from annotations.
  • Remove manual --version and help/usage handling paths from Main, so Cobra’s help/version mechanisms drive behavior.
  • Extend failOnConfigError to exempt --version from config errors and add a test to cover this behavior.
internal/cmd/root.go
internal/cli/cli.go
internal/cli/cli_test.go
Adjust tests to reflect new version string semantics and help delegation behavior for root and self commands.
  • Add tests ensuring root and self commands invoke the configured help func when run without arguments.
  • Update version-related tests to assert on root.Version contents instead of capturing printed output.
  • Ensure self command’s RunE uses cmd.Help() rather than directly calling PrintHelpMessage.
internal/cmd/root_test.go
internal/cmd/self.go
Document the behavior change in the changelog.
  • Add an Unreleased changelog entry describing the new help delegation through Cobra and the relaxed config requirement for --version.
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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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 buildDate annotation on the root command is now unused after inlining it into Version; consider removing the Annotations: map[string]string{"buildDate": buildDate} and any remaining references to that annotation to avoid stale state.
  • PrintVersionMessage no longer appears to be used now that --version is handled via Cobra and cmd.Version; consider deleting it or reusing it via SetVersionTemplate to reduce unused API surface.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `buildDate` annotation on the root command is now unused after inlining it into `Version`; consider removing the `Annotations: map[string]string{"buildDate": buildDate}` and any remaining references to that annotation to avoid stale state.
- `PrintVersionMessage` no longer appears to be used now that `--version` is handled via Cobra and `cmd.Version`; consider deleting it or reusing it via `SetVersionTemplate` to reduce unused API surface.

## Individual Comments

### Comment 1
<location path="internal/cmd/root.go" line_range="92-95" />
<code_context>
 		SilenceUsage: true,
 	}
+
+	cmd.SetHelpFunc(func(c *cobra.Command, _ []string) {
+		var err error
+		if c == c.Root() {
+			err = PrintRootHelpMessage(c)
+		} else {
+			err = PrintHelpMessage(c)
+		}
+
+		if err != nil {
+			c.Println(err)
+		}
+	})
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Avoid printing help errors to stdout; prefer stderr to keep output channels separated.

In the custom help func, errors from `PrintRootHelpMessage` / `PrintHelpMessage` are printed via `c.Println(err)`, so they go to stdout and mix with normal help output, which can break tooling that parses stdout. Since `SetHelpFunc` can’t return errors, consider using `c.PrintErrln(err)` so error output goes to stderr instead.

```suggestion
		if err != nil {
			c.PrintErrln(err)
		}
	})
```
</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.

Comment thread internal/cmd/root.go
Comment on lines +92 to +95
if err != nil {
c.Println(err)
}
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (bug_risk): Avoid printing help errors to stdout; prefer stderr to keep output channels separated.

In the custom help func, errors from PrintRootHelpMessage / PrintHelpMessage are printed via c.Println(err), so they go to stdout and mix with normal help output, which can break tooling that parses stdout. Since SetHelpFunc can’t return errors, consider using c.PrintErrln(err) so error output goes to stderr instead.

Suggested change
if err != nil {
c.Println(err)
}
})
if err != nil {
c.PrintErrln(err)
}
})

@kindermax kindermax force-pushed the update-version-generation branch 2 times, most recently from d8d494d to 2c970e4 Compare June 6, 2026 19:21
@kindermax kindermax force-pushed the update-version-generation branch from 2c970e4 to e7462b6 Compare June 6, 2026 19:23
@kindermax kindermax merged commit f1eb91c into master Jun 6, 2026
5 checks passed
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.

1 participant