Skip to content

Refactor hq CLI for readability and reduced complexity#284

Open
kkozik-amplify wants to merge 5 commits intocomments-hqfrom
refactoring
Open

Refactor hq CLI for readability and reduced complexity#284
kkozik-amplify wants to merge 5 commits intocomments-hqfrom
refactoring

Conversation

@kkozik-amplify
Copy link
Copy Markdown
Collaborator

Summary

The hq CLI module (cli/hq.py) had grown to ~900 lines with a 294-line main(), 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

  • Extract _convert_results() to unify result-conversion + location/provenance injection that was duplicated across _process_file and three serial paths
  • Introduce OutputConfig dataclass to replace 5-6 formatting parameters threaded through _format_result, _format_list, and _format_output; simplifies the _process_file worker tuple from 10 to 6 elements
  • Decompose main() into named phases (_build_parser, _validate_and_configure, _resolve_query, _execute_and_emit), reducing it to 6 lines of orchestration
  • Extract _emit_file_results from _execute_and_emit to isolate per-file output formatting
  • Introduce OutputSink context 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 code

Manual testing checklist

  • hq 'resource' test/integration/hcl2_original/backends.tf — basic structural query still works
  • hq --json 'resource' test/integration/hcl2_original/ — JSON + multi-file output
  • echo 'x = 1' | hq '.' — stdin piping
  • hq --raw '.resource' test/integration/hcl2_original/backends.tf — raw output mode
  • python -m unittest test.unit.cli.test_hq -v — unit tests pass

🤖 Co-Authored-By: Claude Opus 4.6 (1M context) Claude Code

kkozik-amplify and others added 5 commits April 4, 2026 21:16
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>
@kkozik-amplify kkozik-amplify requested a review from a team as a code owner April 4, 2026 20:11
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