From 3045dd55a3c0c5557f707ad3b0e841218008d19f Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Thu, 6 Jan 2022 15:08:18 +0200 Subject: [PATCH] chore: Add shellcheck and make checks passing (#315) Co-authored-by: Anton Babenko --- .github/workflows/pre-commit.yaml | 4 ++++ .pre-commit-config.yaml | 1 + infracost_breakdown.sh | 4 +++- terraform_docs.sh | 14 ++++++++----- tests/hooks_performance_test.sh | 35 +++++++++++++++---------------- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index 07241d1a0..6f2ec84ff 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -21,6 +21,10 @@ jobs: run: | curl -L "$(curl -s https://api.github.com/repos/mvdan/sh/releases/latest | grep -o -E -m 1 "https://.+?linux_amd64")" > shfmt \ && chmod +x shfmt && sudo mv shfmt /usr/bin/ + + - name: Install shellcheck + run: | + sudo apt update && sudo apt install shellcheck # Need to success pre-commit fix push - uses: actions/checkout@v2 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f1a7a65ef..f876ccd35 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,3 +33,4 @@ repos: hooks: - id: shfmt args: ['-l', '-i', '2', '-ci', '-sr', '-w'] + - id: shellcheck diff --git a/infracost_breakdown.sh b/infracost_breakdown.sh index c56e4cd73..f2cb816dd 100755 --- a/infracost_breakdown.sh +++ b/infracost_breakdown.sh @@ -62,6 +62,7 @@ function common::parse_cmdline { ;; --) shift + # shellcheck disable=SC2034 # Common function FILES=("$@") break ;; @@ -101,6 +102,7 @@ function infracost_breakdown_ { # $hook_config receives string like '1 > 2; 3 == 4;' etc. # It gets split by `;` into array, which we're parsing here ('1 > 2' ' 3 == 4') # Next line removes leading spaces, just for fancy output reason. + # shellcheck disable=SC2001 # Rule exception check=$(echo "$check" | sed 's/^[[:space:]]*//') # Drop quotes in hook args section. From: @@ -116,7 +118,7 @@ function infracost_breakdown_ { }; then check="${check:1:-1}" fi - + # shellcheck disable=SC2207 # Can't find working `read` command operations=($(echo "$check" | grep -oE '[!<>=]{1,2}')) # Get the very last operator, that is used in comparison inside `jq` query. # From the example below we need to pick the `>` which is in between `add` and `1000`, diff --git a/terraform_docs.sh b/terraform_docs.sh index 0c1e71159..f2efb0af7 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -121,9 +121,11 @@ function terraform_docs { local add_to_existing=false local create_if_not_exist=false - configs=($hook_config) + read -r -a configs <<< "$hook_config" + for c in "${configs[@]}"; do - config=(${c//=/ }) + + IFS="=" read -r -a config <<< "$c" key=${config[0]} value=${config[1]} @@ -161,9 +163,11 @@ function terraform_docs { dir="$(dirname "$text_file")" mkdir -p "$dir" - echo -e "# ${PWD##*/}\n" >> "$text_file" - echo "" >> "$text_file" - echo "" >> "$text_file" + { + echo -e "# ${PWD##*/}\n" + echo "" + echo "" + } >> "$text_file" fi # If file still not exist - skip dir diff --git a/tests/hooks_performance_test.sh b/tests/hooks_performance_test.sh index 342bb302e..4f35fce23 100755 --- a/tests/hooks_performance_test.sh +++ b/tests/hooks_performance_test.sh @@ -44,33 +44,33 @@ function generate_table { | time command | max | min | mean | median | | -------------- | ------ | ------ | -------- | ------ | | users seconds | $( - printf %"s\n" $users_seconds | datamash max 1 + printf %"s\n" "$users_seconds" | datamash max 1 ) | $( - printf %"s\n" $users_seconds | datamash min 1 + printf %"s\n" "$users_seconds" | datamash min 1 ) | $( - printf %"s\n" $users_seconds | datamash mean 1 - ) | $(printf %"s\n" $users_seconds | datamash median 1) | + printf %"s\n" "$users_seconds" | datamash mean 1 + ) | $(printf %"s\n" "$users_seconds" | datamash median 1) | | system seconds | $( - printf %"s\n" $system_seconds | datamash max 1 + printf %"s\n" "$system_seconds" | datamash max 1 ) | $( - printf %"s\n" $system_seconds | datamash min 1 + printf %"s\n" "$system_seconds" | datamash min 1 ) | $( - printf %"s\n" $system_seconds | datamash mean 1 - ) | $(printf %"s\n" $system_seconds | datamash median 1) | + printf %"s\n" "$system_seconds" | datamash mean 1 + ) | $(printf %"s\n" "$system_seconds" | datamash median 1) | | CPU % | $( - printf %"s\n" $cpu | datamash max 1 + printf %"s\n" "$cpu" | datamash max 1 ) | $( - printf %"s\n" $cpu | datamash min 1 + printf %"s\n" "$cpu" | datamash min 1 ) | $( - printf %"s\n" $cpu | datamash mean 1 - ) | $(printf %"s\n" $cpu | datamash median 1) | + printf %"s\n" "$cpu" | datamash mean 1 + ) | $(printf %"s\n" "$cpu" | datamash median 1) | | Total time | $( - printf %"s\n" $total_time | datamash max 1 + printf %"s\n" "$total_time" | datamash max 1 ) | $( - printf %"s\n" $total_time | datamash min 1 + printf %"s\n" "$total_time" | datamash min 1 ) | $( - printf %"s\n" $total_time | datamash mean 1 - ) | $(printf %"s\n" $total_time | datamash median 1) | + printf %"s\n" "$total_time" | datamash mean 1 + ) | $(printf %"s\n" "$total_time" | datamash median 1) | " } @@ -82,8 +82,7 @@ function save_result { local FILE_NAME=${5:-"tests_result.md"} - echo -e "\n$DESCRIPTION" >> "tests/results/$FILE_NAME" - echo -e "$TABLE" >> "tests/results/$FILE_NAME" + echo -e "\n$DESCRIPTION\n$TABLE" >> "tests/results/$FILE_NAME" # shellcheck disable=SC2016,SC2128 # Irrelevant echo -e '
Run details