Files
michaelschiemer/scripts/install/install-aliases.sh
Michael Schiemer bc7cdf5fed refactor: enhance error reporting and logging, add installer script
- Update `LogReporter` and `Reporter` interface to handle `Throwable` instead of `string`
- Simplify initializer discovery message in `ClassNotInstantiable` exceptions
- Remove unnecessary debug logs in `HttpRouter`
- Add `scripts/install-aliases.sh` for setting up console aliases
- Add minimal `console` script for Docker execution
2025-11-03 14:21:10 +01:00

172 lines
5.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# ────────────────────────────────────────────────────────────────────────────────
# Config (kannst du beim Aufruf per ENV überschreiben)
# PROJECT_CONTAINER: Name des PHP-Containers (Compose-Service). Default: php
# CONSOLE_PATH: Pfad zur console.php im Container. Default: /var/www/html/console.php
# LOCAL_CONSOLE: Pfad zur lokalen console.php. Default: ./console.php
# ────────────────────────────────────────────────────────────────────────────────
PROJECT_CONTAINER="${PROJECT_CONTAINER:-php}"
CONSOLE_PATH="${CONSOLE_PATH:-/var/www/html/console.php}"
LOCAL_CONSOLE="${LOCAL_CONSOLE:-./console.php}"
MARKER_START="# >>> PROJECT CONSOLE ALIASES >>>"
MARKER_END="# <<< PROJECT CONSOLE ALIASES <<<"
detect_shell_rc_file() {
# Bevorzugt Bash-Setup mit ~/.bash_aliases (Debian/WSL-Standard),
# sonst Zsh (~/.zshrc). Fällt zurück auf ~/.bashrc.
if [[ -n "${ZSH_VERSION-}" ]] || [[ "${SHELL:-}" == *"zsh"* ]] || pgrep -fa zsh >/dev/null 2>&1; then
echo "$HOME/.zshrc"
elif [[ -n "${BASH_VERSION-}" ]] || [[ "${SHELL:-}" == *"bash"* ]] || pgrep -fa bash >/dev/null 2>&1; then
# Nutze ~/.bash_aliases falls .bashrc sie lädt, sonst ~/.bashrc
if [[ -f "$HOME/.bashrc" ]] && grep -qs "^\s*if\s\+\[ -f ~/.bash_aliases \]" "$HOME/.bashrc"; then
echo "$HOME/.bash_aliases"
else
echo "$HOME/.bashrc"
fi
else
# Fallback
echo "$HOME/.bashrc"
fi
}
ensure_bashrc_sources_aliases() {
# Stellt sicher, dass ~/.bashrc ~/.bash_aliases lädt (Debian-Standard meistens schon vorhanden)
local bashrc="$HOME/.bashrc"
if [[ ! -f "$bashrc" ]]; then return 0; fi
if grep -qs "~/.bash_aliases" "$bashrc"; then return 0; fi
{
echo
echo "# Load user aliases if present"
echo 'if [ -f ~/.bash_aliases ]; then'
echo ' . ~/.bash_aliases'
echo 'fi'
} >> "$bashrc"
}
install_block() {
local rc_file="$1"
mkdir -p "$(dirname "$rc_file")"
touch "$rc_file"
# Vorhandenen Block entfernen
sed -i "/$MARKER_START/,/$MARKER_END/d" "$rc_file" || true
# Block hinzufügen (Single-quoted EOF, damit Variablen nicht vom Installer expandiert werden)
cat >> "$rc_file" <<'EOF'
# >>> PROJECT CONSOLE ALIASES >>>
# Funktion "console": führt deine PHP-Konsole im Docker-Container *oder* lokal aus.
# - Erkennt "docker compose" vs "docker-compose"
# - Sucht optional ein docker-compose.yml im aktuellen/vererbten Verzeichnisbaum
# - Übergibt alle Argumente transparent weiter
console() {
# Defaults (können per ENV überschrieben werden)
local PROJECT_CONTAINER="${PROJECT_CONTAINER:-php}"
local CONSOLE_PATH="${CONSOLE_PATH:-/var/www/html/console.php}"
local LOCAL_CONSOLE="${LOCAL_CONSOLE:-./console.php}"
# Finde Compose-Befehl
local COMPOSE_BIN="docker compose"
if ! docker compose version >/dev/null 2>&1; then
if command -v docker-compose >/dev/null 2>&1; then
COMPOSE_BIN="docker-compose"
fi
fi
# Suche ein Compose-Projekt nach oben (max 10 Ebenen), damit "console" auch in Subfolders funktioniert
find_compose_dir() {
local d="$PWD"
local i=0
while [[ "$d" != "/" && $i -lt 10 ]]; do
if [[ -f "$d/docker-compose.yml" || -f "$d/compose.yml" ]]; then
echo "$d"
return 0
fi
d="$(dirname "$d")"
i=$((i+1))
done
return 1
}
local compose_dir
if compose_dir="$(find_compose_dir)"; then
# Prüfe, ob der gewünschte Service läuft
local container_id
if container_id=$(
(cd "$compose_dir" && $COMPOSE_BIN ps -q "$PROJECT_CONTAINER" 2>/dev/null) || true
); then
if [[ -n "$container_id" ]]; then
docker exec -it "$container_id" php "$CONSOLE_PATH" "$@"
return $?
fi
fi
fi
# Fallback: Lokal ausführen
if [[ -f "$LOCAL_CONSOLE" ]]; then
php "$LOCAL_CONSOLE" "$@"
else
# Letzter Fallback: direkter Aufruf, falls global im PATH
php console.php "$@"
fi
}
# <<< PROJECT CONSOLE ALIASES <<<
EOF
}
uninstall_block() {
local rc_file="$1"
[[ -f "$rc_file" ]] || return 0
sed -i "/$MARKER_START/,/$MARKER_END/d" "$rc_file" || true
}
usage() {
cat <<USAGE
Installiert eine 'console' Shell-Funktion in dein Shell-RC (Bash/Zsh),
die automatisch entweder im Docker-Container oder lokal deine console.php ausführt.
Verwendung:
$(basename "$0") [--uninstall]
Umgebungsvariablen (optional):
PROJECT_CONTAINER Compose-Service-Name für PHP (Default: php)
CONSOLE_PATH Pfad zur console.php IM Container (Default: /var/www/html/console.php)
LOCAL_CONSOLE Pfad zur lokalen console.php (Default: ./console.php)
USAGE
}
main() {
if [[ "${1-}" == "-h" || "${1-}" == "--help" ]]; then
usage; exit 0
fi
local rc_file
rc_file="$(detect_shell_rc_file)"
if [[ "${1-}" == "--uninstall" ]]; then
uninstall_block "$rc_file"
echo "🗑️ Entfernt: Block aus $rc_file"
echo "➡️ Bitte lade deine Shell neu: source \"$rc_file\" (oder starte ein neues Terminal)"
exit 0
fi
# Bash-spezifisch: sicherstellen, dass ~/.bash_aliases geladen wird
if [[ "$rc_file" == "$HOME/.bash_aliases" ]]; then
ensure_bashrc_sources_aliases
fi
install_block "$rc_file"
echo "✅ Installiert in: $rc_file"
echo " Container-Service: $PROJECT_CONTAINER"
echo " Console im Container: $CONSOLE_PATH"
echo " Console lokal: $LOCAL_CONSOLE"
echo
echo "➡️ Jetzt Shell neu laden: source \"$rc_file\""
echo "➡️ Test: console list oder console migrate --env=staging"
}
main "$@"