refactor: improve script modularity and logging consistency
All checks were successful
Auto Changelog & Release / release (push) Successful in 6s
All checks were successful
Auto Changelog & Release / release (push) Successful in 6s
- 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.
This commit is contained in:
@@ -18,6 +18,9 @@
|
|||||||
# • Generates the changelog; removes a leading empty line.
|
# • Generates the changelog; removes a leading empty line.
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
source "$(dirname "$0")/../lib/utils.sh"
|
||||||
|
source "$(dirname "$0")/../lib/logging.sh"
|
||||||
|
|
||||||
# ───────────────────────────────────────────────
|
# ───────────────────────────────────────────────
|
||||||
CONFIG_FILE=""
|
CONFIG_FILE=""
|
||||||
TAG=""
|
TAG=""
|
||||||
@@ -40,8 +43,8 @@ while [[ $# -gt 0 ]]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# 2 Basic validation
|
# 2 Basic validation
|
||||||
[[ -z "$CONFIG_FILE" || -z "$TAG" ]] && { echo "❌ --config and --tag are required"; 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" ]] && { echo "❌ --out is required unless --debug is set"; 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; }
|
[[ ! -r "$CONFIG_FILE" ]] && { log_error "Config file not found or unreadable: $CONFIG_FILE"; exit 1; }
|
||||||
if [[ "$DEBUG" = false ]]; then
|
if [[ "$DEBUG" = false ]]; then
|
||||||
OUT_DIR=$(dirname "$OUT_FILE")
|
OUT_DIR=$(dirname "$OUT_FILE")
|
||||||
|
@@ -14,6 +14,9 @@
|
|||||||
# • If a specific version was requested, the installed version is compared; identical → exit, different → upgrade.
|
# • If a specific version was requested, the installed version is compared; identical → exit, different → upgrade.
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
source "$(dirname "$0")/../lib/utils.sh"
|
||||||
|
source "$(dirname "$0")/../lib/logging.sh"
|
||||||
|
|
||||||
REPO="orhun/git-cliff"
|
REPO="orhun/git-cliff"
|
||||||
|
|
||||||
# ───────────────────────────────────────────────
|
# ───────────────────────────────────────────────
|
||||||
@@ -64,10 +67,10 @@ while [[ $# -gt 0 ]]; do
|
|||||||
-h|--help) show_help; exit 0 ;;
|
-h|--help) show_help; exit 0 ;;
|
||||||
-n|--dry-run) DRY_RUN=true; shift ;;
|
-n|--dry-run) DRY_RUN=true; shift ;;
|
||||||
-a|--arch)
|
-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 ;;
|
ARCH_OS_INPUT="$2"; shift 2 ;;
|
||||||
-d|--dir)
|
-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 ;;
|
INSTALL_DIR="$2"; shift 2 ;;
|
||||||
*) POSITIONAL+=("$1"); shift ;;
|
*) POSITIONAL+=("$1"); shift ;;
|
||||||
esac
|
esac
|
||||||
@@ -90,22 +93,22 @@ if command -v git-cliff >/dev/null; then
|
|||||||
INSTALLED_PATH=$(command -v git-cliff)
|
INSTALLED_PATH=$(command -v git-cliff)
|
||||||
INSTALLED_VER=$(git-cliff -V | awk '{print $2}')
|
INSTALLED_VER=$(git-cliff -V | awk '{print $2}')
|
||||||
if ! $USER_VERSION_SPECIFIED; then
|
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
|
exit 0
|
||||||
else
|
else
|
||||||
# strip leading 'v' for comparison consistency
|
# strip leading 'v' for comparison consistency
|
||||||
REQ_VER="${VERSION#v}"
|
REQ_VER="${VERSION#v}"
|
||||||
if [[ "$INSTALLED_VER" == "$REQ_VER" ]]; then
|
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
|
exit 0
|
||||||
else
|
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
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 4 Check for required tools
|
# 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
|
need curl; need tar; need grep; need sed; need awk; need jq
|
||||||
|
|
||||||
# 5 Determine version → Fetch release JSON
|
# 5 Determine version → Fetch release JSON
|
||||||
@@ -115,18 +118,18 @@ else
|
|||||||
API_URL="https://api.github.com/repos/${REPO}/releases/tags/v${VERSION}"
|
API_URL="https://api.github.com/repos/${REPO}/releases/tags/v${VERSION}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "🔍 Fetching release info ($API_URL)…"
|
log_info "Fetching release info ($API_URL)…"
|
||||||
JSON=$(curl -fsSL "$API_URL") || { echo "❌ Failed to fetch release info"; exit 1; }
|
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" |
|
ASSET_URL=$(jq -r '.assets[]?.browser_download_url' <<< "$JSON" |
|
||||||
grep -E "${ARCH_OS}\.(tar\.(gz|xz)|zip)$" | head -n1)
|
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")
|
ASSET_FILE=$(basename "$ASSET_URL")
|
||||||
echo "📦 Downloading git-cliff ${VERSION} (${ASSET_FILE}) …"
|
log_info "Downloading git-cliff ${VERSION} (${ASSET_FILE}) …"
|
||||||
TMP=$(mktemp -d)
|
TMP=$(mktemp -d)
|
||||||
curl -#L -o "${TMP}/${ASSET_FILE}" "$ASSET_URL"
|
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.gz|*.tgz) tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" ;;
|
||||||
*.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;;
|
*.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;;
|
||||||
*.zip) need unzip; unzip -q "${TMP}/${ASSET_FILE}" -d "$TMP" ;;
|
*.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
|
esac
|
||||||
|
|
||||||
# 7 Locate & (maybe) install binary
|
# 7 Locate & (maybe) install binary
|
||||||
BIN_PATH=$(find "$TMP" -type f -name git-cliff -perm -u+x | head -n1)
|
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
|
if $DRY_RUN; then
|
||||||
echo "🧪 Dry-run: verified git-cliff binary at ${BIN_PATH}"
|
log_info "Dry-run: verified git-cliff binary at ${BIN_PATH}"
|
||||||
echo "ℹ️ Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)"
|
log_info "Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)"
|
||||||
echo "✅ git-cliff $("$BIN_PATH" --version) would be installed successfully"
|
log_info "git-cliff $("$BIN_PATH" --version) would be installed successfully"
|
||||||
else
|
else
|
||||||
sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff"
|
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
|
fi
|
||||||
|
Reference in New Issue
Block a user