Skip to content

Commit

Permalink
Add validate-semver
Browse files Browse the repository at this point in the history
  • Loading branch information
fabasoad committed Jun 2, 2024
1 parent cb94bc2 commit 14bbaf8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

BIN ?= validate
PREFIX ?= /usr/local
CMDS = dir-exists enum file-exists
CMDS = dir-exists enum file-exists semver

current_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

Expand Down
2 changes: 1 addition & 1 deletion bpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sh-validators",
"version": "v0.1.0",
"description": "A set of scripts to validate inputs in GitHub Actions workflows",
"scripts": ["validate-dir-exists", "validate-enum", "validate-file-exists"],
"scripts": ["validate-dir-exists", "validate-enum", "validate-file-exists", "validate-semver"],
"install": "make install",
"global": "true"
}
20 changes: 20 additions & 0 deletions lib/validate-semver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

validate_semver() {
param_key="$1"
param_val="$2"
semver_regex="^[0-9]\+\(\.[0-9]\+\)\{0,2\}\(-[0-9A-Za-z-]\+\(\.[0-9A-Za-z-]\+\)*\)\?\(\+[0-9A-Za-z-]\+\(\.[0-9A-Za-z-]\+\)*\)\?$"
if ! expr "${param_val}" : "${semver_regex}" >/dev/null; then
msg="\"${param_key}\" parameter is invalid. \"${param_val}\" is not a valid semver."
echo "::error title=Invalid parameter::${msg}"
exit 1
fi
}

## export
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f validate_semver
else
validate_semver "${@}"
exit $?
fi
1 change: 1 addition & 0 deletions validate-semver

0 comments on commit 14bbaf8

Please sign in to comment.