Skip to content

Update install-go script #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vitepress/constants/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const constants = Object.freeze({
golangVersion: "1.22.2",
golangVersion: "go1.22.3",

nodeVersion: "21.7.2",
yarnVersion: "1.22.19",
Expand Down
112 changes: 52 additions & 60 deletions public/install-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ if ! command -v jq &> /dev/null; then
curl -sSL https://rollkit.dev/install-jq.sh | bash
fi

version="${1:-$(curl -s 'https://go.dev/dl/?mode=json' | jq -r '.[0].version')}"
current="$(/usr/local/go/bin/go version 2>/dev/null | awk '{print $3}')"
# Define the Go binary path
GO_BIN_PATH="/usr/local/go/bin"
GO_UNTAR_PATH="/usr/local"

version="${1:-$(curl -sSL 'https://go.dev/dl/?mode=json' | jq -r '.[0].version')}"

current="$($GO_BIN_PATH/go version 2>/dev/null | awk '{print $3}')"
if [[ "$current" == "$version" ]]; then
echo "Go is already up-to-date at version ${version}"
exit 0
Expand All @@ -27,16 +32,16 @@ update_go() {
local arch="$1"
local os="$2"

local go_url="https://golang.org/dl/go${version}.${os}-${arch}.tar.gz"
local go_url="https://golang.org/dl/${version}.${os}-${arch}.tar.gz"

echo "Downloading Go from ${go_url}"

curl -so "/tmp/go${version}.${os}-${arch}.tar.gz" -L "$go_url"
curl -so "/tmp/${version}.${os}-${arch}.tar.gz" -L "$go_url"
if [ $? -eq 0 ]; then
tar -C /usr/local -xzf "/tmp/go${version}.${os}-${arch}.tar.gz"
tar -C $GO_UNTAR_PATH -xzf "/tmp/${version}.${os}-${arch}.tar.gz"
if [ $? -ne 0 ]; then
echo "Failed to extract Go. Possibly corrupted download."
rm "/tmp/go${version}.${os}-${arch}.tar.gz"
rm "/tmp/${version}.${os}-${arch}.tar.gz"
exit 1
else
echo "Go updated to version ${version}"
Expand All @@ -46,70 +51,64 @@ update_go() {
exit 1
fi

rm "/tmp/go${version}.${os}-${arch}.tar.gz"
rm "/tmp/${version}.${os}-${arch}.tar.gz"
}

# Function to add path to the specific shell config file
add_path_to_config() {
local config_file="$1"

if ! grep -q "export PATH=.*$GO_BIN_PATH" "$config_file" ; then
echo "export PATH=\"\$PATH:$GO_BIN_PATH\"" >> "$config_file"
echo "Added $GO_BIN_PATH to $config_file"
else
echo "$GO_BIN_PATH is already in $config_file"
fi
}

case "$(uname -s)" in
Linux)
case "$(uname -m)" in
armv6l|armv7l)
update_go "armv6l" "linux"
;;
arm64)
update_go "arm64" "linux"
;;
x86_64)
update_go "amd64" "linux"
;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
OS="linux"
;;
Darwin)
case "$(uname -m)" in
arm64)
update_go "arm64" "darwin"
;;
x86_64)
update_go "amd64" "darwin"
;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
OS="darwin"
;;
*)
echo "Unsupported operating system: $(uname -s)" >&2
exit 1
;;
esac

# Define the Go binary path
GO_BIN_PATH="/usr/local/go/bin"

# Function to add path to the specific shell config file
add_path_to_config() {
local config_file="$1"
# Determine the architecture
case "$(uname -m)" in
armv6l)
ARCH="armv6l"
;;
armv7l)
ARCH="armv7l"
;;
arm64)
ARCH="arm64"
;;
x86_64)
ARCH="amd64"
;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac

# Check if the Go bin path is already in the config file to prevent duplicates
if ! grep -q "export PATH=.*$GO_BIN_PATH" "$config_file" ; then
echo "export PATH=\$PATH:$GO_BIN_PATH" >> "$config_file"
echo "Added $GO_BIN_PATH to $config_file"
else
echo "$GO_BIN_PATH is already in $config_file"
fi
}
update_go "$ARCH" "$OS"

# Determine shell and appropriate config file
if [[ -n "$ZSH_VERSION" ]]; then
# Assuming the user is using Zsh
CONFIG_FILE="$HOME/.zshenv"
elif [[ -n "$BASH_VERSION" ]]; then
# Check if .bash_profile exists, otherwise use .profile
if [[ -f "$HOME/.bash_profile" ]]; then
if [[ -f "$HOME/.bashrc" ]]; then
CONFIG_FILE="$HOME/.bashrc"
elif [[ -f "$HOME/.bash_profile" ]]; then
CONFIG_FILE="$HOME/.bash_profile"
else
CONFIG_FILE="$HOME/.profile"
Expand All @@ -119,15 +118,8 @@ else
exit 1
fi

# Check if the Go bin path is already in the PATH
if [[ ":$PATH:" != *":$GO_BIN_PATH:"* ]]; then
echo "Adding $GO_BIN_PATH to PATH."
add_path_to_config "$CONFIG_FILE"
# Source the config file to update the current session
source "$CONFIG_FILE"
echo "$GO_BIN_PATH added to PATH successfully."
else
echo "$GO_BIN_PATH is already in PATH."
fi
add_path_to_config "$CONFIG_FILE"

$(which go) version

echo "Now run 'source $CONFIG_FILE' to update your environment"
8 changes: 1 addition & 7 deletions public/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ git clone https://github.com/rollkit/rollkit.git

if ! which go > /dev/null; then
echo "Go is not installed. Attempting to install Go..."
if ! which go > /dev/null; then
curl -sL https://rollkit.dev/install-go.sh | sh -s 1.22.2
if ! which go > /dev/null; then
echo "Failed to install Go. Please install it manually and rerun this script."
exit 1
fi
fi
curl -sL "https://rollkit.dev/install-go.sh" | sh -s "go1.22.2"
fi

cd rollkit || { echo "Failed to find the downloaded repository."; exit 1; }
Expand Down
Loading