From 31586acda246c4a08d23513685870baeea51452e Mon Sep 17 00:00:00 2001 From: Denis Costa Date: Tue, 19 Mar 2024 11:22:12 -0300 Subject: [PATCH] Add shfmt --- .docker/lint-sh.Dockerfile | 2 +- .editorconfig | 3 ++ Makefile | 6 +++- roles/bash/files/bin/clipboard.sh | 14 ++++----- roles/bash/files/bin/colors.sh | 9 +++--- roles/bash/files/bin/passphrase.sh | 48 ++++++++++++++++-------------- roles/git/files/bin/workfy-repo.sh | 2 +- roles/git/files/functions.sh | 4 +-- scripts/lint-sh.sh | 4 +++ scripts/lint-yml.sh | 2 +- 10 files changed, 53 insertions(+), 41 deletions(-) diff --git a/.docker/lint-sh.Dockerfile b/.docker/lint-sh.Dockerfile index da23d35..cb0a310 100644 --- a/.docker/lint-sh.Dockerfile +++ b/.docker/lint-sh.Dockerfile @@ -1,6 +1,6 @@ FROM alpine:latest -RUN apk -U upgrade && apk add shellcheck bash +RUN apk -U upgrade && apk add bash shellcheck shfmt RUN mkdir /code WORKDIR /code diff --git a/.editorconfig b/.editorconfig index d16f02f..d685863 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,3 +17,6 @@ indent_style = tab [*.txt] trim_trailing_whitespace = false + +[roles/terminal/files/z/**] +ignore = true diff --git a/Makefile b/Makefile index 3bfa7dc..6ff7372 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,13 @@ run: lint: @docker build -q -f .docker/lint-ansible.Dockerfile -t lint-ansible . + @docker build -q -f .docker/lint-sh.Dockerfile -t lint-sh . @docker run --rm --name dotfiles-lint-ansible -v $$(pwd):/code lint-ansible + @docker run --rm --name dotfiles-lint-sh -v $$(pwd):/code lint-sh ./scripts/lint-sh.sh + +lint-fix: @docker build -q -f .docker/lint-sh.Dockerfile -t lint-sh . - @docker run --rm --name dotfiles-lint-sh -v $$(pwd):/code lint-sh + @docker run --rm --name dotfiles-lint-sh -v $$(pwd):/code lint-sh shfmt -l -w . test: @docker build -q -f .docker/tests.Dockerfile -t tests . diff --git a/roles/bash/files/bin/clipboard.sh b/roles/bash/files/bin/clipboard.sh index 3a6d2e7..2df7ee7 100755 --- a/roles/bash/files/bin/clipboard.sh +++ b/roles/bash/files/bin/clipboard.sh @@ -32,15 +32,15 @@ function prevent_prompt_from_being_on_the_same_line { function detect_os { case "$(uname -s)" in - Linux*) - printf "linux" + Linux*) + printf "linux" ;; - Darwin*) - printf "macos" + Darwin*) + printf "macos" ;; - *) - echo "Clipboard only supports Linux and MacOS." - exit 1 + *) + echo "Clipboard only supports Linux and MacOS." + exit 1 ;; esac } diff --git a/roles/bash/files/bin/colors.sh b/roles/bash/files/bin/colors.sh index 4f456e8..9254170 100755 --- a/roles/bash/files/bin/colors.sh +++ b/roles/bash/files/bin/colors.sh @@ -13,19 +13,18 @@ set -euo pipefail TEXT='gYw' echo -e "\n 40m 41m 42m 43m\ - 44m 45m 46m 47m"; + 44m 45m 46m 47m" for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \ - '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \ - ' 36m' '1;36m' ' 37m' '1;37m'; do + '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \ + ' 36m' '1;36m' ' 37m' '1;37m'; do FG=${FGs// /} echo -en " ${FGs} \033[${FG} ${TEXT} " for BG in 40m 41m 42m 43m 44m 45m 46m 47m; do - echo -en " \033[${FG}\033[${BG} ${TEXT} \033[0m"; + echo -en " \033[${FG}\033[${BG} ${TEXT} \033[0m" done echo done echo - diff --git a/roles/bash/files/bin/passphrase.sh b/roles/bash/files/bin/passphrase.sh index 2612eaf..716f736 100755 --- a/roles/bash/files/bin/passphrase.sh +++ b/roles/bash/files/bin/passphrase.sh @@ -26,27 +26,27 @@ function show_help { while [[ $# -gt 0 ]]; do case $1 in - --min|-n) - min_word_length=$2 - have_min_word_arg=1 - shift + --min | -n) + min_word_length=$2 + have_min_word_arg=1 + shift ;; - --max|-m) - max_word_length=$2 - have_max_word_arg=1 - shift + --max | -m) + max_word_length=$2 + have_max_word_arg=1 + shift ;; - --count|-c) - passphrase_word_count=$2 - shift + --count | -c) + passphrase_word_count=$2 + shift ;; - --help|-h) - show_help - exit 0 + --help | -h) + show_help + exit 0 ;; - *) - echo "Invalid option" - exit 1 + *) + echo "Invalid option" + exit 1 ;; esac shift @@ -62,13 +62,16 @@ fi set +e if [[ ${have_min_word_arg} -eq ${have_max_word_arg} ]]; then elegible_words=$( - grep -E "^[A-Za-z]{${min_word_length},${max_word_length}}$" /usr/share/dict/words | tr '\n' ' ') + grep -E "^[A-Za-z]{${min_word_length},${max_word_length}}$" /usr/share/dict/words | tr '\n' ' ' + ) elif [[ ${have_min_word_arg} -eq 1 ]]; then elegible_words=$( - grep -E "^[A-Za-z]{${min_word_length},}$" /usr/share/dict/words | tr '\n' ' ') + grep -E "^[A-Za-z]{${min_word_length},}$" /usr/share/dict/words | tr '\n' ' ' + ) else elegible_words=$( - grep -E "^[A-Za-z]{,${max_word_length}}$" /usr/share/dict/words | tr '\n' ' ') + grep -E "^[A-Za-z]{,${max_word_length}}$" /usr/share/dict/words | tr '\n' ' ' + ) fi set -e @@ -77,8 +80,7 @@ if [[ -z ${elegible_words} ]]; then exit 1 fi - -word_count=$(wc -w <<< "${elegible_words}") +word_count=$(wc -w <<<"${elegible_words}") for ((i = 0; i < "${passphrase_word_count}"; i++)); do random_number=$RANDOM @@ -86,7 +88,7 @@ for ((i = 0; i < "${passphrase_word_count}"; i++)); do random_number=$((random_number * RANDOM)) done word_index="$((random_number % word_count + 1))" - word="$(cut -d ' ' -f "${word_index}" <<< "${elegible_words}" | tr '[:upper:]' '[:lower:]')" + word="$(cut -d ' ' -f "${word_index}" <<<"${elegible_words}" | tr '[:upper:]' '[:lower:]')" if [[ $i -lt $((passphrase_word_count - 1)) ]]; then echo -n "${word} " diff --git a/roles/git/files/bin/workfy-repo.sh b/roles/git/files/bin/workfy-repo.sh index fdcbf65..94ba946 100755 --- a/roles/git/files/bin/workfy-repo.sh +++ b/roles/git/files/bin/workfy-repo.sh @@ -2,7 +2,7 @@ set -euo pipefail -if git -C . rev-parse 2> /dev/null; then +if git -C . rev-parse 2>/dev/null; then remote_url=$(git remote -v | head -1 | sed 's/.*\t\(.*\) .*/\1/') if [[ ${remote_url} != *"work.github.com"* ]]; then git remote set-url origin "${remote_url//github.com/work.github.com}" diff --git a/roles/git/files/functions.sh b/roles/git/files/functions.sh index aeef28c..a5890ce 100644 --- a/roles/git/files/functions.sh +++ b/roles/git/files/functions.sh @@ -1,10 +1,10 @@ # shellcheck disable=SC2148 function __git_arrows { git rev-parse --abbrev-ref @"{u}" &>/dev/null || return - git rev-list --left-right --count HEAD...@"{u}" &> /dev/null || return + git rev-list --left-right --count HEAD...@"{u}" &>/dev/null || return local branch_status - branch_status=$(git rev-list --left-right --count HEAD...@"{u}" 2> /dev/null) + branch_status=$(git rev-list --left-right --count HEAD...@"{u}" 2>/dev/null) local left_arrow local right_arrow diff --git a/scripts/lint-sh.sh b/scripts/lint-sh.sh index 0cc76b6..27aa4b5 100755 --- a/scripts/lint-sh.sh +++ b/scripts/lint-sh.sh @@ -10,4 +10,8 @@ for script in ${SH_SCRIPTS}; do EXIT_STATUS=1 done +if ! shfmt -d .; then + EXIT_STATUS=1 +fi + exit "${EXIT_STATUS}" diff --git a/scripts/lint-yml.sh b/scripts/lint-yml.sh index 36c20e5..1cd68e7 100755 --- a/scripts/lint-yml.sh +++ b/scripts/lint-yml.sh @@ -12,7 +12,7 @@ for file in **/*.yaml; do done for file in **/*.yml; do - if ! grep '^---$' "${file}" &> /dev/null; then + if ! grep '^---$' "${file}" &>/dev/null; then echo "The file ${file} does not has a '---' at the beggining." EXIT_STATUS=1 fi