Skip to content

Commit

Permalink
Update go.sh
Browse files Browse the repository at this point in the history
Refactor some functions and improve readability.
  • Loading branch information
rpsene committed May 8, 2023
1 parent 630b78a commit f25356f
Showing 1 changed file with 127 additions and 127 deletions.
254 changes: 127 additions & 127 deletions go.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

: '
Copyright (C) 2018, 2022
Copyright (C) 2018, 2023
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
Expand All @@ -24,17 +24,22 @@ limitations under the License.
# Define the list of supported architectures
SUPPORTED_ARCHS=(arm64 amd64 x86_64 ppc64le s390x)

# Downloads the raw content of Go download page
function get_content {
local content
content=$(wget https://golang.org/dl/ -q -O -)
local url="https://golang.org/dl/"

echo "Fetching content from ${url}..."
content=$(wget "${url}" -q -O -)

if [ $? -ne 0 ]; then
echo "Error: Failed to fetch content from ${url}"
return 1
fi

# Returns the raw content
echo "$content"
}

# Filter the raw data collected from Go download page and
# returns an array with all supported versions for a
# given a supported architecture
function get_all_versions () {
if [ $# -eq 0 ]
then
Expand All @@ -45,91 +50,98 @@ function get_all_versions () {
local content
content=$(get_content)

# From the raw content, gets all URLs that contains the desired
# architecture, is Linux, is a tar file and is NOT a beta. Then
# remove the prefix and the suffix to get only the version number
# Extract URLs from the raw content, filtering for desired architecture, Linux, tar.gz files, and non-beta versions
# Remove prefix and suffix to obtain only the version number, sort and remove duplicates
versions=( "$(echo "$content" | grep -Eoi '<a [^>]+>' | \
grep -Eo 'href="[^\"]+"' | grep "$1" | grep linux | grep "tar.gz" | \
awk '!/beta/' | awk '!/rc/' | sed -e "s/^href=//" | tr -d '",' | \
awk '{split($0, array, "/"); print array[3]}' | \
sort -t. -k 1,1n -k 2,2n -k 3,3n | uniq | sed -e "s/^go//" | \
sed -e "s/.linux-$1.tar.gz//")" )
awk '!/beta/ && !/rc/' | sed -e 's/^href="//' -e 's/"$//' | \
awk -F'/' '{print $3}' | sort -t. -k 1,1n -k 2,2n -k 3,3n | \
uniq | sed -e 's/^go//' -e "s/.linux-$1.tar.gz//")" )

# Returns an array with all supported versions
# Return an array with all supported versions
echo "${versions[@]}"
}

# Returns the lastest version of Go available for a given
# architecture
function download_go () {
if [ $# -eq 0 ]
then
echo "The GO version or the architecture is missing."
if [ $# -ne 2 ]; then
echo "Error: Both the GO version and the architecture are required."
return
fi
wget https://dl.google.com/go/go"$1".linux-"$2".tar.gz
tar -C /usr/local -xzf go"$1".linux-"$2".tar.gz
rm -f ./go"$1".linux-"$2".tar.gz

local go_version="$1"
local arch="$2"
local go_file="go${go_version}.linux-${arch}.tar.gz"
local download_url="https://dl.google.com/go/${go_file}"

echo "Downloading Go ${go_version} for ${arch} architecture..."
if wget "${download_url}" &&
tar -C /usr/local -xzf "${go_file}" &&
rm -f "./${go_file}"; then
echo "Go ${go_version} successfully installed."
else
echo "Error: Failed to download and install Go ${go_version} for ${arch} architecture."
return 1
fi
}

# Creates the necessary infrastructure to get a fully functional
# environment for development and building using Go
function create_go_env () {

# go-workspace is a directory hierarchy where you keep your GO code
# go-workspace is a directory hierarchy for your GO code
# src contains Go source files,
# pkg contains package objects, and
# bin contains executable commands
if [ ! -d "$HOME"/go-workspace ]; then
mkdir -p "$HOME"/go-workspace
fi
if [ ! -d "$HOME"/go-workspace/bin ]; then
mkdir -p "$HOME"/go-workspace/bin
fi
if [ ! -d "$HOME"/go-workspace/src ]; then
mkdir -p "$HOME"/go-workspace/src
fi
if [ ! -d "$HOME"/go-workspace/pkg ]; then
mkdir -p "$HOME"/go-workspace/pkg
fi
local workspace_dirs=("bin" "src" "pkg")
local go_workspace="$HOME/go-workspace"

for dir in "${workspace_dirs[@]}"; do
mkdir -p "${go_workspace}/${dir}"
done

PATH=$PATH:/usr/local/go/bin
PATH="/usr/local/go/bin:$PATH"
export PATH
GOPATH=$HOME/go-workspace
GOPATH="$go_workspace"
export GOPATH
PATH=$PATH:$(go env GOPATH)/bin
PATH="$(go env GOPATH)/bin:$PATH"
export PATH
GOPATH=$(go env GOPATH)
export GOPATH

echo "Go environment successfully set up:"
go env
}


function download_go_dep () {
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
echo "Downloading Go Dep..."
if curl -fsSL https://raw.githubusercontent.com/golang/dep/master/install.sh | sh; then
echo "Go Dep successfully installed."
else
echo "Error: Failed to download and install Go Dep."
return 1
fi
}

# Executes a simple GO application to ensure the setup is correctly
# configured
function run_sample () {

mkdir -p "$GOPATH"/src/hello

echo "
# Create the sample hello directory and file
mkdir -p "$GOPATH/src/hello"
cat > "$GOPATH/src/hello/hello.go" <<- EOM
package main
import \"fmt\"
import "fmt"
func main() {
fmt.Printf(\"hello, your GO setup works!\\n\")
}" >> "$GOPATH"/src/hello/hello.go
fmt.Printf("hello, your GO setup works!\n")
}
EOM

cd "$GOPATH"/src/hello || return
# Build and run the sample program
pushd "$GOPATH/src/hello" >/dev/null || return
go build
./hello
cd "$GOPATH"/src || return
rm -rf ./hello

# Clean up the sample directory
popd >/dev/null || return
rm -rf "$GOPATH/src/hello"
}


function help(){
echo "Usage : source $0 [install, install x,y.z, remove, env, versions]"
echo "
Expand All @@ -141,87 +153,75 @@ function help(){
"
}

# The main function which contains the control of the script
# The main function which controls the script
function run () {
# Gets the architecture from where this is script is running
# Get the architecture on which this script is running
ARCH=$(uname -m)

# Replace x86_64 for amd64 because this is how Go named it :)
if [ "$ARCH" = "x86_64" ]
then
ARCH="amd64"
fi
if [ "$ARCH" = "aarch64" ]
then
ARCH="arm64"
fi

# Check wheter or not the platform where the platform where
# script is executed is supported
case "${SUPPORTED_ARCHS[@]}" in
*"$ARCH"*)
echo "Supported Architecture :)";;
*)
echo "ERROR: Supported architectures are:" "${SUPPORTED_ARCHS[@]}"
return
# Replace x86_64 with amd64 and aarch64 with arm64, as used in Go naming conventions
case "$ARCH" in
"x86_64") ARCH="amd64" ;;
"aarch64") ARCH="arm64" ;;
esac

# Check whether it was set at least two parameters
if [ "$#" -lt 1 ]
then
help
return
# Check if the platform on which the script is executed is supported
if ! [[ "${SUPPORTED_ARCHS[*]}" =~ "$ARCH" ]]; then
echo "ERROR: Supported architectures are:" "${SUPPORTED_ARCHS[@]}"
return
else
echo "Supported Architecture :)"
fi

if [ "$1" = "install" ]
then
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
return
fi
# Get all available versions
all=( $(get_all_versions $ARCH) )

# Pick which version to install
if [ -n "$2" ]; then
for v in ${all[@]}; do
[ "$2" == "$v" ] && version2install=$v
done
fi
[ -z "$version2install" ] && version2install=${all[${#all[@]}-1]} # defaults to the latest available
# Download the lastest version
download_go "$version2install" $ARCH
# Create the env for using GO
create_go_env
# Download Go Dep
download_go_dep
# Run Sample
#run_sample
return
elif [ "$1" = "remove" ]
then
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
return
fi
rm -rf /usr/local/go
return
elif [ "$1" = "env" ]
then
# Create the env for using GO
create_go_env
# Run Sample
#run_sample
# Check if at least one parameter is set
if [ "$#" -lt 1 ]; then
help
return
elif [ "$1" = "versions" ]
then
all=( $(get_all_versions $ARCH) )
echo "${all[*]}"
fi

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
return
else
help
return
fi

case "$1" in
"install")
# Get all available versions
all=( $(get_all_versions $ARCH) )

# Pick which version to install
if [ -n "$2" ]; then
for v in "${all[@]}"; do
[ "$2" == "$v" ] && version2install=$v
done
fi
[ -z "$version2install" ] && version2install=${all[${#all[@]}-1]} # defaults to the latest available

# Download the latest version
download_go "$version2install" $ARCH
# Create the env for using GO
create_go_env
# Download Go Dep
download_go_dep
# Run Sample
#run_sample
;;
"remove")
rm -rf /usr/local/go
;;
"env")
# Create the env for using GO
create_go_env
# Run Sample
#run_sample
;;
"versions")
all=( $(get_all_versions $ARCH) )
echo "${all[*]}"
;;
*)
help
;;
esac
}

### Main Execution ###
Expand Down

0 comments on commit f25356f

Please sign in to comment.