From 7866b6de555f2cbf9183b36ae6e1f9a24c574912 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Sat, 30 Sep 2023 17:48:46 -0600 Subject: [PATCH] Create and use gofmt.sh wrapper script --- gofmt.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++ would-reformat.sh | 27 ++++++------------------ 2 files changed, 58 insertions(+), 21 deletions(-) create mode 100755 gofmt.sh diff --git a/gofmt.sh b/gofmt.sh new file mode 100755 index 0000000..1fb55ba --- /dev/null +++ b/gofmt.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +set -uo pipefail +IFS=$'\n\t' + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source "$DIR/_reformat-common.bash" + +set -e + +file="$1" + +pushd "$(dirname "$file")" 2>&1 + +wrflog hello + +if [[ "$WOULD_REFORMAT" = "would_reformat" ]]; then + success_retval=0 + would_reformat_retval=1 + syntax_error_retval=2 + + set +e + out=$(gofmt -l "$file") + exitval="$?" + set -e + +wrflog out: "$out" + + if [[ "$exitval" = "$syntax_error_retval" ]]; then + retval="$exitval" + elif [[ "$out" = "$file" ]]; then + retval="$would_reformat_retval" + else + # success + retval="$exitval" + fi + + echo "$out" + exit "$retval" + +fi + +if [[ "$WOULD_REFORMAT" = "do_reformat" ]]; then + out=$(npx prettier --write "$file" 2>&1 >/dev/null) + retval="$?" + + echo "$out" + exit "$retval" +fi + +exit 255 diff --git a/would-reformat.sh b/would-reformat.sh index 4a3a794..f3563dd 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -35,35 +35,16 @@ if [[ $file_type = "javascript" || out=$("$WF_ROOT"/./prettier.sh "$file" 2>&1 >/dev/null) retval="$?" set -e - success_retval=0 - would_reformat_retval=1 - syntax_error_retval=2 elif [[ $file_type == "python" ]]; then set +e out=$("$WF_ROOT"/./isort-and-black.sh "$file" 2>&1 >/dev/null) retval="$?" set -e - success_retval=0 - would_reformat_retval=1 - syntax_error_retval=2 elif [[ $file_type == "golang" ]]; then - success_retval=0 - would_reformat_retval=1 - syntax_error_retval=2 - set +e - out=$(gofmt -l "$file") - exitval="$?" + out=$("$WF_ROOT"/./gofmt.sh "$file") + retval="$?" set -e - - if [[ "$exitval" = "$syntax_error_retval" ]]; then - retval="$exitval" - elif [[ "$out" = "$file" ]]; then - retval="$would_reformat_retval" - else - # success - retval="$exitval" - fi elif [[ $file_type == "dart" ]]; then success_retval=0 would_reformat_retval=1 @@ -82,6 +63,10 @@ else fi +success_retval=0 +would_reformat_retval=1 +syntax_error_retval=2 + if [[ "$retval" == "$success_retval" ]]; then echo -n "would not change" exit 0