From ba7b9f2928ed248f26a3010cec0ff5efc44157ea Mon Sep 17 00:00:00 2001 From: "Max P." Date: Wed, 2 Jul 2025 12:59:16 +0200 Subject: [PATCH] feat: modularize scripts and improve project architecture - Added `lib/utils.sh` and `lib/logging.sh` for reusable utility and logging functions. - Updated `run.sh` and `scripts/generate-changelog.sh` to use the new utility and logging functions (`validate_arg`, `log_info`, `log_error`). - Add `config.env` for better clarity and updated all references in the project. - Enhanced documentation in `README.md` to reflect the new configuration file and usage instructions. - Improved error handling and logging consistency across scripts. --- README.md | 56 ++++++++++++++++++++++++++--------- lib/logging.sh | 29 ++++++++++++++++++ lib/utils.sh | 24 +++++++++++++++ run.sh | 26 ++++++++-------- scripts/generate-changelog.sh | 12 ++++---- 5 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 lib/logging.sh create mode 100644 lib/utils.sh diff --git a/README.md b/README.md index d258507..c8cba6d 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,52 @@ -# Debian Changelog Generator +# Auto Debian Package Changelog Generation -## External Usage +This project provides a streamlined solution for generating Debian-compatible changelogs using `git-cliff`. -This repository provides a one-liner to generate a Debian-style changelog using `git-cliff`. +## Features +- Modularized scripts for better maintainability. +- Centralized configuration via `.env` file. +- Enhanced logging with multiple log levels. +- Support for custom `git-cliff` configurations. -## Quick Usage +## Usage -You can run the generator directly from the URL: +### Prerequisites +Ensure the following tools are installed: +- `git` +- `git-cliff` +- `sed` + +### Running the Script Locally +To generate a changelog locally, use the `run.sh` script: ```bash -curl -s https://git.0xmax42.io/actions/deb-changelog-action/raw/branch/main/run.sh | bash -s -- \ - --tag v2.9.1 \ - --package_name my-package \ - --author_name "John Doe" \ - --author_email "john@example.com" +curl -sL https://https://git.0xmax42.io/actions/deb-changelog-action/raw/branch/main/run.sh | bash -s -- \ + --version v0 \ + --tag v2.9.1 \ + --package_name mypkg \ + --author_name "Max Mustermann" \ + --author_email max@example.com \ + [--output_file debian/changelog] \ + [--cliff_config path/to/cliff.toml] ``` -Optional arguments: +### Configuration +The `config.env` file contains default configurations: +- `REPO_URL`: Repository URL for cloning. +- `OUTPUT_FILE`: Default output file for the changelog. +- `CLIFF_CONFIG`: Default configuration file for `git-cliff`. -* `--output_file debian/changelog` (default) -* `--cliff_config path/to/custom.toml` +### Logging +Logs are categorized into three levels: +- `INFO`: General information. +- `WARN`: Warnings about potential issues. +- `ERROR`: Critical errors that require attention. -> ⚠️ This will clone the action to a temporary directory, install `git-cliff`, and write the changelog into your current working directory. +### Testing +Unit tests for the scripts can be added using `bats`. + +## Contributing +Feel free to submit issues or pull requests to improve the project. + +## License +This project is licensed under the MIT License. diff --git a/lib/logging.sh b/lib/logging.sh new file mode 100644 index 0000000..5ce0ff4 --- /dev/null +++ b/lib/logging.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# logging.sh - Logging functions for the project + +# Log levels +LOG_LEVEL_INFO="INFO" +LOG_LEVEL_WARN="WARN" +LOG_LEVEL_ERROR="ERROR" + +# Function to log a message with a specific level +log_message() { + local level="$1" + local message="$2" + local timestamp + timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$timestamp] [$level] $message" +} + +# Convenience functions for specific log levels +log_info() { + log_message "$LOG_LEVEL_INFO" "$1" +} + +log_warn() { + log_message "$LOG_LEVEL_WARN" "$1" +} + +log_error() { + log_message "$LOG_LEVEL_ERROR" "$1" +} diff --git a/lib/utils.sh b/lib/utils.sh new file mode 100644 index 0000000..8184ea9 --- /dev/null +++ b/lib/utils.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# utils.sh - Utility functions for the project + +# Function to check if a required command is available +need() { + command -v "$1" >/dev/null || { echo "❌ $1 is required but not installed"; exit 1; } +} + +# Function to log messages with levels +log() { + local level="$1" + local message="$2" + echo "[$level] $message" +} + +# Function to validate input arguments +validate_arg() { + local arg_name="$1" + local arg_value="$2" + if [[ -z "$arg_value" ]]; then + log "ERROR" "Missing required argument: $arg_name" + exit 1 + fi +} diff --git a/run.sh b/run.sh index 374d2d7..3cc69da 100755 --- a/run.sh +++ b/run.sh @@ -26,7 +26,10 @@ # can reference them (e.g. {{ get_env(name="PACKAGE_NAME") }}). set -euo pipefail -REPO_URL="https://git.0xmax42.io/actions/deb-changelog-action" +source "$(dirname "$0")/lib/utils.sh" +source "$(dirname "$0")/lib/logging.sh" + +source "$(dirname "$0")/config.env" # ─────────────────────────────────────────────── VERSION="" @@ -34,8 +37,7 @@ TAG="" PACKAGE_NAME="" AUTHOR_NAME="" AUTHOR_EMAIL="" -OUTPUT_FILE="debian/changelog" -CLIFF_CONFIG="" +# CLIFF_CONFIG and OUTPUT_FILE are now sourced from .env # ─────────────────────────────────────────────── show_help() { sed -n '2,25p' "$0"; } @@ -56,17 +58,17 @@ while [[ $# -gt 0 ]]; do done # 2 ─ Validate mandatory inputs ──────────────── -[[ -z "$VERSION" ]] && { echo "❌ --version is required"; exit 1; } -[[ -z "$TAG" ]] && { echo "❌ --tag is required"; exit 1; } -[[ -z "$PACKAGE_NAME" ]] && { echo "❌ --package_name is required"; exit 1; } -[[ -z "$AUTHOR_NAME" ]] && { echo "❌ --author_name is required"; exit 1; } -[[ -z "$AUTHOR_EMAIL" ]] && { echo "❌ --author_email is required"; exit 1; } +validate_arg "--version" "$VERSION" +validate_arg "--tag" "$TAG" +validate_arg "--package_name" "$PACKAGE_NAME" +validate_arg "--author_name" "$AUTHOR_NAME" +validate_arg "--author_email" "$AUTHOR_EMAIL" # 3 ─ Prepare temporary clone ────────────────── TMP_DIR=$(mktemp -d) trap 'rm -rf "$TMP_DIR"' EXIT -echo "📥 Cloning ${REPO_URL}@${VERSION} → $TMP_DIR" +log_info "Cloning ${REPO_URL}@${VERSION} → $TMP_DIR" git clone --depth 1 --branch "$VERSION" "$REPO_URL" "$TMP_DIR" >/dev/null # 4 ─ Install git-cliff via helper script ────── @@ -76,10 +78,10 @@ bash "$TMP_DIR/scripts/install-git-cliff.sh" if [[ -z "$CLIFF_CONFIG" ]]; then CLIFF_CONFIG="$TMP_DIR/configs/cliff.debian.toml" fi -[[ ! -r "$CLIFF_CONFIG" ]] && { echo "❌ Config not found: $CLIFF_CONFIG"; exit 1; } +[[ ! -r "$CLIFF_CONFIG" ]] && { log_error "Config not found: $CLIFF_CONFIG"; exit 1; } # 6 ─ Generate changelog in CURRENT dir ──────── -echo "📝 Generating changelog (output → $OUTPUT_FILE)" +log_info "Generating changelog (output → $OUTPUT_FILE)" env \ PACKAGE_NAME="$PACKAGE_NAME" \ AUTHOR_NAME="$AUTHOR_NAME" \ @@ -89,4 +91,4 @@ env \ -t "$TAG" \ -o "$OUTPUT_FILE" -echo "✅ Changelog ready: $OUTPUT_FILE" +log_info "Changelog ready: $OUTPUT_FILE" diff --git a/scripts/generate-changelog.sh b/scripts/generate-changelog.sh index 5ba3794..ffcce83 100755 --- a/scripts/generate-changelog.sh +++ b/scripts/generate-changelog.sh @@ -42,22 +42,24 @@ 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; } -[[ ! -r "$CONFIG_FILE" ]] && { echo "❌ 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 OUT_DIR=$(dirname "$OUT_FILE") - [[ ! -d "$OUT_DIR" ]] && { echo "❌ Output directory does not exist: $OUT_DIR"; exit 1; } + [[ ! -d "$OUT_DIR" ]] && { log_error "Output directory does not exist: $OUT_DIR"; exit 1; } fi # 3 Required tools need() { command -v "$1" >/dev/null || { echo "❌ $1 is required but not installed"; exit 1; }; } +source "$(dirname "$0")/../lib/utils.sh" +source "$(dirname "$0")/../lib/logging.sh" need git; need git-cliff; need sed # 4 Generate changelog if $DEBUG; then - echo "📝 Generating changelog (debug mode – stdout only)" + log_info "Generating changelog (debug mode – stdout only)" git cliff --config "$CONFIG_FILE" -t "$TAG" -s all else - echo "📝 Generating changelog → $OUT_FILE" + log_info "Generating changelog → $OUT_FILE" git cliff --config "$CONFIG_FILE" -t "$TAG" -s all > "$OUT_FILE" - echo "✅ Changelog written to $OUT_FILE" + log_info "Changelog written to $OUT_FILE" fi