sshtunnel/bin/scripts/build-binary.sh
2025-05-23 15:08:44 +02:00

49 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
BINARY_NAME=$1
BINARY_PATH=$2
COMPLETION_SCRIPT=$3
BINARY_PATH_VERSION=$BINARY_PATH.version
source bin/helpers/func.sh
# Check if HEAD is clean, if not abort
if [ -n "$(git status --porcelain)" ]; then
printfe "%s\n" "yellow" "You have uncomitted and/or untracked changes in your working directory."
fi
# Get the current tag checked out to HEAD and hash
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null)
LATEST_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
LATEST_TAG_HASH=$(git rev-list -n 1 --abbrev-commit $LATEST_TAG 2>/dev/null)
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# If BRANCH is HEAD and latest commit hash equals latest tag hash, we are on a tag and up to date
if [ "$BRANCH" == "HEAD" ] && [ "$LATEST_COMMIT_HASH" == "$LATEST_TAG_HASH" ]; then
BRANCH=$LATEST_TAG
fi
# In case the current head has uncomitted and/or untracked changes, append a postfix to the version saying (dirty)
if [ -n "$(git status --porcelain)" ]; then
POSTFIX=" (dirty)"
fi
printfe "%s\n" "cyan" "Building $BINARY_NAME binary for $BRANCH ($LATEST_COMMIT_HASH)$POSTFIX..."
go build -o $BINARY_PATH
if [ $? -ne 0 ]; then
printf "\033[0;31m"
echo "Build failed."
printf "\033[0m"
exit 1
fi
# Put tag and hash in .sshtunnel_version file
echo "$BRANCH ($LATEST_COMMIT_HASH)$POSTFIX" > $BINARY_PATH_VERSION
printfe "%s\n" "cyan" "Generating Bash completion script..."
$BINARY_PATH completion bash > $COMPLETION_SCRIPT
printfe "%s\n" "green" "Bash completion script installed to $COMPLETION_SCRIPT."
printfe "%s\n" "green" "Restart or 'source ~/.bashrc' to update your shell."