From b1dd82f8be256da12ba884cdf6998e357826019d Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 11 Jun 2024 17:11:35 -0400 Subject: [PATCH] update go-glitch build process and install.sh --- build.sh | 77 ++++++++++++++++++------------------------------------ install.sh | 28 +++++++++----------- 2 files changed, 37 insertions(+), 68 deletions(-) diff --git a/build.sh b/build.sh index 55483e1..03907e4 100755 --- a/build.sh +++ b/build.sh @@ -1,70 +1,43 @@ #!/bin/bash -# Default architecture -DEFAULT_ARCH="linux/amd64" +ARCHITECTURES=("linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64") +PROJECT_NAME=$(basename "$(pwd)") -# Supported architectures (adjust as needed) -ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7" "darwin/amd64" "darwin/arm64") - -# Ensure all necessary directories exist and go modules are ready prepare_build() { - # Create necessary directories if they don't exist - mkdir -p dist - mkdir -p build_logs - - # Initialize go modules if go.mod does not exist + mkdir -p dist build_logs if [ ! -f go.mod ]; then - echo "Initializing Go modules" - go mod init go-glitch + go mod init "$PROJECT_NAME" fi - - # Fetch and ensure all dependencies are up to date - echo "Checking dependencies..." go mod tidy } -# Build function build_binary() { - os=$1 - arch=$2 - output_name="go-glitch" - - if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then - output_name="${output_name}_${os}_${arch}" - fi - - output_name="dist/${output_name}" - - # Dynamic Linking - echo "Building dynamically linked for ${os}/${arch} -> ${output_name}" - GOOS=${os} GOARCH=${arch} go build -o ${output_name} main.go 2>build_logs/${os}_${arch}_build.log - if [ $? -eq 0 ]; then - echo "Successfully built ${output_name}" + local os=$1 + local arch=$2 + local output="dist/${PROJECT_NAME}_${os}_${arch}" + env GOOS=$os GOARCH=$arch go build -o $output . &> "build_logs/${os}_${arch}.log" + if [ $? -ne 0 ]; then + echo "Build failed for $os/$arch" >> "build_logs/error.log" else - echo "Failed to build ${output_name}. Check build_logs/${os}_${arch}_build.log for errors." + echo "Build succeeded for $os/$arch" fi - - # Static Linking - if [[ "$os" == "linux" ]]; then # Typically, static linking is most relevant for Linux environments - static_output_name="${output_name}_static" - echo "Building statically linked for ${os}/${arch} -> ${static_output_name}" - CGO_ENABLED=0 GOOS=${os} GOARCH=${arch} go build -a -ldflags '-extldflags "-static"' -o ${static_output_name} main.go 2>build_logs/${os}_${arch}_static_build.log - if [ $? -eq 0 ]; then - echo "Successfully built ${static_output_name}" + if [ "$os" == "linux" ]; then + env GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' -o "${output}_static" . &> "build_logs/${os}_${arch}_static.log" + if [ $? -ne 0 ]; then + echo "Static build failed for $os/$arch" >> "build_logs/error.log" else - echo "Failed to build ${static_output_name}. Check build_logs/${os}_${arch}_static_build.log for errors." + echo "Static build succeeded for $os/$arch" fi fi } -# Main Build Process -prepare_build -for arch in "${ARCHITECTURES[@]}"; do - IFS='/' read -r -a parts <<< "$arch" # Split architecture string - os=${parts[0]} - arch=${parts[1]} - build_binary $os $arch -done - -echo "Build process completed." +main() { + prepare_build + for arch in "${ARCHITECTURES[@]}"; do + IFS="/" read -r os arch <<< "$arch" + build_binary $os $arch + done + echo "Build process completed." +} +main diff --git a/install.sh b/install.sh index 3f67e46..ad161b6 100644 --- a/install.sh +++ b/install.sh @@ -4,12 +4,8 @@ INSTALL_DIR="/usr/local/bin" BINARY_NAME="go-glitch" BASE_URL="https://git.nixc.us/Nixius/go-glitch/raw/branch/main/dist" -declare -A binaries -binaries["linux/amd64"]="go-glitch_linux_amd64" -binaries["linux/arm64"]="go-glitch_linux_arm64" -binaries["linux/arm/v7"]="go-glitch_linux_arm" -binaries["darwin/amd64"]="go-glitch_darwin_amd64" -binaries["darwin/arm64"]="go-glitch_darwin_arm64" +# Supported architectures +ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7" "darwin/amd64" "darwin/arm64") OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" @@ -21,17 +17,17 @@ case $ARCH in *) echo "Unsupported architecture: $ARCH"; exit 1 ;; esac -KEY="${OS}/${ARCH}" - -if [[ -z "${binaries[$KEY]}" ]]; then - echo "No pre-built binary for your system architecture ($KEY)." - exit 1 -fi - -BINARY_URL="${BASE_URL}/${binaries[$KEY]}" +BINARY_URL="${BASE_URL}/oculus_${OS}_${ARCH}" echo "Downloading and installing $BINARY_NAME from $BINARY_URL..." -sudo curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}" -sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}" +# Check if we have write permission to the install directory +if [ -w "${INSTALL_DIR}" ]; then + curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}" + chmod +x "${INSTALL_DIR}/${BINARY_NAME}" +else + sudo curl -sSL "$BINARY_URL" -o "${INSTALL_DIR}/${BINARY_NAME}" + sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}" +fi + echo "Installed $BINARY_NAME to $INSTALL_DIR"