feat: modularize scripts and improve project architecture
All checks were successful
Auto Changelog & Release / release (push) Successful in 8s
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:
54
README.md
54
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
|
```bash
|
||||||
curl -s https://git.0xmax42.io/actions/deb-changelog-action/raw/branch/main/run.sh | bash -s -- \
|
curl -sL https://https://git.0xmax42.io/actions/deb-changelog-action/raw/branch/main/run.sh | bash -s -- \
|
||||||
|
--version v0 \
|
||||||
--tag v2.9.1 \
|
--tag v2.9.1 \
|
||||||
--package_name my-package \
|
--package_name mypkg \
|
||||||
--author_name "John Doe" \
|
--author_name "Max Mustermann" \
|
||||||
--author_email "john@example.com"
|
--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)
|
### Logging
|
||||||
* `--cliff_config path/to/custom.toml`
|
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
29
lib/logging.sh
Normal 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
24
lib/utils.sh
Normal 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
26
run.sh
@@ -26,7 +26,10 @@
|
|||||||
# can reference them (e.g. {{ get_env(name="PACKAGE_NAME") }}).
|
# can reference them (e.g. {{ get_env(name="PACKAGE_NAME") }}).
|
||||||
set -euo pipefail
|
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=""
|
VERSION=""
|
||||||
@@ -34,8 +37,7 @@ TAG=""
|
|||||||
PACKAGE_NAME=""
|
PACKAGE_NAME=""
|
||||||
AUTHOR_NAME=""
|
AUTHOR_NAME=""
|
||||||
AUTHOR_EMAIL=""
|
AUTHOR_EMAIL=""
|
||||||
OUTPUT_FILE="debian/changelog"
|
# CLIFF_CONFIG and OUTPUT_FILE are now sourced from .env
|
||||||
CLIFF_CONFIG=""
|
|
||||||
# ───────────────────────────────────────────────
|
# ───────────────────────────────────────────────
|
||||||
|
|
||||||
show_help() { sed -n '2,25p' "$0"; }
|
show_help() { sed -n '2,25p' "$0"; }
|
||||||
@@ -56,17 +58,17 @@ while [[ $# -gt 0 ]]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# 2 ─ Validate mandatory inputs ────────────────
|
# 2 ─ Validate mandatory inputs ────────────────
|
||||||
[[ -z "$VERSION" ]] && { echo "❌ --version is required"; exit 1; }
|
validate_arg "--version" "$VERSION"
|
||||||
[[ -z "$TAG" ]] && { echo "❌ --tag is required"; exit 1; }
|
validate_arg "--tag" "$TAG"
|
||||||
[[ -z "$PACKAGE_NAME" ]] && { echo "❌ --package_name is required"; exit 1; }
|
validate_arg "--package_name" "$PACKAGE_NAME"
|
||||||
[[ -z "$AUTHOR_NAME" ]] && { echo "❌ --author_name is required"; exit 1; }
|
validate_arg "--author_name" "$AUTHOR_NAME"
|
||||||
[[ -z "$AUTHOR_EMAIL" ]] && { echo "❌ --author_email is required"; exit 1; }
|
validate_arg "--author_email" "$AUTHOR_EMAIL"
|
||||||
|
|
||||||
# 3 ─ Prepare temporary clone ──────────────────
|
# 3 ─ Prepare temporary clone ──────────────────
|
||||||
TMP_DIR=$(mktemp -d)
|
TMP_DIR=$(mktemp -d)
|
||||||
trap 'rm -rf "$TMP_DIR"' EXIT
|
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
|
git clone --depth 1 --branch "$VERSION" "$REPO_URL" "$TMP_DIR" >/dev/null
|
||||||
|
|
||||||
# 4 ─ Install git-cliff via helper script ──────
|
# 4 ─ Install git-cliff via helper script ──────
|
||||||
@@ -76,10 +78,10 @@ bash "$TMP_DIR/scripts/install-git-cliff.sh"
|
|||||||
if [[ -z "$CLIFF_CONFIG" ]]; then
|
if [[ -z "$CLIFF_CONFIG" ]]; then
|
||||||
CLIFF_CONFIG="$TMP_DIR/configs/cliff.debian.toml"
|
CLIFF_CONFIG="$TMP_DIR/configs/cliff.debian.toml"
|
||||||
fi
|
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 ────────
|
# 6 ─ Generate changelog in CURRENT dir ────────
|
||||||
echo "📝 Generating changelog (output → $OUTPUT_FILE)"
|
log_info "Generating changelog (output → $OUTPUT_FILE)"
|
||||||
env \
|
env \
|
||||||
PACKAGE_NAME="$PACKAGE_NAME" \
|
PACKAGE_NAME="$PACKAGE_NAME" \
|
||||||
AUTHOR_NAME="$AUTHOR_NAME" \
|
AUTHOR_NAME="$AUTHOR_NAME" \
|
||||||
@@ -89,4 +91,4 @@ env \
|
|||||||
-t "$TAG" \
|
-t "$TAG" \
|
||||||
-o "$OUTPUT_FILE"
|
-o "$OUTPUT_FILE"
|
||||||
|
|
||||||
echo "✅ Changelog ready: $OUTPUT_FILE"
|
log_info "Changelog ready: $OUTPUT_FILE"
|
||||||
|
@@ -42,22 +42,24 @@ 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" ]] && { 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; }
|
[[ "$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
|
if [[ "$DEBUG" = false ]]; then
|
||||||
OUT_DIR=$(dirname "$OUT_FILE")
|
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
|
fi
|
||||||
|
|
||||||
# 3 Required tools
|
# 3 Required tools
|
||||||
need() { command -v "$1" >/dev/null || { echo "❌ $1 is required but not installed"; exit 1; }; }
|
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
|
need git; need git-cliff; need sed
|
||||||
|
|
||||||
# 4 Generate changelog
|
# 4 Generate changelog
|
||||||
if $DEBUG; then
|
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
|
git cliff --config "$CONFIG_FILE" -t "$TAG" -s all
|
||||||
else
|
else
|
||||||
echo "📝 Generating changelog → $OUT_FILE"
|
log_info "Generating changelog → $OUT_FILE"
|
||||||
git cliff --config "$CONFIG_FILE" -t "$TAG" -s all > "$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
|
fi
|
||||||
|
Reference in New Issue
Block a user