Create and use gofmt.sh wrapper script

This commit is contained in:
Michael Wolf
2023-09-30 17:48:46 -06:00
parent 0273d5762e
commit 7866b6de55
2 changed files with 58 additions and 21 deletions

52
gofmt.sh Executable file
View File

@@ -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

View File

@@ -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