Skip to content

Commit

Permalink
Fix shellcheck errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalabsio committed Jul 8, 2024
1 parent fa6e362 commit d8859c1
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 46 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: CI

on: ['push', 'pull_request']

Expand All @@ -10,6 +10,20 @@ defaults:
working-directory: './'

jobs:
shellcheck:
name: ShellCheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install prerequisites
run: |
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends shellcheck
- name: Run ShellCheck
run: |
shellcheck --version
bin/shellcheck
test-macos:
name: MacOS Test
strategy:
Expand Down
6 changes: 6 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source-path=lib
source-path=tests
source-path=tests/playground

source-path=./tests/*
disable=SC2164,SC2103 # cd dir || exit 1 / cd ..
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: lint
lint:
./bin/shellcheck $(filter-out $@,$(MAKECMDGOALS))

.PHONY: help
help:
@echo 'lint - run linters and code format checkers'
4 changes: 2 additions & 2 deletions _test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ all_tests_passed=true
run_single_test() {

echo "== Running $test_file"
$shell_command $test_file 2>> "$stderr_file" || all_tests_passed=false
$shell_command "$test_file" 2>> "$stderr_file" || all_tests_passed=false
echo ""
}

Expand All @@ -34,7 +34,7 @@ run_all_tests() {

for file in "${_test_files[@]}" ; do
echo "== Running $file"
$shell_command $file 2>> "$stderr_file" || all_tests_passed=false
$shell_command "$file" 2>> "$stderr_file" || all_tests_passed=false

echo ""
done
Expand Down
33 changes: 33 additions & 0 deletions bin/shellcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -e

if [[ $1 == --list ]]; then
list_only_the_targets=true
shift
fi

if [[ -n $1 ]]; then
targets=("$@")
else
targets=()
while IFS= read -r -d $'\0'; do
targets+=("$REPLY")
done < <(
# Find files
# - That are not in vendor or .git directories
# - That are shell scripts or have no extension and are bash executables
find . -type f -not -path './vendor/*' -not -path './.git/*' \( -name '*.sh' -o \
\( -not -name '*.*' -exec sh -c 'head -n 1 "$1" | grep -q "^#!/usr/bin/env bash"' _ {} \; \) \) -print0
)
fi

# List all targets
if [[ $list_only_the_targets ]]; then
printf "%s\n" "${targets[@]}"
exit 0
fi

shellcheck --external-sources --shell=bash "${targets[@]}"

exit $?
2 changes: 1 addition & 1 deletion dev_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ timer() {
shift
fi

echo "$(time ( $@ ) 2>&1 1>/dev/null )"s
time ( "$@" ) > /dev/null
}
11 changes: 5 additions & 6 deletions lib/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env zsh

export SHERPA_ENABLED=true
export SHERPA_LOG_LEVEL='info' # debug, info, no talking

Expand All @@ -11,17 +9,18 @@ fi

SHERPA_PATH="$(dirname "$SHERPA_LIB_PATH")"

# shellcheck disable=SC2034
SHERPA_CHECKSUM_DIR="$HOME/.local/share/local_sherpa"

# Load the dependencies
source "$SHERPA_PATH/vendor/smartcd/arrays"
source "$SHERPA_PATH/vendor/smartcd/varstash"

# Load the app
source $SHERPA_LIB_PATH/logger.sh
source $SHERPA_LIB_PATH/trust_verification.sh
source $SHERPA_LIB_PATH/local_env_file_parser.sh
source $SHERPA_LIB_PATH/setup_cd_hook.sh
source "$SHERPA_LIB_PATH/logger.sh"
source "$SHERPA_LIB_PATH/trust_verification.sh"
source "$SHERPA_LIB_PATH/local_env_file_parser.sh"
source "$SHERPA_LIB_PATH/setup_cd_hook.sh"

source "$SHERPA_LIB_PATH/sherpa.sh"

Expand Down
21 changes: 12 additions & 9 deletions lib/local_env_file_parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,37 @@ _fetch_variable_names() {
local filter_pattern='^[[:space:]]*export[[:space:]]+[[:alnum:]_]+'
# Cleanup:
# export var_1 -> var_1
local variable_names=$(grep -oE "$filter_pattern" .local-sherpa | \
awk '{print $2}')
local variable_names
variable_names=$(grep -oE "$filter_pattern" .local-sherpa | awk '{print $2}')

if [ -n "$variable_names" ]; then
echo "$variable_names"
fi
}

_fetch_aliase_names() {
local filter_pattern='^[[:space:]]*alias[[:space:]]'
local filter_pattern
filter_pattern='^[[:space:]]*alias[[:space:]]'
# Cleanup:
# alias alias_1=... -> alias_1
local alias_names=$(grep -E "$filter_pattern" .local-sherpa | \
awk -F'[ =]' '{print $2}')
local alias_names
alias_names=$(grep -E "$filter_pattern" .local-sherpa | awk -F'[ =]' '{print $2}')

if [ -n "$alias_names" ]; then
echo "$alias_names"
fi
}

_fetch_function_names() {
local filter_pattern='^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)'
local filter_pattern
filter_pattern='^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)'
# Cleanup:
# function_1() -> function_1
# function function_2() -> function_2() -> function_2
local function_names=$(grep -oE "$filter_pattern" .local-sherpa | \
sed 's/function //' | \
sed 's/()//')
local function_names
function_names=$(grep -oE "$filter_pattern" .local-sherpa | \
sed 's/function //' | \
sed 's/()//')

if [ -n "$function_names" ]; then
echo "$function_names"
Expand Down
6 changes: 4 additions & 2 deletions lib/setup_cd_hook.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
setup_cd_hook() {
if [ -n "$ZSH_VERSION" ]; then
# ZSH
function sherpa_chpwd_handler() {
# shellcheck disable=SC2317
sherpa_chpwd_handler() {
# Changed directory?
if [[ -n $OLDPWD && $PWD != $OLDPWD ]]; then
if [[ -n $OLDPWD && $PWD != "$OLDPWD" ]]; then
alert_sherpa_we_changed_dir
fi
}
Expand All @@ -12,6 +13,7 @@ setup_cd_hook() {
add-zsh-hook chpwd sherpa_chpwd_handler
else
# BASH
# shellcheck disable=SC2317
_sherpa_chpwd_hook() {
# run commands in CHPWD_COMMAND variable on dir change
if [[ "$PREVPWD" != "$PWD" ]]; then
Expand Down
7 changes: 5 additions & 2 deletions lib/sherpa.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function sherpa() {
sherpa() {
local command="$1"

local usage_text="Example usage:
Expand All @@ -21,7 +21,7 @@ Tell sherpa how much he should talk (works only for the current session):
e|edit|init) edit; trust_current_env; unload_current_env; load_current_env;;
rest|off|disable) disable;;
work|on|enable) enable;;
talk) shift; set_log_level $1;;
talk) shift; set_log_level "$1";;
debug) set_log_level "debug";;
info) set_log_level "info";;
shh|shhh) set_log_level "no talking";;
Expand Down Expand Up @@ -117,10 +117,12 @@ load_current_env() {

stash_local_env
log_debug "Load local env"
# shellcheck disable=SC1091
source .local-sherpa
# Append the current directory to the list. This is needed to unload the envs
# in the right order when we change directories. The root directory should be
# the last one to unload.
# shellcheck disable=SC2207
PATHS_WHERE_LOCAL_ENV_WAS_LOADED=($(pwd) "${PATHS_WHERE_LOCAL_ENV_WAS_LOADED[@]}")
}

Expand All @@ -136,6 +138,7 @@ was_env_loaded() {

stash_local_env() {
log_debug "Stash local env"
# shellcheck disable=SC2034
varstash_dir="$PWD"

while IFS= read -r env_item_name || [[ -n $env_item_name ]]; do
Expand Down
18 changes: 12 additions & 6 deletions lib/trust_verification.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ _calculate_checksum() {
}

verify_trust() {
local checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"
local checksum_file
checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"

local current_checksum=$(_calculate_checksum)
local current_checksum
current_checksum=$(_calculate_checksum)

# No checksum file?
if [[ ! -f "$checksum_file" ]]; then
log_info "The local env file is not trusted. Run \`sherpa trust\` to mark it as trusted."
return 1
fi

local stored_checksum=$(cat "$checksum_file")
local stored_checksum
stored_checksum=$(cat "$checksum_file")

# Did the local env file change?
if [[ "$current_checksum" != "$stored_checksum" ]]; then
Expand All @@ -35,8 +38,10 @@ trust_current_env() {

mkdir -p "$SHERPA_CHECKSUM_DIR"

local checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"
local current_checksum=$(_calculate_checksum)
local checksum_file
checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"
local current_checksum
current_checksum=$(_calculate_checksum)

echo "$current_checksum" > "$checksum_file"
log_info "Trusted!"
Expand All @@ -45,7 +50,8 @@ trust_current_env() {
}

untrust_current_env() {
local checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"
local checksum_file
checksum_file="$SHERPA_CHECKSUM_DIR/$(pwd | md5sum | cut -d ' ' -f 1)"

if [[ -f "$checksum_file" ]]; then
rm "$checksum_file"
Expand Down
4 changes: 2 additions & 2 deletions test_all
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

_tests_passed=true

./test_bash $@ || _tests_passed=false
./test_bash "$@" || _tests_passed=false
echo ""
echo ""

./test_zsh $@ || _tests_passed=false
./test_zsh "$@" || _tests_passed=false

echo ""

Expand Down
4 changes: 2 additions & 2 deletions test_all_in_ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ _tests_passed=true

echo "**************** Running in Ubuntu ****************"

$_docker_exec ./test_bash $@ || _tests_passed=false
$_docker_exec ./test_bash "$@" || _tests_passed=false
echo ""
echo ""

$_docker_exec ./test_zsh $@ || _tests_passed=false
$_docker_exec ./test_zsh "$@" || _tests_passed=false

echo ""

Expand Down
2 changes: 1 addition & 1 deletion test_bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Disable history to avoid polluting the user's history file
export HISTFILE=

./_test "bash" $@
./_test "bash" "$@"
2 changes: 1 addition & 1 deletion test_zsh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

./_test "zsh" $@
./_test "zsh" "$@"
1 change: 1 addition & 0 deletions tests/features/edit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source tests/support/init.sh
# Setup
cd playground/project_1
sherpa trust
# shellcheck disable=SC2154
is "$var_1" "LOCAL VAR PROJECT 1" "The local env is loaded"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ is "$var_1" "LOCAL VAR SUBPROJECT" "Project 1 env is overridden by Subproject en
is "$(alias_1)" "LOCAL ALIAS SUBPROJECT" "Project 1 env is overridden by Subproject env (alias_1)"
is "$(function_1)" "LOCAL FUNCTION SUBPROJECT" "Project 1 env is overridden by Subproject env (function_1)"

# shellcheck disable=SC2154
is "$subvar_1" "SUBLOCAL VAR SUBPROJECT" "Subproject env is loaded (subvar_1)"
is "$(subalias_1)" "SUBLOCAL ALIAS SUBPROJECT" "Subproject env is loaded (subalias_1)"
is "$(subfunction_1)" "SUBLOCAL FUNCTION SUBPROJECT" "Subproject env is loaded (subfunction_1)"
Expand Down
Loading

0 comments on commit d8859c1

Please sign in to comment.