Skip to content

feat: add cv yaml to json conversion#37

Merged
vitali87 merged 10 commits intomainfrom
feat/cv-yaml-to-json
Mar 27, 2026
Merged

feat: add cv yaml to json conversion#37
vitali87 merged 10 commits intomainfrom
feat/cv-yaml-to-json

Conversation

@vitali87
Copy link
Owner

Summary

  • cv yaml <input> to json [yield <output>] — converts YAML to JSON using yq
  • File existence check, dry-run support, unsupported format handling
  • Added 3 tests

Test plan

  • u7 cv yaml config.yaml to json — creates config.json
  • u7 cv yaml nonexistent.yaml to json — reports missing file
  • Run full test suite

@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

/gemini review

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new command-line utility within the u7 tool to facilitate the conversion of YAML data to JSON. This enhancement provides users with a straightforward method to transform configuration or data files between these common formats, improving interoperability and workflow efficiency. The implementation includes necessary validations and testing to ensure reliability.

Highlights

  • New Feature: YAML to JSON Conversion: Introduced a new u7 cv yaml <input> to json [yield <output>] command to convert YAML files to JSON format using yq.
  • Robustness and Error Handling: Implemented checks for file existence, dry-run support, and proper error handling for unsupported conversion formats.
  • Comprehensive Testing: Added three new tests to cover successful YAML to JSON conversion, handling of missing input files, and rejection of unsupported output formats.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a useful feature for converting YAML files to JSON. The implementation in utility.sh is mostly solid, but I've pointed out a missing argument validation that should be addressed. The accompanying tests in test.sh cover the basic success and failure cases, but their structure should be refactored to align with the existing test suite's setup and teardown process. Additionally, for a complete implementation, please consider updating the shell autocompletion in _u7_complete_entities to include the new yaml entity for the cv verb.

utility.sh Outdated
Comment on lines +969 to +971
if [[ "$4" == "yield" ]]; then
output="$5"
fi

Choose a reason for hiding this comment

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

high

The code should validate that an output filename is provided after the yield keyword. If a user runs u7 cv yaml test.yaml to json yield without specifying a filename, the $5 variable will be empty, leading to a runtime error instead of a helpful usage message. This is inconsistent with how other commands like cv csv handle this case.

Suggested change
if [[ "$4" == "yield" ]]; then
output="$5"
fi
if [[ "$4" == "yield" ]]; then
if [[ -z "$5" ]]; then
echo "Usage: u7 cv yaml <input> to json [yield <output>]"
return 1
fi
output="$5"
fi

Comment on lines +845 to +868
# Test: Convert YAML to JSON
TEST_DIR=$(mktemp -d)
cd "$TEST_DIR"
echo -e "key: value\nlist:\n - one\n - two" > test.yaml
u7 cv yaml test.yaml to json yield test.json >/dev/null 2>&1
if [[ -f "test.json" ]]; then
result=$(cat test.json)
assert_contains "cv yaml to json works" "key" "$result"
else
echo -e "${RED}✗${NC} cv yaml to json (file not created)"
((FAILED++))
fi

# Test: Convert YAML missing file
result=$(u7 cv yaml nonexistent.yaml to json 2>&1)
assert_contains "cv yaml reports missing file" "not found" "$result"

# Test: Convert YAML unsupported format
result=$(u7 cv yaml test.yaml to xml 2>&1)
assert_contains "cv yaml rejects unsupported format" "Unsupported" "$result"

cd /
rm -rf "$TEST_DIR"

Choose a reason for hiding this comment

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

medium

This block of tests introduces its own temporary directory setup and cleanup (mktemp, cd, rm), which is redundant and breaks the existing structure of the test script where a single TEST_DIR is used for all tests. To improve maintainability and align with the rest of the test suite, these tests should be integrated into the main test environment.

Please refactor this by:

  1. Moving this test block before the main cleanup section (i.e., before line 842 cd /).
  2. Removing the redundant TEST_DIR management lines (TEST_DIR=$(mktemp -d), cd "$TEST_DIR", cd /, and rm -rf "$TEST_DIR") from this block.

@greptile-apps
Copy link

greptile-apps bot commented Mar 24, 2026

Greptile Summary

This PR adds a yaml → json sub-command to the u7 cv convert family, mirroring the existing json → yaml and csv → json handlers. The implementation addresses the concerns raised in the previous review round.\n\nKey changes:\n- New yaml) case in lib/convert.sh with file-existence check, atomic write via mktemp/mv, yield-with-no-arg guard, dry-run support, and an unsupported-format catch-all\n- Comment explicitly documents the Mike Farah (yq-go) flavour requirement\n- Dry-run message for the json → yaml handler now quotes $input/$output (cosmetic fix)\n- 3 new tests in test.sh, each properly scoped to a fresh mktemp -d directory; the conversion and unsupported-format tests are gated on command -v yq to handle environments without the tool\n- Test 17 (json → yaml) stderr is no longer suppressed, which makes failures easier to diagnose\n\nRemaining minor point (previously flagged):\n- _u7_require yq is still placed before the case \"$to_fmt\" block rather than inside the json) branch. This means running u7 cv yaml file.yaml to xml in an environment without yq produces "yq not found" instead of "Unsupported conversion: yaml to xml". The new tests guard this scenario with command -v yq, so CI will not break, but the UX ordering is inconsistent with the csv handler.

Confidence Score: 5/5

Safe to merge — all P0/P1 issues from prior rounds were addressed; only a P2 style point remains.

The implementation correctly uses atomic writes, guards yield without an argument, checks file existence before dependency resolution, and wraps tests in command -v yq guards. No new P0 or P1 issues were found. The one outstanding concern (_u7_require yq ordering) is a pre-flagged P2 UX inconvenience that does not affect correctness or CI stability.

No files require special attention; lib/convert.sh has a minor ordering nit already noted.

Important Files Changed

Filename Overview
lib/convert.sh Adds yaml→json handler with atomic writes via temp file, yield-guard, file-existence check, dry-run support, and a help-text entry. Minor: _u7_require yq sits before the case block and the default output path is computed before it is known to be needed.
test.sh Adds 3 YAML tests in a fresh temp directory, properly guarded by command -v yq. Splits Test 17 assert into two. Removal of 2>&1 from the JSON→YAML test redirect means yq warnings/errors now surface in test output, which is an improvement.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["u7 cv yaml input to fmt"] --> B{"$2 == 'to'?"}
    B -- No --> C["echo Usage; return 1"]
    B -- Yes --> D{"$4 == 'yield'?"}
    D -- "Yes, $5 empty" --> E["echo Usage; return 1"]
    D -- "Yes, $5 set" --> F["output = $5"]
    D -- No --> G["output = input basename + .fmt"]
    F --> H
    G --> H{"input file exists?"}
    H -- No --> I["echo File not found; return 1"]
    H -- Yes --> J["_u7_require yq"]
    J -- "yq missing" --> K["echo yq not found; return 1"]
    J -- "yq present" --> L{"case to_fmt"}
    L -- json --> M{"DRY_RUN?"}
    M -- Yes --> N["echo dry-run message"]
    M -- No --> O["mktemp output.XXXXXX"]
    O --> P{"yq -o=json < input > tmpfile"}
    P -- success --> Q["mv tmpfile output"]
    P -- failure --> R["rm tmpfile; return 1"]
    L -- other --> S["echo Unsupported conversion; return 1"]
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: lib/convert.sh
Line: 197

Comment:
**Default output path computed before file existence check**

`output` is derived from `input` on line 197 unconditionally. If the `yield` branch isn't taken, the output path is silently based on whatever `$to_fmt` is — including unsupported formats like `xml`. For unsupported formats the output variable is never used (the `*` catch-all short-circuits before writing), so there is no data hazard. However, for readability and defensive correctness, it is clearer to move the output default inside the `json)` branch, consistent with how the output path is only meaningful for supported targets. As written it is harmless but could mislead future contributors.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (10): Last reviewed commit: "fix: create temp dir for yaml tests afte..." | Re-trigger Greptile

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request successfully introduces functionality to convert YAML files to JSON, enhancing the cv verb. The new feature includes file existence checks, dry-run support, and proper handling of unsupported conversion formats. The accompanying tests adequately cover the new functionality and error cases. One minor improvement could be made to ensure robust argument parsing for the yield option.

utility.sh Outdated
Comment on lines +969 to +971
if [[ "$4" == "yield" ]]; then
output="$5"
fi

Choose a reason for hiding this comment

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

medium

The yaml conversion logic is missing a check for an empty output filename when the yield keyword is used. This could lead to unexpected behavior if a user types u7 cv yaml input.yaml to json yield without providing an output file name. For consistency and robustness, consider adding a check similar to the one present in the csv conversion, which ensures that if yield is specified, an actual output filename ($5) is provided.

Suggested change
if [[ "$4" == "yield" ]]; then
output="$5"
fi
if [[ "$4" == "yield" ]]; then
if [[ -z "$5" ]]; then
echo "Usage: u7 cv yaml <input> to json [yield <output>]"
return 1
fi
output="$5"
fi

@vitali87
Copy link
Owner Author

@greptile

1 similar comment
@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

@greptile

1 similar comment
@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

@greptile

@vitali87 vitali87 force-pushed the feat/cv-yaml-to-json branch from 49d5a42 to 1b90789 Compare March 27, 2026 09:22
@vitali87
Copy link
Owner Author

@greptile

@vitali87
Copy link
Owner Author

@greptile

@vitali87 vitali87 merged commit c07a5d9 into main Mar 27, 2026
3 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