Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active September 21, 2024 05:09
Show Gist options
  • Save dayne/63c53402f0045d2c856ac9400bd303bc to your computer and use it in GitHub Desktop.
Save dayne/63c53402f0045d2c856ac9400bd303bc to your computer and use it in GitHub Desktop.
go-lang bash.d

Tool to check for go, and help install it if needed, uses .bash.d style.

Tasks

A set of tasks to test this gist using xc

Setup

cd $HOME

if [ ! -d gists ]; then 
    mkdir gists
fi

cd gists
git clone https://gist.github.com/dayne/63c53402f0045d2c856ac9400bd303bc gist-dayne-golang

cd gist-dayne-golang

if [ ! -d $HOME/.bash.d ]; then
    echo "Error: you need to install t3x or setup .bash.d"
    exit 1
fi

cp golang.sh $HOME/.bash.d/
cp golang.installer.dsh $HOME/.bash.d/

# open new bash (close/open a terminal)

Docker-Test

CMD="golang.sh"
docker run -it --rm \
       -v "$(pwd)/golang.sh:/golang.sh" \
       -v "$(pwd)/golang.installer.dsh/:/golang.installer.dsh" \
       ubuntu bash -c "./${CMD}"
#!/usr/bin/bash
#set -e
# inspired areas:
# https://raw.githubusercontent.com/canha/golang-tools-install-script/master/goinstall.sh
GOROOT=${GOROOT:-"$HOME/.go"}
GOPATH=${GOPATH:-"$HOME/go"}
GO_VERSION="1.23.1"
function boom() {
echo "Error: ${1}"
sleep 1
exit 1
}
function require_command() {
which "$1" > /dev/null 2>&1
if [ $? -eq 1 ]; then
if [ "$INSTALL_DEPS" == 'true' ]; then
sudo apt install -y $1
else
if [[ "$2" != "" ]]; then
echo "#>> missing command : $1"
echo "#>> POSSIBLE FIX: $2"
fi
boom "required command not found: $1"
fi
fi
}
get_platform() {
if ! command -v uname > /dev/null; then
echo "Error: We dont' have uname"
exit 1
fi
OS="$(uname -s)"
ARCH="$(uname -m)"
case $OS in
"Linux")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"aarch64")
ARCH=arm64
;;
"armv6" | "armv7l")
ARCH=armv6l
;;
"armv8")
ARCH=arm64
;;
"i686")
ARCH=386
;;
.*386.*)
ARCH=386
;;
esac
PLATFORM="linux-$ARCH"
;;
"Darwin")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"arm64")
ARCH=arm64
;;
esac
PLATFORM="darwin-$ARCH"
;;
esac
echo $PLATFORM
}
get_go_version() {
# https://go.dev/dl/
echo $GO_VERSION
}
get_go_package_url() {
local version="$(get_go_version)"
local platform="$(get_platform)"
local go_pkg_url="https://storage.googleapis.com/golang/go${version}.${platform}.tar.gz"
echo "$go_pkg_url"
}
require_package() {
echo -n "Checking for required package: $1 "
dpkg -s $1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo " ... installed"
return 0
else
echo " ... not installed"
if [[ "$INSTALL_DEPS" == "true" ]]; then
sudo apt install -y $1
else
# ask_to_install_package
exit 1
fi
fi
}
user=$(whoami)
echo "..... $user "
if [[ "$user" == "root" ]]; then
# likely docker test situation
export INSTALL_DEPS=true
echo "Running as root : INSTALL_DEPS=$INSTALL_DEPS"
if ! command -v sudo > /dev/null; then
apt update
apt install -y sudo
fi
fi
require_command wget
require_command git
require_package "build-essential"
if [ -f $GOROOT/.installed ]; then
echo "Skipping download & install: $GOROOT/.installed exists"
else
TEMP_DIR=$(mktemp -d)
PKG_URL="$(get_go_package_url)"
echo "Downloading $(basename $PKG_URL)"
wget $PKG_URL -O "$TEMP_DIR/go.tar.gz"
if [ $? -ne 0 ]; then
# TODO: check internet access / dns issues
# and warn if that appears to be the issue
echo "Download failed! Exiting."
fi
echo "Extracting Go into $GOROOT"
mkdir -p "$GOROOT"
tar -C "$GOROOT" --strip-components=1 -xzf "$TEMP_DIR/go.tar.gz"
if [ $? -ne 0 ]; then
echo "Failed to extract the tar file into $GOROOT"
exit
fi
touch $GOROOT/.installed
fi
for D in "${GOPATH}/"{src,pkg,bin}; do
if [[ ! -d $D ]]; then
mkdir -vp $D
fi
done
echo -e '\n# GoLang'
echo "##### TODO: add ~/.bash.d/go.sh stuff"
echo "export GOROOT=${GOROOT}"
echo 'export PATH=$GOROOT/bin:$PATH'
echo "export GOPATH=$GOPATH"
echo 'export PATH=$GOPATH/bin:$PATH'
echo "###########"
#!/usr/bin/bash
# this script lives in $HOME/.bash.d/chruby.sh
#
# https://golang.org/doc/code.html#GOPATH
if [[ -d $HOME/.go && -f $HOME/.go/bin/go ]]; then
export GOROOT=$HOME/.go
export PATH=$GOROOT/bin:$PATH
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH
fi
if command -v go > /dev/null; then
# we have go
for I in $HOME/.bash.d/go-tools/*.sh; do
if [ ! -f $I.disabled ]; then
source $I
fi
done
else
# we do not have go
function _dsh_install_go() {
SCRIPT_DIR=$(cd $(dirname $BASH_SOURCE[0]) > /dev/null; pwd)
CMD=${SCRIPT_DIR}/golang.installer.dsh
echo "Running $CMD"
$CMD
}
# echo "missing go - apt install golang or:"
# https://awesomeopensource.com/project/canha/golang-tools-install-script
# echo "wget -q -O - https://git.io/vQhTU | bash"
if [[ "${BASH_SCRIPT[0]}" == "${0}" ]]; then
# Script is being sourced"
echo "missing go - try fixing with: _dsh_install_go"
else
# script being executed .. run installer
_dsh_install_go
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment