Created
August 14, 2024 16:55
-
-
Save powersj/59e42642aa61beb5adc3b361257f1d0b to your computer and use it in GitHub Desktop.
Telegraf Build
This file contains hidden or 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 | |
# Checks out specified release tag and builds the binary | |
# | |
VERBOSITY=0 | |
TEMP_D="" | |
error() { echo "$@" 1>&2; } | |
fail() { [ $# -eq 0 ] || error "$@"; exit 1; } | |
bad_usage() { usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; } | |
cleanup() { | |
[ -z "${TEMP_D}" ] || [ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}" | |
} | |
debug() { | |
local level=${1}; shift; | |
[ "${level}" -gt "${VERBOSITY}" ] && return | |
error "${@}" | |
} | |
usage() { | |
cat <<EOF | |
Usage: ${0##*/} [ options ] <tag[s]> | |
Will checkout a specific git tag of Telegraf and build the project | |
options: | |
-g|--go path to go binary (default: /usr/bin/go) | |
-h|--help this help output | |
-v|--verbose verbose logging | |
EOF | |
} | |
main() { | |
local short_opts="hg:v" | |
local long_opts="help,go:,verbose" | |
local getopt_out="" | |
getopt_out=$(getopt --name "${0##*/}" \ | |
--options "${short_opts}" --long "${long_opts}" -- "$@") || | |
{ bad_usage; return; } | |
eval set -- "${getopt_out}" || | |
{ bad_usage; return; } | |
local go_binary="/usr/bin/go" | |
local cur="" next="" | |
while [ $# -ne 0 ]; do | |
cur="$1"; next="$2"; | |
case "$cur" in | |
-g|--go) go_binary=$next;; | |
-h|--help) usage ; exit 0;; | |
-v|--verbose) VERBOSITY=$((VERBOSITY+1));; | |
--) shift; break;; | |
esac | |
shift; | |
done | |
## check arguments here - how many args do you expect? | |
[ $# -gt 0 ] || { bad_usage "must provide at least one git tag"; return; } | |
DEST_D=$(pwd) | |
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || | |
fail "failed to make tempdir" | |
trap cleanup EXIT | |
git version || fail "git binary not found" | |
$go_binary version || fail "go binary not found" | |
# program starts here | |
git clone https://github.com/influxdata/telegraf.git "$TEMP_D" || fail "git checkout failed" | |
cd "$TEMP_D" || fail "failed to switch to temporary dir" | |
git fetch --all --tags || fail "could not fetch tags" | |
for tag in "$@"; do | |
echo "checking out and building: $tag" | |
git checkout "tags/$tag" -B "$tag" || fail "failed to checkout $tag" | |
git show | |
$go_binary mod vendor | |
time make | |
mv telegraf "$DEST_D/telegraf-$tag" | |
done | |
return 0 | |
} | |
main "$@" | |
# vi: ts=4 noexpandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment