-
Notifications
You must be signed in to change notification settings - Fork 8
Enforce bare-group-shows-help convention #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #!/usr/bin/env bash | ||
| # Verify that command group parents don't set RunE (bare invocation shows help). | ||
| # | ||
| # Shortcut commands that intentionally perform an action AND have subcommands | ||
| # are listed in the allowlist below. | ||
| set -euo pipefail | ||
|
|
||
| COMMANDS_DIR="internal/commands" | ||
|
|
||
| # Commands allowed to have RunE + AddCommand (shortcuts, not groups) | ||
| ALLOWLIST=( | ||
| NewCardCmd # shortcut: creates a card | ||
| NewCompletionCmd # dispatches by shell arg | ||
| NewRecordingsCmd # shortcut: lists recordings | ||
| NewSearchCmd # shortcut: performs search | ||
| NewSetupCmd # wizard entry point | ||
| NewSkillCmd # renders skill content | ||
| NewTimesheetCmd # shortcut: shows report | ||
| NewURLCmd # shortcut: opens URL | ||
| ) | ||
|
|
||
| is_allowed() { | ||
| local func="$1" | ||
| for allowed in "${ALLOWLIST[@]}"; do | ||
| if [[ "$func" == "$allowed" ]]; then | ||
| return 0 | ||
| fi | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| violations=0 | ||
|
|
||
| # Find exported New*Cmd functions that have both AddCommand and RunE | ||
| for file in "$COMMANDS_DIR"/*.go; do | ||
| [[ "$file" == *_test.go ]] && continue | ||
|
|
||
| # Extract each NewXxxCmd function body (from "func NewXxxCmd" to next "^func ") | ||
| while IFS= read -r func_name; do | ||
| # Get the function body | ||
| body=$(sed -n "/^func ${func_name}(/,/^func /p" "$file" | sed '$d') | ||
|
|
||
| has_addcommand=$(echo "$body" | grep -Ec 'AddCommand|cmd\.AddCommand' || true) | ||
| has_rune=$(echo "$body" | grep -c 'RunE:' || true) | ||
|
|
||
| if [[ "$has_addcommand" -gt 0 && "$has_rune" -gt 0 ]]; then | ||
| if ! is_allowed "$func_name"; then | ||
| echo "FAIL: $file: $func_name has RunE + AddCommand (group parents should show help, not run)" | ||
| ((violations++)) | ||
| fi | ||
| fi | ||
| done < <(grep -Eo 'func (New[A-Za-z]+Cmd)\(' "$file" | sed 's/func //; s/(//' | sort -u) | ||
| done | ||
|
|
||
| if [[ "$violations" -gt 0 ]]; then | ||
| echo "" | ||
| echo "$violations violation(s). Group commands must not set RunE." | ||
| echo "If this is an intentional shortcut, add it to the ALLOWLIST in $0." | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Bare group convention check passed ($(printf '%s\n' "${ALLOWLIST[@]}" | wc -l | tr -d ' ') allowlisted shortcuts)" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.