Skip to content

Instantly share code, notes, and snippets.

@jtsagata
Created July 24, 2019 07:36
Show Gist options
  • Save jtsagata/6977b131ab55a74d3fa3472aafc68e59 to your computer and use it in GitHub Desktop.
Save jtsagata/6977b131ab55a74d3fa3472aafc68e59 to your computer and use it in GitHub Desktop.
Install rust using rustup
#!/bin/bash
dir=$(dirname "$0")
#
# DESC: Install Rust language and rust tools
#
reset=`tput sgr0`
red=`tput sgr0; tput setaf 1`
bRed=`tput bold; tput setaf 1`
rRed=`tput rev; tput setaf 1`
green=`tput sgr0; tput setaf 2`
bGreen=`tput bold; tput setaf 2`
rGreen=`tput rev; tput setaf 2`
yellow=`tput setaf 3`
blue=`tput setaf 4`
magenta=`tput setaf 5`
cyan=`tput setaf 6`
white=`tput setaf 7`
black=`tput setaf 0`
bold=`tput bold`
APT=/usr/bin/apt-get
APT_CACHE=/usr/bin/apt-cache
DPKG=/usr/bin/dpkg
#
# pkg_install <package> [<command>]
# Install a package from apt repositories
# example: pkg_install tmux
# pkg_install tmux tmux
#
function pkg_install() {
pkg=$1
cmd=$2
set +e
# Check if package exists
pkg_name_exist=`${APT_CACHE} pkgnames | grep "^${pkg}$"`
if [[ "${pkg_name_exist}" == "${pkg}" ]]; then
# Check if package allready installed
${DPKG} --get-selections | grep -q "^${pkg}[[:space:]]*install$"
if [[ "$?" == "1" ]]; then
# Warning if command exists on path
if [[ ! "${cmd}" == "" ]]; then
if [ -x "$(command -v ${cmd})" ] ; then
echo "${magenta}${cmd} is allready installed in '${blue}$(command -v ${cmd})${reset}'."
fi
fi
# Do the installation
echo "${green}Installing package '${bGreen}${pkg}${green}'...${reset}"
sudo ${APT} install -y ${pkg}
# Check if package failed to install
if [ $? != 0 ]; then
echo "${bRed}ERROR:${red}package ${pkg} failed do installl.${reset}"
else
# If cmd show the binary
if [[ ! "${cmd}" == "" ]]; then
echo "${green}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
fi
fi
else
echo "${yellow}Skipping installation of package '${bold}${pkg}${reset}${yellow}'...${reset}"
# If cmd show the binary
if [[ ! "${cmd}" == "" ]]; then
echo "${yellow}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
fi
fi
else
echo "${red}${bold}ERROR:${reset}${red}package ${pkg} is not at the repositories${reset}"
fi
}
function install_rust() {
pkg_install curl curl
pkg_install build-essential gcc
if [ ! -x "$(command -v rustup)" ] ; then
echo "${green}Installing Rust language using ${bGreen}'rustup'${reset}"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
echo "${green}rustup is installed in '${blue}$(command -v rustup)${reset}'"
else
echo "${yellow}rustup is installed in '${blue}$(command -v rustup)${reset}'"
fi
}
function crate_install() {
pkg=$1
cmd=$2
if [[ "${cmd}" == "" ]]; then
cmd=${pkg}
fi
if [ ! -x "$(command -v rustup)" ] ; then
install_rust
fi
if [ ! -x "$(command -v ${cmd})" ] ; then
info_msg "Installing crate ${cmd} using ${bGreen}'rustup'${reset}"
cargo install ${pkg}
echo "${green}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
else
echo "${yellow}Skipping installation of crate '${bold}${cmd}${reset}${yellow}'...${reset}"
echo "${yellow}${cmd} is installed in '${blue}$(command -v ${cmd})${reset}'"
fi
}
crate_install topgrade
crate_install lsd
crate_install bat
crate_install ripgrep rg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment