57 lines
1.4 KiB
Bash
57 lines
1.4 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
fetch_and_execute() {
|
|
local script_name=$1
|
|
shift # Shift the arguments so $@ contains the additional arguments to pass
|
|
local token_file=".gitea_token"
|
|
local gitea_user="user"
|
|
local gitea_repo="repo"
|
|
local gitea_domain="http://127.0.0.1:3333"
|
|
local gitea_access_token
|
|
|
|
if [[ -f "$token_file" ]]; then
|
|
gitea_access_token=$(<"$token_file")
|
|
else
|
|
echo "Cannot find the Gitea token file. Ensure it exists at $token_file."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Fetching and executing ${script_name}..."
|
|
|
|
tmp_script="$(mktemp)"
|
|
|
|
if curl -fSL --show-error --header "Authorization: token $gitea_access_token" \
|
|
"${gitea_domain}/api/v1/repos/${gitea_user}/${gitea_repo}/raw/${script_name}" \
|
|
-o "$tmp_script"; then
|
|
|
|
chmod +x "$tmp_script"
|
|
|
|
mkdir -p ./log/homelab
|
|
|
|
logfile="./log/homelab/$(basename "$script_name").log"
|
|
|
|
if "$tmp_script" "$@" 2>&1 | tee "$logfile"; then
|
|
echo "Execution of ${script_name} was successful."
|
|
else
|
|
echo "Failed to execute ${script_name}. Log saved to $logfile"
|
|
rm -f "$tmp_script"
|
|
exit 2
|
|
fi
|
|
else
|
|
echo "Failed to download ${script_name}."
|
|
exit 1
|
|
fi
|
|
|
|
rm -f "$tmp_script"
|
|
}
|
|
|
|
|
|
|
|
# How to use in other scripts:
|
|
|
|
# path_to_fetch_script="fetch-and-execute.sh"
|
|
# source "$path_to_fetch_script"
|
|
|
|
# fetch_and_execute "name_of_script_to_fetch_and_run.sh" arg1 arg2 arg3
|