diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 88ca2f1..a6ea7ba 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -25,10 +25,11 @@ jobs: - uses: actions/checkout@v3 # Runs a set of commands using the runners shell - - name: Install Expect + - name: Install tools run: | - sudo apt-get -y install expect + sudo apt-get -y install expect shellcheck hash expect + hash shellcheck - name: Login to Docker Hub uses: docker/login-action@v2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd16ba2..b690bad 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,3 +8,20 @@ repos: - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files +- repo: local + hooks: + - id: shell-fmt + name: shfmt + description: Check all shell scripts are in canonical format. + entry: shfmt + language: system + types: [file, shell] + exclude_types: [zsh] + args: [-w,-i,"4"] + - id: shell-check + name: shellcheck + description: Shell script analysis tool + entry: shellcheck + language: system + types: [file, shell] + files: ^bash_profile_switcher.sh$ diff --git a/Makefile b/Makefile index 01d3dcb..1f2c52d 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ else endif test: + echo "Checking shell format..." + docker run --rm -v "$(PWD)/bash_profile_switcher.sh:/tmp/bash_profile_switcher.sh:ro" mvdan/shfmt:v3 -d -i 4 /tmp/bash_profile_switcher.sh + echo "Analyze with shellcheck..." + shellcheck bash_profile_switcher.sh docker pull bash:5 ./tests/automated_tests.exp clean: diff --git a/bash_profile_switcher.sh b/bash_profile_switcher.sh index fedca79..12346de 100644 --- a/bash_profile_switcher.sh +++ b/bash_profile_switcher.sh @@ -1,3 +1,4 @@ +# shellcheck shell=bash # Bash profile switcher # bash-profile-switcher.sh @@ -40,21 +41,22 @@ alias _save_bash_profile='eval echo "export BASH_CURRENT_PROFILE=$SELECTED_PROFI alias _reset_bash_profile='eval echo "unset BASH_CURRENT_PROFILE" > "$HOME/$SWITCH_PROFILE_SAVED"' # Create list of profiles from .load files -_switch_profile_list () { - local PROFILE_LIST - # Note: If there are no matching files, echo *.load output literally "*.load" - PROFILE_LIST="$(cd "$HOME/$SWITCH_PROFILE_DIRECTORY"; echo *.load)" - PROFILE_LIST="${PROFILE_LIST//.load/}" - [ "$PROFILE_LIST" = '*' ] && PROFILE_LIST="" - echo "$PROFILE_LIST" +_switch_profile_list() { + local PROFILE_LIST + # Note: If there are no matching files, echo *.load output literally "*.load" + PROFILE_LIST="$(echo "$HOME/$SWITCH_PROFILE_DIRECTORY/"*.load)" + PROFILE_LIST="${PROFILE_LIST//$HOME\/$SWITCH_PROFILE_DIRECTORY\//}" + PROFILE_LIST="${PROFILE_LIST//.load/}" + [ "$PROFILE_LIST" = '*' ] && PROFILE_LIST="" + echo "$PROFILE_LIST" } SWITCH_PROFILE_LIST=$(_switch_profile_list) export SWITCH_PROFILE_LIST ### FUNCTION DECLARATION ### -_switch_profile_help () { -cat << EOF +_switch_profile_help() { + cat <