Newer
Older
openstack-caracal-dc-dc / scripts / prereqs / lib-prereq.sh
# scripts/prereqs/lib-prereq.sh
# Shared helpers for the prereq installers in this directory. SOURCED, never run
# directly. Provides mode parsing (--check/--dry-run), a presence check, an apt
# guard, and a dry-run-aware command runner. ASCII + LF.

pq_have() { command -v "$1" >/dev/null 2>&1; }

# pq_require_apt <toolname> : succeed on Debian/Ubuntu, else fail loud (return 3).
pq_require_apt() {
  pq_have apt-get && return 0
  echo "FAIL: ${1:-this prereq}'s installer supports Debian/Ubuntu (apt-get) only." >&2
  echo "      Install it manually for this OS, then re-run with --check to confirm." >&2
  return 3
}

# pq_parse_mode "$@" : sets PQ_CHECK / PQ_DRYRUN. Returns 10 for --help, 2 on bad arg.
pq_parse_mode() {
  PQ_CHECK=0; PQ_DRYRUN=0
  local a
  for a in "$@"; do
    case "$a" in
      --check)   PQ_CHECK=1 ;;
      --dry-run) PQ_DRYRUN=1 ;;
      -h|--help) return 10 ;;
      *) echo "FAIL: unknown arg '$a' (use: --check | --dry-run)" >&2; return 2 ;;
    esac
  done
  return 0
}

# pq_run <desc> -- <cmd...> : print-and-skip under dry-run, else announce+execute.
pq_run() {
  local desc="$1"; shift
  [ "${1:-}" = "--" ] && shift
  if [ "${PQ_DRYRUN:-0}" = "1" ]; then
    printf '  [dry-run] %s\n            $ %s\n' "$desc" "$*"
    return 0
  fi
  echo "  -> $desc"
  "$@"
}