Skip to content

Instantly share code, notes, and snippets.

@widnyana
Last active October 16, 2023 06:46
Show Gist options
  • Save widnyana/1dfdd02922c05100c37703b22f6f1670 to your computer and use it in GitHub Desktop.
Save widnyana/1dfdd02922c05100c37703b22f6f1670 to your computer and use it in GitHub Desktop.
Bash script to install golang compiler
#!/bin/bash
# shellcheck disable=SC2016
set -ex
VERSION="${GO_VERSION:-"1.20.6"}"
GOROOT="/build/.go"
GOPATH="/build/.go/gopath"
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
print_help() {
echo "Usage: bash goinstall.sh OPTIONS"
echo -e "\nOPTIONS:"
echo -e " --remove\tRemove currently installed version"
echo -e " --version\tSpecify a version number to install"
}
if [ -z "$PLATFORM" ]; then
echo "Your operating system is not supported by the script."
exit 1
fi
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
shell_profile="$HOME/.zshrc"
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
shell_profile="$HOME/.bashrc"
elif [ -n "$($SHELL -c 'echo $FISH_VERSION')" ]; then
shell="fish"
if [ -d "$XDG_CONFIG_HOME" ]; then
shell_profile="$XDG_CONFIG_HOME/fish/config.fish"
else
shell_profile="$HOME/.config/fish/config.fish"
fi
fi
if [ "$1" == "--remove" ]; then
rm -rf "$GOROOT"
if [ "$OS" == "Darwin" ]; then
if [ "$shell" == "fish" ]; then
sed -i "" '/# GoLang/d' "$shell_profile"
sed -i "" '/set GOROOT/d' "$shell_profile"
sed -i "" '/set GOPATH/d' "$shell_profile"
sed -i "" '/set PATH $GOPATH\/bin $GOROOT\/bin $PATH/d' "$shell_profile"
else
sed -i "" '/# GoLang/d' "$shell_profile"
sed -i "" '/export GOROOT/d' "$shell_profile"
sed -i "" '/$GOROOT\/bin/d' "$shell_profile"
sed -i "" '/export GOPATH/d' "$shell_profile"
sed -i "" '/$GOPATH\/bin/d' "$shell_profile"
fi
else
if [ "$shell" == "fish" ]; then
sed -i '/# GoLang/d' "$shell_profile"
sed -i '/set GOROOT/d' "$shell_profile"
sed -i '/set GOPATH/d' "$shell_profile"
sed -i '/set PATH $GOPATH\/bin $GOROOT\/bin $PATH/d' "$shell_profile"
else
sed -i '/# GoLang/d' "$shell_profile"
sed -i '/export GOROOT/d' "$shell_profile"
sed -i '/$GOROOT\/bin/d' "$shell_profile"
sed -i '/export GOPATH/d' "$shell_profile"
sed -i '/$GOPATH\/bin/d' "$shell_profile"
fi
fi
echo "Go removed."
exit 0
elif [ "$1" == "--help" ]; then
print_help
exit 0
elif [ "$1" == "--version" ]; then
if [ -z "$2" ]; then # Check if --version has a second positional parameter
echo "Please provide a version number for: $1"
else
VERSION=$2
fi
elif [ ! -z "$1" ]; then
echo "Unrecognized option: $1"
exit 1
fi
if [ -d "$GOROOT" ]; then
echo "The Go install directory ($GOROOT) already exists. Exiting."
exit 1
fi
PACKAGE_NAME="go$VERSION.$PLATFORM.tar.gz"
TEMP_DIRECTORY=$(mktemp -d)
echo "Downloading $PACKAGE_NAME ..."
if hash wget 2>/dev/null; then
wget https://storage.googleapis.com/golang/$PACKAGE_NAME -O "$TEMP_DIRECTORY/go.tar.gz"
else
curl -o "$TEMP_DIRECTORY/go.tar.gz" https://storage.googleapis.com/golang/$PACKAGE_NAME
fi
if [ $? -ne 0 ]; then
echo "Download failed! Exiting."
exit 1
fi
echo "Extracting File..."
mkdir -p "$GOROOT"
tar -C "$GOROOT" --strip-components=1 -xzf "$TEMP_DIRECTORY/go.tar.gz"
echo "Configuring shell profile in: $shell_profile"
touch "$shell_profile"
if [ "$shell" == "fish" ]; then
{
echo -e '\n# GoLang'
echo "set GOROOT '${GOROOT}'"
echo "set GOPATH '$GOPATH'"
echo 'set PATH $GOPATH/bin $GOROOT/bin $PATH'
} >> "$shell_profile"
else
{
echo -e '\n# GoLang'
echo "export GOROOT=${GOROOT}"
echo 'export PATH=$GOROOT/bin:$PATH'
echo "export GOPATH=$GOPATH"
echo 'export PATH=$GOPATH/bin:$PATH'
} >> "$shell_profile"
fi
mkdir -p "${GOPATH}/"{src,pkg,bin}
echo -e "\nGo $VERSION was installed into $GOROOT.\nMake sure to relogin into your shell or run:"
echo -e "\n\tsource $shell_profile\n\nto update your environment variables."
echo "Tip: Opening a new terminal window usually just works. :)"
rm -f "$TEMP_DIRECTORY/go.tar.gz"
#!/usr/bin/env bash
## Reference: https://github.com/confluentinc/quickstart-confluent-kafka/blob/902f6ebec0917df5d3dee42e6335e4fbd3ebfe7d/scripts/cp-install.sh
set -euo pipefail
THIS_SCRIPT=`readlink -f $0`
SCRIPTDIR=`dirname ${THIS_SCRIPT}`
LOG=/tmp/cp-install.log
CONFLUENT_VERSION="7.5"
OS_VER="8"
REPO_FILE="/etc/yum.repos.d/confluent.repo"
add_confluent_repo() {
[ -f $REPO_FILE ] && return
cat >> $REPO_FILE << EOF_repo
[Confluent.dist]
name=Confluent repository (dist)
baseurl=http://packages.confluent.io/rpm/${CONFLUENT_VERSION}/$OS_VER
gpgcheck=1
gpgkey=http://packages.confluent.io/rpm/${CONFLUENT_VERSION}/archive.key
enabled=1
[Confluent]
name=Confluent repository
baseurl=http://packages.confluent.io/rpm/${CONFLUENT_VERSION}
gpgcheck=1
gpgkey=http://packages.confluent.io/rpm/${CONFLUENT_VERSION}/archive.key
enabled=1
EOF_repo
rpm --import https://packages.confluent.io/rpm/${CONFLUENT_VERSION}/archive.key
cat $REPO_FILE
# update-crypto-policies --set DEFAULT:SHA1
}
update_confluent_repo() {
if [ -f $REPO_FILE ] ; then
sed -i "s/rpm\/.../rpm\/${CONFLUENT_VERSION}/" $REPO_FILE
else
add_confluent_repo
fi
microdnf makecache
}
install_librdkafka_from_repo() {
microdnf --nodocs install --assumeyes \
librdkafka1
}
main()
{
echo "$0 script started at "`date` >> $LOG
if [ `id -u` -ne 0 ] ; then
echo " ERROR: script must be run as root" >> $LOG
exit 1
fi
update_confluent_repo
install_librdkafka_from_repo
echo "$0 script finished at "`date` >> $LOG
}
main $@
exitCode=$?
set +x
exit $exitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment