Skip to content

Clean up Cobra Example blocks#41

Merged
austin-denoble merged 4 commits intomainfrom
adenoble/clean-up-examples
Oct 1, 2025
Merged

Clean up Cobra Example blocks#41
austin-denoble merged 4 commits intomainfrom
adenoble/clean-up-examples

Conversation

@austin-denoble
Copy link
Collaborator

@austin-denoble austin-denoble commented Sep 30, 2025

Problem

We're working on improving the overall documentation experience around the CLI. One area that could use some attention are our Example blocks in our cobra.Command configurations. We're inconsistently using utilities in different commands, some commands need examples, etc. These are important because they're part of the self-documenting nature of the CLI, and are used when calling --help against a command, or generating man pages.

Solution

  • Improve the help.Examples helper function, move heredoc inside of it so we get easier expression at callsite.
  • Make sure our example commands are consistent in syntax and style. Update commands that need examples.
  • Add new unit tests for help.Examples to validate behavior.

After these updates, the examples that are printed with the --help flag should be consistently styled across all commands.

Before After
Screenshot 2025-09-30 at 1 28 53 PM Screenshot 2025-09-30 at 1 29 11 PM

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

Pull this branch down, build the binary with goreleaser, and run commands with --help to validate the output.

…so we get easier expression at callsite, make sure our example commands are consistently styled
@claude
Copy link

claude bot commented Sep 30, 2025

Claude finished @austin-denoble's task —— View job


Code Review Complete ✅

I've conducted a thorough review of the PR that cleans up Cobra Example blocks across 46 files. Here's my comprehensive feedback:

Review Checklist:

  • Read repository guidelines (CLAUDE.md)
  • Analyze core helper function changes in examples.go
  • Review command file changes for consistency
  • Check for potential bugs or issues
  • Assess performance considerations
  • Evaluate security concerns
  • Check test coverage implications
  • Provide comprehensive feedback

Overall Assessment: 🟢 Excellent Refactoring

This is a well-executed cleanup that significantly improves the CLI's documentation experience and maintainability.

Code Quality & Best Practices ✅

Strengths:

  • Excellent abstraction: The new help.Examples() function centralizes example formatting logic, reducing code duplication across 46 files
  • Clean API design: Changed from Examples([]string) to Examples(string) with heredoc support makes callsite code more readable
  • Consistent formatting: All command examples now follow a uniform pattern with proper indentation and styling
  • Good separation of concerns: Helper handles all formatting complexity while commands focus on content

Minor suggestions:

  • Consider adding a godoc example to internal/pkg/utils/help/examples.go:16 showing typical usage
  • The function signature could benefit from a more descriptive parameter name: func Examples(exampleBlock string) vs func Examples(examples string)

Implementation Analysis 🔍

Key improvements in examples.go:

  • Heredoc integration: Uses heredoc.Doc() to handle indentation normalization automatically
  • Smart line processing: Differentiates between comment lines (styled with Faint) and command lines (styled with CodeWithPrompt)
  • Whitespace handling: Properly trims trailing whitespace while preserving interior blank lines
  • Robust edge cases: Handles empty blocks gracefully

Command file updates:

  • Consistent replacement of heredoc.Doc() with help.Examples() across all commands
  • Removal of manual $ prompt prefixes (now handled by style.CodeWithPrompt)
  • Improved visual hierarchy with comments vs commands

Potential Issues 🟡

Very minor concerns:

  1. Memory allocation: out := make([]string, 0, len(lines)) pre-allocates optimally, but could use len(lines) instead of 0 capacity for slightly better performance in typical cases
  2. Error handling: Function doesn't validate malformed input, but this is acceptable for internal tooling
  3. Consistency: Mixed indentation in some examples (tabs vs spaces) but the helper normalizes this anyway

Performance Considerations ⚡

Positive impacts:

  • Reduces code duplication across 46 files
  • Centralized logic means easier maintenance
  • String processing is efficient for typical CLI example sizes

No performance concerns: The string operations are lightweight and appropriate for CLI help text generation.

Security Concerns 🔒

No security issues identified:

  • No user input validation needed (internal tooling)
  • No external dependencies introduced beyond existing heredoc library
  • No credentials or sensitive data handling

Test Coverage 📊

Observations:

  • No existing tests for the help package utilities
  • The refactor maintains existing behavior, so regression risk is low
  • Consider adding tests for the new Examples() function to cover:
    • Empty input handling
    • Mixed content (comments + commands)
    • Whitespace edge cases
    • Heredoc normalization

Suggestion:

func TestExamples(t *testing.T) {
    // Test cases for comment/command differentiation,
    // empty blocks, whitespace handling, etc.
}

Specific File Feedback 📁

internal/pkg/utils/help/examples.go - Excellent implementation

  • Clean, readable, well-documented
  • Handles edge cases appropriately
  • Good use of existing style utilities

Command files - Consistent improvements

  • internal/pkg/cli/command/auth/clear.go:22-31 - Good example of clean conversion
  • internal/pkg/cli/command/index/create.go:82-91 - Complex examples handled well
  • All files show consistent application of the new pattern

Final Recommendation ✨

Approve and merge - This is a high-quality refactoring that:

  • Improves code maintainability
  • Enhances user experience with consistent help formatting
  • Reduces technical debt
  • Follows Go best practices
  • Maintains backward compatibility

The only enhancement I'd suggest is adding unit tests for the new helper function, but this shouldn't block merging since the behavior is well-defined and the risk is low.


@claude
Copy link

claude bot commented Sep 30, 2025

Claude finished @austin-denoble's task —— View job


Code Review Complete ✅

I've conducted a thorough review of the PR that cleans up Cobra Example blocks across 46 files. Here's my comprehensive feedback:

Review Checklist:

  • Read repository guidelines (CLAUDE.md) - No repo-specific guidelines found
  • Analyze core helper function changes in examples.go
  • Review command file changes for consistency
  • Check for potential bugs or issues
  • Assess performance considerations
  • Evaluate security concerns
  • Check test coverage implications
  • Provide comprehensive feedback

Overall Assessment: 🟢 Excellent Refactoring

This is a well-executed cleanup that significantly improves the CLI's documentation experience and maintainability.

Code Quality & Best Practices ✅

Strengths:

  • Excellent abstraction: The new help.Examples() function centralizes example formatting logic, reducing code duplication across 46 files
  • Clean API design: Changed from Examples([]string) to Examples(string) with heredoc support makes callsite code more readable
  • Consistent formatting: All command examples now follow a uniform pattern with proper indentation and styling
  • Good separation of concerns: Helper handles all formatting complexity while commands focus on content

Minor suggestions:

  • Consider adding a godoc example to internal/pkg/utils/help/examples.go:16 showing typical usage
  • The function signature could benefit from a more descriptive parameter name: func Examples(exampleBlock string) vs func Examples(examples string)

Implementation Analysis 🔍

Key improvements in examples.go:

  • Heredoc integration: Uses heredoc.Doc() to handle indentation normalization automatically
  • Smart line processing: Differentiates between comment lines (styled with Faint) and command lines (styled with CodeWithPrompt)
  • Whitespace handling: Properly trims trailing whitespace while preserving interior blank lines
  • Robust edge cases: Handles empty blocks gracefully

Command file updates:

  • Consistent replacement of heredoc.Doc() with help.Examples() across all commands
  • Removal of manual $ prompt prefixes (now handled by style.CodeWithPrompt)
  • Improved visual hierarchy with comments vs commands

Potential Issues 🟡

Very minor concerns:

  1. Memory allocation: out := make([]string, 0, len(lines)) pre-allocates optimally, but could use len(lines) instead of 0 capacity for slightly better performance in typical cases
  2. Error handling: Function doesn't validate malformed input, but this is acceptable for internal tooling
  3. Consistency: Mixed indentation in some examples (tabs vs spaces) but the helper normalizes this anyway

Performance Considerations ⚡

Positive impacts:

  • Reduces code duplication across 46 files
  • Centralized logic means easier maintenance
  • String processing is efficient for typical CLI example sizes

No performance concerns: The string operations are lightweight and appropriate for CLI help text generation.

Security Concerns 🔒

No security issues identified:

  • No user input validation needed (internal tooling)
  • No external dependencies introduced beyond existing heredoc library
  • No credentials or sensitive data handling

Test Coverage 📊

Observations:

  • Excellent new test suite in examples_test.go with 118 lines
  • Tests cover empty input, mixed content, whitespace handling, and color modes
  • Good edge case coverage including ANSI stripping and color toggling
  • Tests validate both the text output and ANSI color formatting

Test quality is high with comprehensive coverage of the new functionality.

Specific File Feedback 📁

internal/pkg/utils/help/examples.go - Excellent implementation

  • Clean, readable, well-documented
  • Handles edge cases appropriately
  • Good use of existing style utilities

Command files - Consistent improvements

  • internal/pkg/cli/command/auth/clear.go:22-31 - Good example of clean conversion
  • internal/pkg/cli/command/index/create.go:82-91 - Complex examples handled well
  • All files show consistent application of the new pattern

Final Recommendation ✨

Approve and merge - This is a high-quality refactoring that:

  • Improves code maintainability
  • Enhances user experience with consistent help formatting
  • Reduces technical debt
  • Follows Go best practices
  • Maintains backward compatibility

The comprehensive test suite addresses my earlier concerns about test coverage, making this a complete and well-tested improvement.


@claude
Copy link

claude bot commented Sep 30, 2025

Claude finished @austin-denoble's task —— View job


Code Review Complete ✅

I've conducted a thorough review of the PR that cleans up Cobra Example blocks across 47 files. Here's my comprehensive feedback:

Review Checklist:

  • Read repository guidelines (CLAUDE.md) - No repo-specific guidelines found
  • Analyze core helper function changes in examples.go
  • Review command file changes for consistency
  • Check for potential bugs or issues
  • Assess performance considerations
  • Evaluate security concerns
  • Check test coverage implications
  • Provide comprehensive feedback

Overall Assessment: 🟢 Excellent Refactoring

This is a well-executed cleanup that significantly improves the CLI's documentation experience and maintainability.

Code Quality & Best Practices ✅

Strengths:

  • Excellent abstraction: The new help.Examples() function centralizes example formatting logic, reducing code duplication across 47 files
  • Clean API design: Changed from Examples([]string) to Examples(string) with heredoc support makes callsite code more readable
  • Consistent formatting: All command examples now follow a uniform pattern with proper indentation and styling
  • Good separation of concerns: Helper handles all formatting complexity while commands focus on content

Minor suggestions:

  • Consider adding a godoc example to internal/pkg/utils/help/examples.go:16 showing typical usage
  • The function signature could benefit from a more descriptive parameter name: func Examples(exampleBlock string) vs func Examples(examples string)

Implementation Analysis 🔍

Key improvements in examples.go:

  • Heredoc integration: Uses heredoc.Doc() to handle indentation normalization automatically at examples.go:17
  • Smart line processing: Differentiates between comment lines (styled with Faint) and command lines (styled with CodeWithPrompt) at examples.go:35-42
  • Whitespace handling: Properly trims trailing whitespace while preserving interior blank lines at examples.go:26-30
  • Robust edge cases: Handles empty blocks gracefully at examples.go:18-20

Command file updates:

  • Consistent replacement of heredoc.Doc() with help.Examples() across all commands
  • Removal of manual $ prompt prefixes (now handled by style.CodeWithPrompt)
  • Improved visual hierarchy with comments vs commands

Potential Issues 🟡

Very minor concerns:

  1. Memory allocation: out := make([]string, 0, len(lines)) at examples.go:23 pre-allocates optimally, but could use len(lines) instead of 0 capacity for slightly better performance in typical cases
  2. Error handling: Function doesn't validate malformed input, but this is acceptable for internal tooling
  3. Consistency: Mixed indentation in some examples (tabs vs spaces) but the helper normalizes this anyway

Performance Considerations ⚡

Positive impacts:

  • Reduces code duplication across 47 files
  • Centralized logic means easier maintenance
  • String processing is efficient for typical CLI example sizes

No performance concerns: The string operations are lightweight and appropriate for CLI help text generation.

Security Concerns 🔒

No security issues identified:

  • No user input validation needed (internal tooling)
  • No external dependencies introduced beyond existing heredoc library
  • No credentials or sensitive data handling

Test Coverage 📊

Observations:

  • Excellent new test suite in examples_test.go with 118 lines
  • Tests cover empty input, mixed content, whitespace handling, and color modes
  • Good edge case coverage including ANSI stripping and color toggling at examples_test.go:94-118
  • Tests validate both the text output and ANSI color formatting

Test quality is high with comprehensive coverage of the new functionality.

Specific File Feedback 📁

internal/pkg/utils/help/examples.go - Excellent implementation

  • Clean, readable, well-documented
  • Handles edge cases appropriately
  • Good use of existing style utilities

Command files - Consistent improvements

  • internal/pkg/cli/command/auth/clear.go:22-31 - Good example of clean conversion
  • internal/pkg/cli/command/index/create.go:82-91 - Complex examples handled well
  • All files show consistent application of the new pattern

Final Recommendation ✨

Approve and merge - This is a high-quality refactoring that:

  • Improves code maintainability
  • Enhances user experience with consistent help formatting
  • Reduces technical debt
  • Follows Go best practices
  • Maintains backward compatibility

The comprehensive test suite addresses test coverage concerns, making this a complete and well-tested improvement.


createParams.Name = options.name
}
if options.roles != nil {
if len(options.roles) > 0 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fix for defaulting the appropriate role if a user hasn't passed any.

@austin-denoble austin-denoble merged commit 33be8e4 into main Oct 1, 2025
7 checks passed
@austin-denoble austin-denoble deleted the adenoble/clean-up-examples branch October 1, 2025 17:20
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