Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script_run, script_load and file_lookup #33

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,104 @@ fail() {
echo "$message" >&2
exit 1
}

#######################################
# Gets the directory of the current script.
# Globals:
# BASH_SOURCE
# Arguments:
# None
# Outputs:
# Writes the directory path to stdout.
# Returns:
# 0 (true) if the directory is found, 1 (false) otherwise.
#######################################
script_dir() {
realpath "$(dirname "${BASH_SOURCE[0]}")"
}

#######################################
# Looks up a file in predefined directories.
# Globals:
# PATH
# Arguments:
# filename (string): The name of the file to look up.
# Outputs:
# Writes the file path to stdout if found.
# Returns:
# 0 (true) if the file is found, 1 (false) otherwise.
#######################################
file_lookup() {
local filename="$1"
local search_dirs=(
"$(script_dir)"
"$PWD"
)

# Add directories from PATH to search_dirs
for dir in ${PATH//:/ }; do
search_dirs+=("$dir")
done

for dir in "${search_dirs[@]}"; do
local filepath="$dir/$filename"
if [ -f "$filepath" ]; then
echo "$filepath"
return 0
fi
done

return 1
}

#######################################
# Runs a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to run.
# ... (any): Additional arguments to pass to the script.
# Outputs:
# Writes the script output to stdout.
# Returns:
# 0 (true) if the script runs successfully, 1 (false) otherwise.
#######################################
script_run() {
local filename="$1"
local output status
if filepath=$(file_lookup "$filename"); then
shift
output="$("$filepath" "$@" 2>&1)"
status=$?

# make output and status available to other scripts
export RUN_OUTPUT="$output"
export RUN_STATUS="$status"

echo "$output"
return $status
else
return 1
fi
}

#######################################
# Loads a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to load.
# Outputs:
# None
# Returns:
# 0 (true) if the script is loaded successfully, 1 (false) otherwise.
#######################################
script_load() {
local filename="$1"
if filepath=$(file_lookup "$filename"); then
# shellcheck disable=SC1090
source "$filepath"
else
return 1
fi
}
101 changes: 101 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,104 @@ fail() {
echo "$message" >&2
exit 1
}

#######################################
# Gets the directory of the current script.
# Globals:
# BASH_SOURCE
# Arguments:
# None
# Outputs:
# Writes the directory path to stdout.
# Returns:
# 0 (true) if the directory is found, 1 (false) otherwise.
#######################################
script_dir() {
realpath "$(dirname "${BASH_SOURCE[0]}")"
}

#######################################
# Looks up a file in predefined directories.
# Globals:
# PATH
# Arguments:
# filename (string): The name of the file to look up.
# Outputs:
# Writes the file path to stdout if found.
# Returns:
# 0 (true) if the file is found, 1 (false) otherwise.
#######################################
file_lookup() {
local filename="$1"
local search_dirs=(
"$(script_dir)"
"$PWD"
)

# Add directories from PATH to search_dirs
for dir in ${PATH//:/ }; do
search_dirs+=("$dir")
done

for dir in "${search_dirs[@]}"; do
local filepath="$dir/$filename"
if [ -f "$filepath" ]; then
echo "$filepath"
return 0
fi
done

return 1
}

#######################################
# Runs a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to run.
# ... (any): Additional arguments to pass to the script.
# Outputs:
# Writes the script output to stdout.
# Returns:
# 0 (true) if the script runs successfully, 1 (false) otherwise.
#######################################
script_run() {
local filename="$1"
local output status
if filepath=$(file_lookup "$filename"); then
shift
output="$("$filepath" "$@" 2>&1)"
status=$?

# make output and status available to other scripts
export RUN_OUTPUT="$output"
export RUN_STATUS="$status"

echo "$output"
return $status
else
return 1
fi
}

#######################################
# Loads a script if found in predefined directories.
# Globals:
# None
# Arguments:
# filename (string): The name of the script to load.
# Outputs:
# None
# Returns:
# 0 (true) if the script is loaded successfully, 1 (false) otherwise.
#######################################
script_load() {
local filename="$1"
if filepath=$(file_lookup "$filename"); then
# shellcheck disable=SC1090
source "$filepath"
else
return 1
fi
}
34 changes: 34 additions & 0 deletions tests/helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ setup() {
assert_failure
assert_output ""
}

# Test: script_dir should return the directory of the script
@test "script_dir returns the directory of the script" {
run script_dir
assert_success
assert_line --partial "utils-sh"
}

# Test: file_lookup should return 0 if the command exists
@test "file_lookup returns 0 if the command exists" {
run file_lookup "bats"
assert_success
}

# Test: file_lookup should return 1 if the command does not exist
@test "file_lookup returns 1 if the command does not exist" {
run file_lookup "nonexistentcommand"
assert_failure
}

# Test: script_run should execute a command and return its output
@test "script_run executes a command and returns its output" {
run script_run echo "Hello"
assert_success
assert_output "Hello"
}

# Test: script_load should source a file and make its functions available
@test "script_load sources a file and makes its functions available" {
run script_load "../src/helpers.sh"
assert_success
run is_set "Hello"
assert_success
}
Loading