dx/dxkit/runtime/runtime.sh

89 lines
No EOL
2.5 KiB
Bash

#!/usr/bin/env bash
# dxkit/runtime/runtime.sh
#
# Runtime abstraction layer.
# Commands and modules call runtime::* — this file dispatches to
# the active driver based on RUNTIME_DRIVER (default: docker).
# ============================================
# Driver loader
# ============================================
RUNTIME_DRIVER="${RUNTIME_DRIVER:-docker}"
function runtime::driver() { echo "${RUNTIME_DRIVER}"; }
function runtime::service() { echo "${APP_SERVICE:-app}"; }
function runtime::_load_driver() {
local driver_file
driver_file="$(ctx::dxkit)/runtime/${RUNTIME_DRIVER}.runtime.sh"
if [[ ! -f "$driver_file" ]]; then
log::error "Unknown runtime driver: '${RUNTIME_DRIVER}'"
log::error "Expected: ${driver_file}"
return 1
fi
source "$driver_file"
}
runtime::_load_driver
# ============================================
# Interface
# ============================================
function runtime::up() { "runtime::$(runtime::driver)::up" "$@"; }
function runtime::down() { "runtime::$(runtime::driver)::down" "$@"; }
function runtime::logs() { "runtime::$(runtime::driver)::logs" "$@"; }
function runtime::list() { "runtime::$(runtime::driver)::list" "$@"; }
function runtime::status() { "runtime::$(runtime::driver)::status" "$@"; }
function runtime::build() { "runtime::$(runtime::driver)::build" "$@"; }
function runtime::ensure() { "runtime::$(runtime::driver)::ensure" "$@"; }
function runtime::is_inside() { "runtime::$(runtime::driver)::is_inside" "$@"; }
# Exec and shell are special — they handle "running inside the runtime"
# vs "running on host" transparently
function runtime::exec() {
local args=("$@")
[[ "${#args[@]}" -eq 0 ]] && return 1
if runtime::is_inside; then
"${args[@]}"
else
runtime::ensure
"runtime::$(runtime::driver)::exec" "${args[@]}"
fi
}
function runtime::shell() {
if runtime::is_inside; then
runtime::_local_shell
else
runtime::ensure
"runtime::$(runtime::driver)::shell"
fi
}
# Conditionally run a function based on a flag value.
# Driver-agnostic — useful in build pipelines.
function runtime::run_if() {
local flag="$1"
shift
if [[ "${!flag:-}" == true ]]; then
"$@" || return 1
fi
}
# ============================================
# Internal helpers
# ============================================
function runtime::_local_shell() {
if command -v bash >/dev/null 2>&1; then
bash
else
sh
fi
}