Refactor hq CLI for readability and reduced complexity#284
Open
kkozik-amplify wants to merge 5 commits intocomments-hqfrom
Open
Refactor hq CLI for readability and reduced complexity#284kkozik-amplify wants to merge 5 commits intocomments-hqfrom
kkozik-amplify wants to merge 5 commits intocomments-hqfrom
Conversation
The convert-for-json + location/provenance injection loop was duplicated across _process_file and three serial paths in main(). Unified into a single shared function. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…hq CLI Replaces 5-6 repeated formatting params threaded through _format_result, _format_list, and _format_output with methods on a single OutputConfig dataclass. Simplifies the _process_file worker tuple from 10 elements to 6 and removes the too-many-locals pylint suppress. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extracts _build_parser, _validate_and_configure, _resolve_query, and _execute_and_emit from the 294-line main(), reducing it to 6 lines of orchestration. Pylint suppressions move from main() to _execute_and_emit where the inherent branching complexity lives. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Moves serial per-file output formatting into a dedicated function, dropping _execute_and_emit below the too-many-statements threshold and simplifying the with_location path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…cleanups Introduces OutputSink to own the output lifecycle (stream/accumulate/flush), replacing the mutable json_accumulator parameter threading. Reorganizes all definitions into dependency-ordered sections (constants → helpers → query dispatch → output → execution → CLI). Deduplicates file reading via _read_input, simplifies _extract_location with a loop, collapses redundant except blocks, and removes dead code. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
hqCLI module (cli/hq.py) had grown to ~900 lines with a 294-linemain(), duplicated result-conversion logic across multiple code paths, and 5-6 formatting parameters threaded through every function. This PR incrementally decomposes and reorganizes the module into well-separated sections with clear responsibilities.Changes
_convert_results()to unify result-conversion + location/provenance injection that was duplicated across_process_fileand three serial pathsOutputConfigdataclass to replace 5-6 formatting parameters threaded through_format_result,_format_list, and_format_output; simplifies the_process_fileworker tuple from 10 to 6 elementsmain()into named phases (_build_parser,_validate_and_configure,_resolve_query,_execute_and_emit), reducing it to 6 lines of orchestration_emit_file_resultsfrom_execute_and_emitto isolate per-file output formattingOutputSinkcontext manager to own the output lifecycle (stream/accumulate/flush), reorganize all definitions into dependency-ordered sections, deduplicate file reading via_read_input, and remove dead codeManual testing checklist
hq 'resource' test/integration/hcl2_original/backends.tf— basic structural query still workshq --json 'resource' test/integration/hcl2_original/— JSON + multi-file outputecho 'x = 1' | hq '.'— stdin pipinghq --raw '.resource' test/integration/hcl2_original/backends.tf— raw output modepython -m unittest test.unit.cli.test_hq -v— unit tests pass🤖 Co-Authored-By: Claude Opus 4.6 (1M context) Claude Code