80 lines
1.6 KiB
Bash
80 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
function build() {
|
|
ROOT=$(dirname "$0")
|
|
NAME="edge-httpdns"
|
|
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
|
|
DIST=$ROOT/"../dist/${NAME}"
|
|
OS=${1}
|
|
ARCH=${2}
|
|
|
|
if [ -z "$OS" ]; then
|
|
echo "usage: build.sh OS ARCH"
|
|
exit 1
|
|
fi
|
|
if [ -z "$ARCH" ]; then
|
|
echo "usage: build.sh OS ARCH"
|
|
exit 1
|
|
fi
|
|
|
|
ZIP_PATH=$(which zip)
|
|
if [ -z "$ZIP_PATH" ]; then
|
|
echo "we need 'zip' command to compress files"
|
|
exit 1
|
|
fi
|
|
|
|
echo "building v${VERSION}/${OS}/${ARCH} ..."
|
|
ZIP="${NAME}-${OS}-${ARCH}-v${VERSION}.zip"
|
|
|
|
rm -rf "$DIST"
|
|
mkdir -p "$DIST"/bin
|
|
mkdir -p "$DIST"/configs
|
|
mkdir -p "$DIST"/logs
|
|
mkdir -p "$DIST"/data
|
|
|
|
cp "$ROOT"/configs/api_httpdns.template.yaml "$DIST"/configs
|
|
|
|
if [ "${OS}" = "linux" ]; then
|
|
env GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=0 \
|
|
go build -trimpath -o "$DIST"/bin/${NAME} -ldflags="-s -w" "$ROOT"/../cmd/edge-httpdns/main.go
|
|
else
|
|
env GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=1 \
|
|
go build -trimpath -o "$DIST"/bin/${NAME} -ldflags="-s -w" "$ROOT"/../cmd/edge-httpdns/main.go
|
|
fi
|
|
|
|
if [ ! -f "$DIST"/bin/${NAME} ]; then
|
|
echo "build failed!"
|
|
exit 1
|
|
fi
|
|
|
|
# delete hidden files
|
|
find "$DIST" -name ".DS_Store" -delete
|
|
find "$DIST" -name ".gitignore" -delete
|
|
|
|
echo "zip files ..."
|
|
cd "${DIST}/../" || exit 1
|
|
if [ -f "${ZIP}" ]; then
|
|
rm -f "${ZIP}"
|
|
fi
|
|
zip -r -X -q "${ZIP}" ${NAME}/
|
|
rm -rf "${NAME}"
|
|
cd - || exit 1
|
|
|
|
echo "OK"
|
|
}
|
|
|
|
function lookup-version() {
|
|
FILE=$1
|
|
VERSION_DATA=$(cat "$FILE")
|
|
re="Version[ ]+=[ ]+\"([0-9.]+)\""
|
|
if [[ $VERSION_DATA =~ $re ]]; then
|
|
echo "${BASH_REMATCH[1]}"
|
|
else
|
|
echo "could not match version"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
build "$1" "$2"
|