Skip to content

Commit

Permalink
Merge pull request #17 from gianluca-mascolo/ci/shellcheck
Browse files Browse the repository at this point in the history
Ci/shellcheck
  • Loading branch information
gianluca-mascolo authored Jul 16, 2022
2 parents eceb41e + 7068279 commit f84326e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 73 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
147 changes: 76 additions & 71 deletions bash_profile_switcher.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck shell=bash
# Bash profile switcher

# bash-profile-switcher.sh
Expand Down Expand Up @@ -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 <<EOF
switch_profile [options] profile
OPTIONS
Expand All @@ -81,72 +83,75 @@ Example:
EOF
}

switch_profile () {
local OPTIND OPTARG SELECTED_PROFILE KEEP_ENV TEMP_PROFILE

KEEP_ENV=0
TEMP_PROFILE=0
while getopts "tkdhl" Option
do
case $Option in
k)
KEEP_ENV=1
;;
t)
TEMP_PROFILE=1
;;
l)
SWITCH_PROFILE_LIST=$(_switch_profile_list)
export SWITCH_PROFILE_LIST
[ -n "$SWITCH_PROFILE_LIST" ] && complete -o nospace -W "$SWITCH_PROFILE_LIST" switch_profile
echo "Available profiles:"
echo -e "${SWITCH_PROFILE_LIST// /\\n}"
return 0
;;
h)
_switch_profile_help
return 0
;;
d)
_unload_bash_profile
_reset_bash_profile
switch_profile() {
local OPTIND OPTARG SELECTED_PROFILE KEEP_ENV TEMP_PROFILE

KEEP_ENV=0
TEMP_PROFILE=0
while getopts "tkdhl" Option; do
case $Option in
k)
KEEP_ENV=1
;;
t)
TEMP_PROFILE=1
;;
l)
SWITCH_PROFILE_LIST=$(_switch_profile_list)
export SWITCH_PROFILE_LIST
[ -n "$SWITCH_PROFILE_LIST" ] && complete -o nospace -W "$SWITCH_PROFILE_LIST" switch_profile
echo "Available profiles:"
echo -e "${SWITCH_PROFILE_LIST// /\\n}"
return 0
;;
h)
_switch_profile_help
return 0
;;
d)
_unload_bash_profile
_reset_bash_profile
exec bash
;;
*)
_switch_profile_help
return 1
;;
esac
done
shift $((OPTIND - 1))

SELECTED_PROFILE="$1"
if [ -f "${HOME}/${SWITCH_PROFILE_DIRECTORY}/${SELECTED_PROFILE}.load" ]; then {
[ $KEEP_ENV -eq 0 ] && _unload_bash_profile
if [ $TEMP_PROFILE -eq 0 ]; then _save_bash_profile; else export BASH_NEXT_PROFILE="$SELECTED_PROFILE"; fi
exec bash
;;
*)
_switch_profile_help
return 1
;;
esac
done
shift $(($OPTIND - 1))

SELECTED_PROFILE="$1"
if ( [ -f "${HOME}/${SWITCH_PROFILE_DIRECTORY}/${SELECTED_PROFILE}.load" ] ); then {
[ $KEEP_ENV -eq 0 ] && _unload_bash_profile
[ $TEMP_PROFILE -eq 0 ] && _save_bash_profile || export BASH_NEXT_PROFILE="$SELECTED_PROFILE"
exec bash
} else {
_switch_profile_help
echo "Selected profile does not exist."
switch_profile -l
echo ""
return 1
}
fi
}; else
{
_switch_profile_help
echo "Selected profile does not exist."
switch_profile -l
echo ""
return 1
}
fi
}

### MAIN SCRIPT ###
[ -n "$SWITCH_PROFILE_LIST" ] && complete -o nospace -W "$SWITCH_PROFILE_LIST" switch_profile

if ( [ -z ${BASH_NEXT_PROFILE+is_set} ] ); then {
if ( [ -f "$HOME/$SWITCH_PROFILE_SAVED" ] ); then {
source "$HOME/$SWITCH_PROFILE_SAVED"
[ -n "${BASH_CURRENT_PROFILE+is_set}" ] && _load_bash_profile
}
fi
} else {
export BASH_CURRENT_PROFILE="$BASH_NEXT_PROFILE"
unset BASH_NEXT_PROFILE
_load_bash_profile
}
if [ -z ${BASH_NEXT_PROFILE+is_set} ]; then {
if [ -f "$HOME/$SWITCH_PROFILE_SAVED" ]; then
{
# shellcheck source=/dev/null
source "$HOME/$SWITCH_PROFILE_SAVED"
[ -n "${BASH_CURRENT_PROFILE+is_set}" ] && _load_bash_profile
}
fi
}; else
{
export BASH_CURRENT_PROFILE="$BASH_NEXT_PROFILE"
unset BASH_NEXT_PROFILE
_load_bash_profile
}
fi

0 comments on commit f84326e

Please sign in to comment.