-
-
Save colby/5f612eacbfd81e267a8a06a0de588b02 to your computer and use it in GitHub Desktop.
Bumps a git tag and metadata.rb version for a given Chef cookbook, follows SemVer standards.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
semver_regex="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$" | |
usage="\ | |
Usage: | |
$0 | |
$0 show | |
$0 major | |
$0 6.6.6 | |
Commands: | |
show fetch and show current versions. | |
major bump the major level of current version. | |
minor bump the minor level of current version. | |
patch bump the patch level of current version." | |
function errcho { | |
>&2 echo -e "error: $*" | |
exit 1 | |
} | |
function update { | |
git fetch --tags | |
} | |
function show { | |
echo "Current git tag: $current_tag" | |
echo "Current meta version: $current_version" | |
} | |
function validate { | |
local version="$1" | |
if [[ "$version" =~ $semver_regex ]] | |
then | |
apply "$version" | |
else | |
errcho "version $version does not follow semver standard" | |
fi | |
} | |
function bump { | |
local level="$1" | |
IFS=$'.' | |
set -- $current_version | |
local major=$1 | |
local minor=$2 | |
local patch=$3 | |
case $level in | |
major) ((major++));; | |
minor) ((minor++));; | |
patch) ((patch++));; | |
esac | |
validate "$major.$minor.$patch" | |
} | |
function apply { | |
local new_version="$1" | |
$sed -i "s/$current_version/$new_version/" metadata.rb | |
git add metadata.rb | |
git commit -m"bump to $new_version" | |
git tag "$new_version" | |
git push origin --tags | |
} | |
platform=$(uname) | |
if [ "$platform" == 'Darwin' ] | |
then | |
sed=$(which gsed || errcho "install gnu-sed") | |
else | |
sed=$(which sed) | |
fi | |
if [ ! -f metadata.rb ] | |
then | |
errcho "metadata.rb file missing" | |
fi | |
current_version=$(grep -E '^version' metadata.rb | cut -d"'" -f2 || errcho "a cookbook version was not found") | |
current_tag=$(update | git describe --abbrev=0 --tags) | |
if [ -z "$1" ] | |
# there are no args | |
then | |
show | |
read -r -p "enter a new semver version: " answer | |
validate "$answer" | |
# there are args | |
else | |
# first arg matches semver | |
if [[ "$1" =~ $semver_regex ]] | |
then | |
apply "$1" | |
else | |
# first arg is either a known command or throw usage | |
case "$1" in | |
show) show;; | |
major|minor|patch) bump "$1";; | |
*) echo "$usage"; exit 1;; | |
esac | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment