feat: modularize scripts and improve project architecture
All checks were successful
Auto Changelog & Release / release (push) Successful in 8s

- 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.
This commit is contained in:
2025-07-02 12:59:16 +02:00
parent a59359545d
commit ba7b9f2928
5 changed files with 116 additions and 31 deletions

View File

@@ -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.

29
lib/logging.sh Normal file
View File

@@ -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"
}

24
lib/utils.sh Normal file
View File

@@ -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
}

26
run.sh
View File

@@ -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"

View File

@@ -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