From bbf9587ee7098730d0976b5df645ebab886a3912 Mon Sep 17 00:00:00 2001 From: "Max P." Date: Wed, 2 Jul 2025 13:02:09 +0200 Subject: [PATCH] refactor: improve script modularity and logging consistency - Updated `scripts/generate-changelog.sh` to use `log_info` and `log_error` for all outputs. - Moved `source` statements for utility files to the top of the script. - Refactored `scripts/install-git-cliff.sh` to replace `echo` with logging functions. - Ensured consistent error handling and logging across all scripts. --- scripts/generate-changelog.sh | 7 +++++-- scripts/install-git-cliff.sh | 37 +++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/scripts/generate-changelog.sh b/scripts/generate-changelog.sh index ffcce83..2314c76 100755 --- a/scripts/generate-changelog.sh +++ b/scripts/generate-changelog.sh @@ -18,6 +18,9 @@ # • Generates the changelog; removes a leading empty line. set -euo pipefail +source "$(dirname "$0")/../lib/utils.sh" +source "$(dirname "$0")/../lib/logging.sh" + # ─────────────────────────────────────────────── CONFIG_FILE="" TAG="" @@ -40,8 +43,8 @@ while [[ $# -gt 0 ]]; do done # 2 Basic validation -[[ -z "$CONFIG_FILE" || -z "$TAG" ]] && { echo "❌ --config and --tag are required"; show_help; exit 1; } -[[ "$DEBUG" = false && -z "$OUT_FILE" ]] && { echo "❌ --out is required unless --debug is set"; show_help; exit 1; } +[[ -z "$CONFIG_FILE" || -z "$TAG" ]] && { log_error "--config and --tag are required"; show_help; exit 1; } +[[ "$DEBUG" = false && -z "$OUT_FILE" ]] && { log_error "--out is required unless --debug is set"; show_help; exit 1; } [[ ! -r "$CONFIG_FILE" ]] && { log_error "Config file not found or unreadable: $CONFIG_FILE"; exit 1; } if [[ "$DEBUG" = false ]]; then OUT_DIR=$(dirname "$OUT_FILE") diff --git a/scripts/install-git-cliff.sh b/scripts/install-git-cliff.sh index 6edf4a9..0e340c2 100755 --- a/scripts/install-git-cliff.sh +++ b/scripts/install-git-cliff.sh @@ -14,6 +14,9 @@ # • If a specific version was requested, the installed version is compared; identical → exit, different → upgrade. set -euo pipefail +source "$(dirname "$0")/../lib/utils.sh" +source "$(dirname "$0")/../lib/logging.sh" + REPO="orhun/git-cliff" # ─────────────────────────────────────────────── @@ -64,10 +67,10 @@ while [[ $# -gt 0 ]]; do -h|--help) show_help; exit 0 ;; -n|--dry-run) DRY_RUN=true; shift ;; -a|--arch) - [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } + [[ $# -lt 2 ]] && { log_error "Option $1 requires an argument"; exit 1; } ARCH_OS_INPUT="$2"; shift 2 ;; -d|--dir) - [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } + [[ $# -lt 2 ]] && { log_error "Option $1 requires an argument"; exit 1; } INSTALL_DIR="$2"; shift 2 ;; *) POSITIONAL+=("$1"); shift ;; esac @@ -90,22 +93,22 @@ if command -v git-cliff >/dev/null; then INSTALLED_PATH=$(command -v git-cliff) INSTALLED_VER=$(git-cliff -V | awk '{print $2}') if ! $USER_VERSION_SPECIFIED; then - echo "✅ git-cliff already present at ${INSTALLED_PATH} (version ${INSTALLED_VER}) – nothing to do." + log_info "git-cliff already present at ${INSTALLED_PATH} (version ${INSTALLED_VER}) – nothing to do." exit 0 else # strip leading 'v' for comparison consistency REQ_VER="${VERSION#v}" if [[ "$INSTALLED_VER" == "$REQ_VER" ]]; then - echo "✅ git-cliff ${INSTALLED_VER} already installed – requested version matches." + log_info "git-cliff ${INSTALLED_VER} already installed – requested version matches." exit 0 else - echo "ℹ️ git-cliff ${INSTALLED_VER} installed, upgrading to ${REQ_VER}..." + log_info "git-cliff ${INSTALLED_VER} installed, upgrading to ${REQ_VER}..." fi fi fi # 4 Check for required tools -need() { command -v "$1" >/dev/null || { echo "$1 is missing"; exit 1; }; } +need() { command -v "$1" >/dev/null || { log_error "$1 is missing"; exit 1; }; } need curl; need tar; need grep; need sed; need awk; need jq # 5 Determine version → Fetch release JSON @@ -115,18 +118,18 @@ else API_URL="https://api.github.com/repos/${REPO}/releases/tags/v${VERSION}" fi -echo "🔍 Fetching release info ($API_URL)…" -JSON=$(curl -fsSL "$API_URL") || { echo "❌ Failed to fetch release info"; exit 1; } +log_info "Fetching release info ($API_URL)…" +JSON=$(curl -fsSL "$API_URL") || { log_error "Failed to fetch release info"; exit 1; } -VERSION=$(jq -r '.tag_name' <<< "$JSON") || { echo "❌ Could not extract version"; exit 1; } +VERSION=$(jq -r '.tag_name' <<< "$JSON") || { log_error "Could not extract version"; exit 1; } ASSET_URL=$(jq -r '.assets[]?.browser_download_url' <<< "$JSON" | grep -E "${ARCH_OS}\.(tar\.(gz|xz)|zip)$" | head -n1) -[[ -z "$ASSET_URL" ]] && { echo "❌ Matching asset not found for architecture ${ARCH_OS}"; exit 1; } +[[ -z "$ASSET_URL" ]] && { log_error "Matching asset not found for architecture ${ARCH_OS}"; exit 1; } ASSET_FILE=$(basename "$ASSET_URL") -echo "📦 Downloading git-cliff ${VERSION} (${ASSET_FILE}) …" +log_info "Downloading git-cliff ${VERSION} (${ASSET_FILE}) …" TMP=$(mktemp -d) curl -#L -o "${TMP}/${ASSET_FILE}" "$ASSET_URL" @@ -135,18 +138,18 @@ case "$ASSET_FILE" in *.tar.gz|*.tgz) tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" ;; *.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;; *.zip) need unzip; unzip -q "${TMP}/${ASSET_FILE}" -d "$TMP" ;; - *) echo "❌ Unknown archive format: $ASSET_FILE"; exit 1 ;; + *) log_error "Unknown archive format: $ASSET_FILE"; exit 1 ;; esac # 7 Locate & (maybe) install binary BIN_PATH=$(find "$TMP" -type f -name git-cliff -perm -u+x | head -n1) -[[ -z "$BIN_PATH" ]] && { echo "❌ Binary not found"; exit 1; } +[[ -z "$BIN_PATH" ]] && { log_error "Binary not found"; exit 1; } if $DRY_RUN; then - echo "🧪 Dry-run: verified git-cliff binary at ${BIN_PATH}" - echo "ℹ️ Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)" - echo "✅ git-cliff $("$BIN_PATH" --version) would be installed successfully" + log_info "Dry-run: verified git-cliff binary at ${BIN_PATH}" + log_info "Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)" + log_info "git-cliff $("$BIN_PATH" --version) would be installed successfully" else sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff" - echo "✅ git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}" + log_info "git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}" fi