Skip to content

Commit

Permalink
fix(bash): Fix bash scripts finding relative references (as good as b…
Browse files Browse the repository at this point in the history
…ash can)
  • Loading branch information
stevenj committed Nov 28, 2023
1 parent ad41aea commit dcfffea
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
7 changes: 6 additions & 1 deletion earthly/bash/shellcheck-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ source "$(dirname "$0")/include/colors.sh"
shopt -s globstar
rc=0
for file in "$1"/**/*.sh; do
path=$(dirname "${file}")
filename=$(basename "${file}")
pushd "${path}" >/dev/null || return 1
status "${rc}" "Checking Bash Lint of '${file}'" \
shellcheck --check-sourced --external-sources --color=always --enable=all "${file}"
shellcheck --check-sourced --external-sources --color=always --enable=all "${filename}"
popd >/dev/null || return 1

rc=$?
done

Expand Down
8 changes: 7 additions & 1 deletion earthly/postgresql/scripts/db_ops.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
# This script is not intended to be run by itself, and provides common functions
# for database operations.

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}/
else
basedir=./
fi

echo BASEDIR = "${basedir}"

source "${basedir}/include/colors.sh"
source "${basedir}/include/params.sh"
Expand Down
8 changes: 6 additions & 2 deletions earthly/postgresql/scripts/std_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
# It validates that all the migrations and importable data are able to be
# used without error.

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}
else
basedir=.
fi

source "${basedir}/include/colors.sh"
source "${basedir}/db_ops.sh"
Expand Down Expand Up @@ -46,7 +50,7 @@ do

# Reset schema so all seed data get applied to a clean database.
setup_and_migrate "Reset" "$@"
done < <(find ./seed/* -maxdepth 1 -type d -print0 | sort -z)
done < <(find ./seed/* -maxdepth 1 -type d -print0 | sort -z) || true

# Stop the database
status_and_exit "DB Stop" \
Expand Down
6 changes: 5 additions & 1 deletion earthly/postgresql/scripts/std_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# Individual targets can add extra `check` steps, but these checks must always
# pass.

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}/
else
basedir=./
fi

source "${basedir}/include/colors.sh"

Expand Down
6 changes: 5 additions & 1 deletion earthly/rust/scripts/std_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# Individual targets can add extra `check` steps, but these checks must always
# pass.

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}/
else
basedir=./
fi

source "${basedir}/include/colors.sh"

Expand Down
6 changes: 5 additions & 1 deletion earthly/rust/scripts/std_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# Individual targets can add extra `check` steps, but these checks must always
# pass.

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}/
else
basedir=./
fi

source "${basedir}/include/colors.sh"

Expand Down
6 changes: 5 additions & 1 deletion earthly/rust/scripts/verify_toolchain.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env bash

basedir=$(dirname "$0")
if [[ ${BASH_SOURCE[0]} = */* ]]; then
basedir=${BASH_SOURCE%/*}/
else
basedir=./
fi

source "${basedir}/include/colors.sh"

Expand Down
16 changes: 8 additions & 8 deletions utilities/scripts/bash/params.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ get_param() {
# Access the values of the associative arrays
local env_var_map
local default_map
declare -n env_var_map="$env_var_map_name"
declare -n default_map="$default_map_name"
declare -n env_var_map="${env_var_map_name}"
declare -n default_map="${default_map_name}"
shift 3

# Check if ENV_VAR defined, has priority over options and defaults
local env_var_name="${env_var_map[$param_name]:-}"
if [[ -n "$env_var_name" ]]; then
local env_var_name="${env_var_map[${param_name}]:-}"
if [[ -n "${env_var_name}" ]]; then
local env_var_value="${!env_var_name}"
if [[ -n "$env_var_value" ]]; then
echo "$env_var_value"
if [[ -n "${env_var_value}" ]]; then
echo "${env_var_value}"
return
fi
fi

# Check for the parameter in the named options
for arg in "$@"; do
if [[ "$arg" == "--$param_name="* ]]; then
if [[ "${arg}" == "--${param_name}="* ]]; then
echo "${arg#*=}"
return
fi
done

# Return the default value
local default_value="${default_map[$param_name]:-}"
local default_value="${default_map[${param_name}]:-}"
echo "${default_value:-}"

return
Expand Down

0 comments on commit dcfffea

Please sign in to comment.