Skip to content

Commit

Permalink
prettybat: Use yq for YAML and JSON pretty formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eth-p committed Aug 24, 2024
1 parent 32706dd commit 47cfb9e
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/prettybat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ hook_version
# -----------------------------------------------------------------------------

FORMATTERS=(
"prettier" "rustfmt" "shfmt" "clangformat"
"yq" "prettier" "rustfmt" "shfmt" "clangformat"
"black" "mix_format" "column"
)

Expand Down Expand Up @@ -171,6 +171,46 @@ formatter_column_process() {
{ { "$needs_newline" && sed 's/$/\n/'; } || cat; } | column "${args[@]}"
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

formatter_yq_supports__version_ok() {
local yq_version
yq_version=$(yq --version | sed 's/^.* version v//')

# If it's older than yq version 4, replace the functions to save processing.
if version_compare "$yq_version" -lt 4.0; then
formatter_yq_supports() { return 1; }
formatter_yq_supports__version_ok() { return 1; }
return 1
fi

# It's supported.
formatter_yq_supports__version_ok() { return 0; }
return 0
}

formatter_yq_supports() {
case "$1" in
.yaml | yml | \
.json)
formatter_yq_supports__version_ok
return $?
;;
esac

return 1
}

formatter_yq_process() {
local args=()
case "$1" in
*.json) args+=(--output-format json) ;;
esac

yq --prettyPrint --indent 2 "${args[@]}"
return $?
}

# -----------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 47cfb9e

Please sign in to comment.