|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) HashiCorp, Inc. |
| 3 | +# SPDX-License-Identifier: BUSL-1.1 |
| 4 | + |
| 5 | + |
| 6 | +set -uo pipefail |
| 7 | + |
| 8 | +function usage { |
| 9 | + cat <<-'EOF' |
| 10 | +Usage: ./changelog.sh <command> [<options>] |
| 11 | +
|
| 12 | +Description: |
| 13 | + This script will update CHANGELOG.md with the given version and date. |
| 14 | +
|
| 15 | +Commands: |
| 16 | + prepare <version> <date> |
| 17 | + prepare updates the first line in the CHANGELOG.md file with the |
| 18 | + given version and date. |
| 19 | +
|
| 20 | + ./changelog.sh prepare 1.0.0 "November 1, 2021" |
| 21 | +
|
| 22 | + cleanup <released-version> <next-version> |
| 23 | + cleanup prepends a new section to the CHANGELOG.md file with the given |
| 24 | + version and (Unreleased) as the date. If the released version contains a |
| 25 | + pre-release tag, the next version will replace the top line instead of |
| 26 | + inserting a new section. |
| 27 | +EOF |
| 28 | +} |
| 29 | + |
| 30 | +function prepare { |
| 31 | + VERSION="${1:-}" |
| 32 | + DATE="${2:-}" |
| 33 | + |
| 34 | + if [[ -z "$VERSION" || -z "$DATE" ]]; then |
| 35 | + echo "missing at least one of [<version>, <date>] arguments" |
| 36 | + usage |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + |
| 40 | + sed -i '' -e "1s/.*/## $VERSION ($DATE)/" CHANGELOG.md |
| 41 | +} |
| 42 | + |
| 43 | +function cleanup { |
| 44 | + RELEASED_VERSION="${1:-}" |
| 45 | + NEXT_VERSION="${2:-}" |
| 46 | + |
| 47 | + if [[ -z "$RELEASED_VERSION" || -z "$NEXT_VERSION" ]]; then |
| 48 | + echo "missing at least one of [<released-version>, <next-version>] arguments" |
| 49 | + usage |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + |
| 53 | + if [[ "$RELEASED_VERSION" == *-* ]]; then |
| 54 | + # then we have a pre-release version, so we should replace the top line |
| 55 | + sed -i '' -e "1s/.*/## $NEXT_VERSION (Unreleased)/" CHANGELOG.md |
| 56 | + else |
| 57 | + sed -i '' -e "1s/^/## $NEXT_VERSION (Unreleased)\n\n/" CHANGELOG.md |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +function main { |
| 62 | + case "$1" in |
| 63 | + prepare) |
| 64 | + prepare "${@:2}" |
| 65 | + |
| 66 | + ;; |
| 67 | + cleanup) |
| 68 | + cleanup "${@:2}" |
| 69 | + |
| 70 | + ;; |
| 71 | + *) |
| 72 | + usage |
| 73 | + exit 1 |
| 74 | + |
| 75 | + ;; |
| 76 | + esac |
| 77 | +} |
| 78 | + |
| 79 | +main "$@" |
| 80 | +exit $? |
0 commit comments