114 lines
2.0 KiB
Bash

#!/usr/bin/env bash
if [[ -n "${ATLAS_COMMON_SH_LOADED:-}" ]]; then
return 0
fi
ATLAS_COMMON_SH_LOADED=1
ATLAS_FRAMEWORK_VERSION="2.0.0"
ATLAS_EXIT_OK=0
ATLAS_EXIT_GENERAL=1
ATLAS_EXIT_USAGE=2
ATLAS_EXIT_PREREQ=3
ATLAS_EXIT_VALIDATION=4
ATLAS_EXIT_UNSAFE=5
ATLAS_EXIT_INTERNAL=10
ATLAS_DRY_RUN=0
ATLAS_VERBOSE=0
ATLAS_SCRIPT_NAME="$(basename "${0:-atlas-script}")"
atlas_strict_mode() {
set -Eeuo pipefail
}
atlas_set_script_name() {
ATLAS_SCRIPT_NAME="$1"
}
atlas_enable_dry_run() {
ATLAS_DRY_RUN=1
}
atlas_enable_verbose() {
ATLAS_VERBOSE=1
}
atlas_is_dry_run() {
[[ "${ATLAS_DRY_RUN}" == "1" ]]
}
atlas_is_verbose() {
[[ "${ATLAS_VERBOSE}" == "1" ]]
}
atlas_usage_error() {
local message="$1"
printf 'ERROR: %s\n' "$message" >&2
return 0
}
atlas_require_command() {
local command_name="$1"
if ! command -v "$command_name" >/dev/null 2>&1; then
printf 'ERROR: required command not found: %s\n' "$command_name" >&2
return "${ATLAS_EXIT_PREREQ}"
fi
}
atlas_have_command() {
command -v "$1" >/dev/null 2>&1
}
atlas_require_sudo_noninteractive() {
if [[ "${EUID}" -eq 0 ]]; then
return 0
fi
sudo -n true >/dev/null 2>&1
}
atlas_run() {
if atlas_is_dry_run; then
printf '[DRY-RUN] %s\n' "$*"
return 0
fi
"$@"
}
atlas_sudo_run() {
if [[ "${EUID}" -eq 0 ]]; then
atlas_run "$@"
else
atlas_run sudo -n "$@"
fi
}
atlas_print_version() {
printf '%s %s\n' "${ATLAS_SCRIPT_NAME}" "${ATLAS_FRAMEWORK_VERSION}"
}
atlas_install_error_trap() {
trap 'atlas_on_error $? $LINENO "$BASH_COMMAND"' ERR
}
atlas_install_cleanup_trap() {
trap 'atlas_on_exit' EXIT
}
atlas_on_error() {
local exit_code="$1"
local line_no="$2"
local command_text="$3"
if declare -F atlas_log_error >/dev/null 2>&1; then
atlas_log_error "Command failed at line ${line_no} with exit ${exit_code}: ${command_text}"
else
printf 'ERROR: command failed at line %s with exit %s\n' "$line_no" "$exit_code" >&2
fi
return "$exit_code"
}
atlas_on_exit() {
:
}