Script repair

This commit is contained in:
2025-12-18 17:09:59 -06:00
parent 65729844ce
commit 174a823be4
4 changed files with 477 additions and 105 deletions

View File

@@ -18,16 +18,14 @@
#
# You should have received a copy of the GNU General Public License (./LICENSE.md).
# -----------------------------------------------------------------------------
# FILE INFORMATION
# DEFGROUP: MokoStandards
# INGROUP: Generic.Script
# REPO: https://github.com/mokoconsulting-tech/MokoDefaults
# PATH: /scripts/update_changelog.sh
# VERSION: 01.00.00
# BRIEF: Insert a versioned CHANGELOG.md entry immediately after the main Changelog heading.
# NOTES
# # Purpose:
# BRIEF: Insert a versioned CHANGELOG.md entry immediately after the main Changelog heading
# Purpose:
# - Apply the MokoWaaS-Brand CHANGELOG template entry for a given version.
# - Insert a new header at the top of CHANGELOG.md, immediately after "# Changelog".
# - Avoid duplicates if an entry for the version already exists.
@@ -45,80 +43,80 @@ set -euo pipefail
CHANGELOG_FILE="CHANGELOG.md"
die() {
echo "ERROR: $*" 1>&2
exit 1
echo "ERROR: $*" 1>&2
exit 1
}
info() {
echo "INFO: $*"
echo "INFO: $*"
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
validate_version() {
local v="$1"
[[ "$v" =~ ^[0-9]{2}\.[0-9]{2}\.[0-9]{2}$ ]] || die "Invalid version '$v'. Expected NN.NN.NN (example 03.01.00)."
local v="$1"
[[ "$v" =~ ^[0-9]{2}\.[0-9]{2}\.[0-9]{2}$ ]] || die "Invalid version '$v'. Expected NN.NN.NN (example 03.01.00)."
}
main() {
require_cmd awk
require_cmd grep
require_cmd mktemp
require_cmd date
require_cmd awk
require_cmd grep
require_cmd mktemp
require_cmd date
[[ $# -eq 1 ]] || die "Usage: $0 <VERSION>"
local version="$1"
validate_version "$version"
[[ $# -eq 1 ]] || die "Usage: $0 <VERSION>"
local version="$1"
validate_version "$version"
[[ -f "$CHANGELOG_FILE" ]] || die "Missing $CHANGELOG_FILE in repo root."
[[ -f "$CHANGELOG_FILE" ]] || die "Missing $CHANGELOG_FILE in repo root."
if ! grep -qE '^# Changelog[[:space:]]*$' "$CHANGELOG_FILE"; then
die "$CHANGELOG_FILE must contain a top level heading exactly: # Changelog"
fi
if ! grep -qE '^# Changelog[[:space:]]*$' "$CHANGELOG_FILE"; then
die "$CHANGELOG_FILE must contain a top level heading exactly: # Changelog"
fi
if grep -qE "^## \[$version\][[:space:]]" "$CHANGELOG_FILE"; then
info "CHANGELOG.md already contains an entry for version $version. No action taken."
exit 0
fi
if grep -qE "^## \[$version\][[:space:]]" "$CHANGELOG_FILE"; then
info "CHANGELOG.md already contains an entry for version $version. No action taken."
exit 0
fi
local stamp
stamp="$(date '+%Y-%m-%d')"
local stamp
stamp="$(date '+%Y-%m-%d')"
local tmp
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
local tmp
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
awk -v v="$version" -v d="$stamp" '
BEGIN { inserted=0 }
{
awk -v v="$version" -v d="$stamp" '
BEGIN { inserted=0 }
{
print $0
if (inserted==0 && $0 ~ /^# Changelog[[:space:]]*$/) {
print ""
print "## [" v "] " d
print "- Version bump."
print ""
inserted=1
print ""
print "## [" v "] " d
print "- Version bump."
print ""
inserted=1
}
}
END {
}
END {
if (inserted==0) {
exit 3
exit 3
}
}
' "$CHANGELOG_FILE" > "$tmp" || {
rc=$?
if [[ $rc -eq 3 ]]; then
}
' "$CHANGELOG_FILE" > "$tmp" || {
rc=$?
if [[ $rc -eq 3 ]]; then
die "Insertion point not found. Expected: # Changelog"
fi
die "Failed to update $CHANGELOG_FILE (awk exit code $rc)."
}
fi
die "Failed to update $CHANGELOG_FILE (awk exit code $rc)."
}
mv "$tmp" "$CHANGELOG_FILE"
trap - EXIT
mv "$tmp" "$CHANGELOG_FILE"
trap - EXIT
info "Inserted CHANGELOG.md entry for version $version on $stamp."
info "Inserted CHANGELOG.md entry for version $version on $stamp."
}
main "$@"