From ecf60a5f2f597504f6221d87b6f88d5afe5fba9a Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Thu, 13 Oct 2022 18:43:40 -0500 Subject: [PATCH] Teach it about golang --- _reformat-common.sh | 2 ++ do-reformat.sh | 4 ++++ would-reformat.sh | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/_reformat-common.sh b/_reformat-common.sh index 171b89f..19152ff 100644 --- a/_reformat-common.sh +++ b/_reformat-common.sh @@ -27,6 +27,8 @@ function sniff_file_type() { file_type="scss" elif [[ $ff = *.sh ]]; then file_type="sh" + elif [[ $ff = *.go ]] ; then + file_type="golang" fi echo $file_type diff --git a/do-reformat.sh b/do-reformat.sh index b566c07..042f0b9 100755 --- a/do-reformat.sh +++ b/do-reformat.sh @@ -29,6 +29,10 @@ elif [[ $file_type == "python" ]]; then out=$(pipx run black $file 2>/dev/null && pipx run isort $file 2>/dev/null) retval="$?" success_retval=0 +elif [[ $file_type == "golang" ]] ; then + out=$(gofmt -w "$file" 2>/dev/null) + retval="$?" + success_retval=0 else echo -n "ignoring" exit 0 diff --git a/would-reformat.sh b/would-reformat.sh index d031b3d..96ab8ff 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -42,6 +42,22 @@ elif [[ $file_type == "python" ]]; then else retval="$isort_retval" fi +elif [[ $file_type == "golang" ]] ; then + success_retval=0 + would_reformat_retval=1 + syntax_error_retval=2 + + out=$(gofmt -l $file) + exitval="$?" + + if [[ x"$exitval" = x"$syntax_error_retval" ]] ; then + retval="$exitval" + elif [[ x"$out" = x"$file" ]] ; then + retval="$would_reformat_retval" + else + # success + retval="$exitval" + fi else echo -n "ignoring" exit 0