...
1#!/bin/bash
2
3set -e
4
5DRY_RUN=1
6
7if [ $# -eq 0 ]; then
8 echo "Error: Tag version is required"
9 help
10fi
11
12TAG=$1
13shift
14
15while 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
25done
26
27help() {
28 cat <<- EOF
29Usage: $0 TAGVERSION [-t]
30
31Creates git tags for public Go packages.
32
33ARGUMENTS:
34 TAGVERSION Tag version to create, for example v1.0.0
35
36OPTIONS:
37 -t Execute git commands (default: dry run)
38EOF
39 exit 0
40}
41
42if [ "$DRY_RUN" -eq 1 ]; then
43 echo "Running in dry-run mode"
44fi
45
46if ! grep -Fq "\"${TAG#v}\"" version.go
47then
48 printf "version.go does not contain ${TAG#v}\n"
49 exit 1
50fi
51
52PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; \
53 | grep -E -v "example|internal" \
54 | sed 's/^\.\///' \
55 | sort)
56
57
58execute_git_command() {
59 if [ "$DRY_RUN" -eq 0 ]; then
60 "$@"
61 else
62 echo "DRY-RUN: Would execute: $@"
63 fi
64}
65
66execute_git_command git tag ${TAG}
67execute_git_command git push origin ${TAG}
68
69for dir in $PACKAGE_DIRS
70do
71 printf "tagging ${dir}/${TAG}\n"
72 execute_git_command git tag ${dir}/${TAG}
73 execute_git_command git push origin ${dir}/${TAG}
74done
View as plain text