Skip to content

Commit c7e4fc1

Browse files
improved tag script
1 parent 5f9e3b1 commit c7e4fc1

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

scripts/tag.sh

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,45 @@
22

33
set -e
44

5+
DRY_RUN=1
6+
7+
if [ $# -eq 0 ]; then
8+
echo "Error: Tag version is required"
9+
help
10+
fi
11+
12+
TAG=$1
13+
shift
14+
15+
while getopts "t" opt; do
16+
case $opt in
17+
t)
18+
DRY_RUN=0
19+
;;
20+
\?)
21+
echo "Invalid option: -$OPTARG" >&2
22+
exit 1
23+
;;
24+
esac
25+
done
26+
527
help() {
628
cat <<- EOF
7-
Usage: TAG=tag $0
29+
Usage: $0 TAGVERSION [-t]
830
931
Creates git tags for public Go packages.
1032
11-
VARIABLES:
12-
TAG git tag, for example, v1.0.0
33+
ARGUMENTS:
34+
TAGVERSION Tag version to create, for example v1.0.0
35+
36+
OPTIONS:
37+
-t Execute git commands (default: dry run)
1338
EOF
1439
exit 0
1540
}
1641

17-
if [ -z "$TAG" ]
18-
then
19-
printf "TAG env var is required\n\n";
20-
help
42+
if [ "$DRY_RUN" -eq 1 ]; then
43+
echo "Running in dry-run mode"
2144
fi
2245

2346
if ! grep -Fq "\"${TAG#v}\"" version.go
@@ -31,12 +54,21 @@ PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; \
3154
| sed 's/^\.\///' \
3255
| sort)
3356

34-
git tag ${TAG}
35-
git push origin ${TAG}
57+
58+
execute_git_command() {
59+
if [ "$DRY_RUN" -eq 0 ]; then
60+
"$@"
61+
else
62+
echo "DRY-RUN: Would execute: $@"
63+
fi
64+
}
65+
66+
execute_git_command git tag ${TAG}
67+
execute_git_command git push origin ${TAG}
3668

3769
for dir in $PACKAGE_DIRS
3870
do
3971
printf "tagging ${dir}/${TAG}\n"
40-
git tag ${dir}/${TAG}
41-
git push origin ${dir}/${TAG}
72+
execute_git_command git tag ${dir}/${TAG}
73+
execute_git_command git push origin ${dir}/${TAG}
4274
done

0 commit comments

Comments
 (0)