Skip to content
Merged
41 changes: 40 additions & 1 deletion lib/convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ _u7_convert() {
case "$to_fmt" in
yaml|yml)
if [[ "$_U7_DRY_RUN" == "1" ]]; then
echo "[dry-run] yq -P < $input > $output"
echo "[dry-run] yq -P < \"$input\" > \"$output\""
else
yq -P < "$input" > "$output"
fi
Expand All @@ -187,6 +187,44 @@ _u7_convert() {
esac
;;

yaml)
local input="$1"
if [[ "$2" != "to" ]]; then
echo "Usage: u7 cv yaml <input> to json [yield <output>]"
return 1
fi
local to_fmt="$3"
local output="${input%.*}.$to_fmt"
if [[ "$4" == "yield" ]]; then
if [[ -z "$5" ]]; then
echo "Usage: u7 cv yaml <input> to json [yield <output>]"
return 1
fi
output="$5"
fi

if [[ ! -f "$input" ]]; then
echo "File not found: $input"
return 1
fi

# Requires yq-go (Mike Farah's yq), provided by Nix
_u7_require yq || return 1

case "$to_fmt" in
json)
if [[ "$_U7_DRY_RUN" == "1" ]]; then
echo "[dry-run] yq -o=json < \"$input\" > \"$output\""
else
local tmpfile
tmpfile=$(mktemp "${output}.XXXXXX") || { echo "Error: Failed to create temp file"; return 1; }
yq -o=json < "$input" > "$tmpfile" && mv "$tmpfile" "$output" || { rm -f "$tmpfile"; return 1; }
fi
;;
*) echo "Unsupported conversion: yaml to $to_fmt" ; return 1 ;;
esac
;;

csv)
local input="$1"
if [[ "$2" != "to" ]]; then
Expand Down Expand Up @@ -279,6 +317,7 @@ Entities:
image <input> to <format> [yield <output>] Convert image (png/jpg/webp/gif/etc)
video <input> to <format> [yield <output>] Convert video
json <input> to yaml [yield <output>] Convert JSON to YAML
yaml <input> to json [yield <output>] Convert YAML to JSON
csv <input> to json [yield <output>] Convert CSV to JSON
case upper to lower on <files...> Rename to lowercase
case lower to upper on <files...> Rename to uppercase
Expand Down
38 changes: 36 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ assert_equals "Set file permissions" "644" "$perms"

# Test 17: Convert JSON to YAML
echo '{"key": "value"}' > test.json
u7 cv json test.json to yaml yield test.yaml >/dev/null 2>&1
u7 cv json test.json to yaml yield test.yaml >/dev/null
if [[ -f "test.yaml" ]]; then
result=$(cat test.yaml)
assert_contains "JSON to YAML conversion" "key: value" "$result"
assert_contains "JSON to YAML conversion has key" "key" "$result"
assert_contains "JSON to YAML conversion has value" "value" "$result"
else
echo -e "${RED}✗${NC} JSON to YAML conversion (file not created)"
((FAILED++))
Expand Down Expand Up @@ -904,6 +905,39 @@ assert_contains "sh system shows user" "User:" "$result"
cd /
rm -rf "$TEST_DIR"

# Test: Convert YAML to JSON
echo -e "key: value\nlist:\n - one\n - two" > test.yaml
if command -v yq &>/dev/null; then
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
else
echo -e "${GREEN}✓${NC} cv yaml to json (skipped - yq not installed)"
((PASSED++))
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
if command -v yq &>/dev/null; then
result=$(u7 cv yaml test.yaml to xml 2>&1)
assert_contains "cv yaml rejects unsupported format" "Unsupported" "$result"
else
echo -e "${GREEN}✓${NC} cv yaml rejects unsupported format (skipped - yq not installed)"
((PASSED++))
fi

# Cleanup
cd /
rm -rf "$TEST_DIR"

# Summary
echo "==================="
echo "Tests passed: $PASSED"
Expand Down
Loading