From 973b9e4c2f68822a2c9353704160962513d98510 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Sat, 30 Sep 2023 18:14:47 -0600 Subject: [PATCH] Move wrapper selection into a function --- _reformat-common.bash | 28 ++++++++++++++++++++++++++++ default.sh | 2 +- would-reformat.sh | 39 +++++---------------------------------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/_reformat-common.bash b/_reformat-common.bash index 2519d31..c5c82f0 100644 --- a/_reformat-common.bash +++ b/_reformat-common.bash @@ -165,6 +165,34 @@ function sniff_file_type() { echo $file_type } +function choose_wrapper() { + file_type="$1" + + if [[ $file_type = "javascript" || + $file_type = "vue" || + $file_type = "typescript" || + $file_type = "css" || + $file_type = "php" || + $file_type = "html" || + $file_type = "jsx" || + $file_type = "tsx" || + $file_type == "css" || + $file_type == "scss" || + $file_type = "sh" ]]; then + out="$WF_ROOT"/./prettier.sh + elif [[ $file_type == "python" ]]; then + out="$WF_ROOT"/./isort-and-black.sh + elif [[ $file_type == "golang" ]]; then + out="$WF_ROOT"/./gofmt.sh + elif [[ $file_type == "dart" ]]; then + out="$WF_ROOT"/./dart_format.sh + else + out="$WF_ROOT"/./default.sh + fi + + echo "$out" +} + function wrflog() { echo "$@" >>/tmp/wrflog } diff --git a/default.sh b/default.sh index 8bb2d30..50e7a8c 100644 --- a/default.sh +++ b/default.sh @@ -1,4 +1,4 @@ #!/bin/bash -echo "ignoring" +echo -n "unhandled file type; ignoring" exit 0 diff --git a/would-reformat.sh b/would-reformat.sh index 54f4c02..99cee8e 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -22,41 +22,12 @@ file_type=$(sniff_file_type "$file") export WOULD_REFORMAT=would_reformat -if [[ $file_type = "javascript" || - $file_type = "vue" || - $file_type = "typescript" || - $file_type = "css" || - $file_type = "php" || - $file_type = "html" || - $file_type = "js" || - $file_type = "ts" || - $file_type == "css" || - $file_type == "scss" || - $file_type = "sh" ]]; then - set +e - out=$("$WF_ROOT"/./prettier.sh "$file" 2>&1 >/dev/null) - retval="$?" - set -e -elif [[ $file_type == "python" ]]; then - set +e - out=$("$WF_ROOT"/./isort-and-black.sh "$file" 2>&1 >/dev/null) - retval="$?" - set -e -elif [[ $file_type == "golang" ]]; then - set +e - out=$("$WF_ROOT"/./gofmt.sh "$file") - retval="$?" - set -e -elif [[ $file_type == "dart" ]]; then - set +e - out=$("$WF_ROOT"/./dart_format.sh "$file") - retval="$?" - set -e -else - echo -n "ignoring" - exit 0 +wrapper=$(choose_wrapper "$file_type") -fi +set +e +out=$("$wrapper" "$file" 2>&1 >/dev/null) +retval="$?" +set -e success_retval=0 would_reformat_retval=1