Last active
January 14, 2025 02:50
-
-
Save nguyenvanduocit/9fb6a565e38db354914cfef36efffff6 to your computer and use it in GitHub Desktop.
Auto install/upgrade golang
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 | |
| set -e | |
| # Function to detect OS and architecture | |
| detect_platform() { | |
| OS="$(uname -s)" | |
| ARCH="$(uname -m)" | |
| case "$OS" in | |
| "Linux") | |
| OS="linux" | |
| ;; | |
| "Darwin") | |
| OS="darwin" | |
| ;; | |
| "Windows"*) | |
| OS="windows" | |
| ;; | |
| *) | |
| echo "Unsupported operating system: $OS" | |
| exit 1 | |
| ;; | |
| esac | |
| case "$ARCH" in | |
| "x86_64"|"amd64") | |
| ARCH="amd64" | |
| ;; | |
| "aarch64"|"arm64") | |
| ARCH="arm64" | |
| ;; | |
| "armv6l"|"armv7l") | |
| ARCH="armv6l" | |
| ;; | |
| *) | |
| echo "Unsupported architecture: $ARCH" | |
| exit 1 | |
| ;; | |
| esac | |
| echo "$OS $ARCH" | |
| } | |
| # Function to get the latest Go version | |
| get_latest_version() { | |
| local latest_version | |
| if command -v curl >/dev/null 2>&1; then | |
| latest_version=$(curl -sSL https://golang.org/VERSION?m=text | grep -o 'go[0-9.]*' | sed 's/go//') | |
| elif command -v wget >/dev/null 2>&1; then | |
| latest_version=$(wget -qO- https://golang.org/VERSION?m=text | grep -o 'go[0-9.]*' | sed 's/go//') | |
| else | |
| echo "Error: Neither curl nor wget is installed" | |
| exit 1 | |
| fi | |
| echo "$latest_version" | |
| } | |
| # Function to detect current Go environment | |
| detect_current_env() { | |
| if command -v go >/dev/null 2>&1; then | |
| local current_goroot=$(go env GOROOT) | |
| local current_gopath=$(go env GOPATH) | |
| local current_install_dir=$(dirname "$current_goroot") | |
| echo "$current_install_dir" | |
| else | |
| echo "/usr/local" # default install directory | |
| fi | |
| } | |
| # Function to download and install Go | |
| install_go() { | |
| local version=$1 | |
| local os=$2 | |
| local arch=$3 | |
| local ext="tar.gz" | |
| local install_dir=$(detect_current_env) | |
| # Show current Go installation details before removal | |
| if [ -d "${install_dir}/go" ]; then | |
| echo "Current Go installation details:" | |
| "${install_dir}/go/bin/go" version 2>/dev/null || echo "No valid Go installation found" | |
| echo "Installation directory: ${install_dir}/go" | |
| echo "-----------------------------------" | |
| fi | |
| if [ "$os" = "windows" ]; then | |
| ext="zip" | |
| [ "$install_dir" = "/usr/local" ] && install_dir="/c/Program Files" | |
| fi | |
| local filename="go${version}.${os}-${arch}.${ext}" | |
| local download_url="https://dl.google.com/go/${filename}" | |
| echo "Downloading Go ${version} for ${os}/${arch}..." | |
| # Download the file with better error handling | |
| if command -v curl >/dev/null 2>&1; then | |
| if ! curl -fLO "${download_url}"; then | |
| echo "Error: Failed to download Go using curl" | |
| exit 1 | |
| fi | |
| elif command -v wget >/dev/null 2>&1; then | |
| if ! wget "${download_url}"; then | |
| echo "Error: Failed to download Go using wget" | |
| exit 1 | |
| fi | |
| else | |
| echo "Error: Neither curl nor wget is installed" | |
| exit 1 | |
| fi | |
| # Remove existing Go installation if it exists | |
| if [ -d "${install_dir}/go" ]; then | |
| echo "Removing existing Go installation..." | |
| sudo rm -rf "${install_dir}/go" | |
| fi | |
| echo "Installing Go ${version}..." | |
| if [ "$os" = "windows" ]; then | |
| unzip -q "${filename}" -d "${install_dir}" | |
| else | |
| sudo tar -C "${install_dir}" -xzf "${filename}" | |
| fi | |
| # Clean up downloaded file | |
| rm "${filename}" | |
| # Set up environment variables | |
| setup_environment "$os" "${install_dir}/go" | |
| } | |
| # Function to set up environment variables | |
| setup_environment() { | |
| local os=$1 | |
| local go_root=$2 | |
| local shell_rc="" | |
| local fish_config="$HOME/.config/fish/config.fish" | |
| # Detect shell configuration file | |
| if [ -f "$HOME/.zshrc" ]; then | |
| shell_rc="$HOME/.zshrc" | |
| elif [ -f "$HOME/.bashrc" ]; then | |
| shell_rc="$HOME/.bashrc" | |
| elif [ -f "$HOME/.profile" ]; then | |
| shell_rc="$HOME/.profile" | |
| fi | |
| if [ -n "$shell_rc" ]; then | |
| # Remove existing Go paths | |
| sed -i.bak '/export GOROOT/d' "$shell_rc" | |
| sed -i.bak '/export GOPATH/d' "$shell_rc" | |
| sed -i.bak '/export PATH=.*go\/bin/d' "$shell_rc" | |
| # Add new Go paths | |
| echo "export GOROOT=$go_root" >> "$shell_rc" | |
| echo 'export GOPATH=$HOME/go' >> "$shell_rc" | |
| echo 'export PATH=$GOROOT/bin:$GOPATH/bin:$PATH' >> "$shell_rc" | |
| echo "Environment variables have been set in $shell_rc" | |
| echo "Please run 'source $shell_rc' to apply the changes" | |
| fi | |
| # Handle Fish shell configuration | |
| if [ -f "$fish_config" ] || [ -d "$HOME/.config/fish" ]; then | |
| # Create fish config directory if it doesn't exist | |
| mkdir -p "$(dirname "$fish_config")" | |
| # Remove existing Go paths from fish config | |
| if [ -f "$fish_config" ]; then | |
| sed -i.bak '/set -x GOROOT/d' "$fish_config" | |
| sed -i.bak '/set -x GOPATH/d' "$fish_config" | |
| sed -i.bak '/set -x PATH.*go\/bin/d' "$fish_config" | |
| fi | |
| # Add new Go paths for fish | |
| echo "set -x GOROOT $go_root" >> "$fish_config" | |
| echo 'set -x GOPATH $HOME/go' >> "$fish_config" | |
| echo 'set -x PATH $GOROOT/bin $GOPATH/bin $PATH' >> "$fish_config" | |
| echo "Environment variables have been set in $fish_config" | |
| echo "Please run 'source $fish_config' to apply the changes" | |
| fi | |
| if [ -z "$shell_rc" ] && [ ! -f "$fish_config" ]; then | |
| echo "Warning: Could not find shell configuration file" | |
| echo "Please manually set the following environment variables:" | |
| echo "export GOROOT=$go_root" | |
| echo 'export GOPATH=$HOME/go' | |
| echo 'export PATH=$GOROOT/bin:$GOPATH/bin:$PATH' | |
| fi | |
| } | |
| # Main execution | |
| main() { | |
| # Check if running with sudo/admin privileges | |
| if [ "$EUID" -eq 0 ]; then | |
| echo "Please do not run this script with sudo/admin privileges" | |
| exit 1 | |
| fi | |
| echo "Detecting platform..." | |
| read -r OS ARCH <<< "$(detect_platform)" | |
| echo "Detected: $OS/$ARCH" | |
| echo "Fetching latest Go version..." | |
| VERSION=$(get_latest_version) | |
| echo "Latest version: $VERSION" | |
| # Check if Go is already installed | |
| if command -v go >/dev/null 2>&1; then | |
| CURRENT_VERSION=$(go version | awk '{print $3}' | sed 's/go//') | |
| echo "Current Go version: $CURRENT_VERSION" | |
| echo "Current GOROOT: $(go env GOROOT)" | |
| echo "Current GOPATH: $(go env GOPATH)" | |
| if [ "$CURRENT_VERSION" = "$VERSION" ]; then | |
| echo "You already have the latest version of Go installed" | |
| read -p "Do you want to reinstall? [y/N] " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| exit 0 | |
| fi | |
| fi | |
| fi | |
| echo "Debug: download URL would be: https://dl.google.com/go/go${VERSION}.${OS}-${ARCH}.tar.gz" | |
| install_go "$VERSION" "$OS" "$ARCH" | |
| echo "Go $VERSION has been successfully installed!" | |
| echo "To verify the installation, open a new terminal and run: go version" | |
| } | |
| main "$@" |
Author
nguyenvanduocit
commented
Jan 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment