diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..5d7f1cf --- /dev/null +++ b/install.sh @@ -0,0 +1,143 @@ +#!/bin/bash + +set -u + +usage() { + cat 1>&2 <&1 +} + +info() { + printf "%s\n" "$1" >&1 +} + +warning() { + printf "\033[33m%s\033[0m\n" "$1" >&2 +} + +error() { + printf "\033[31;1m%s\033[0m\n" "$1" >&2 + exit 1 +} + +cmd_chk() { + command -v "$1" >/dev/null 2>&1 +} + +## Ensures that the command executes without error +ensure() { + if ! "$@"; then error "command failed: $*"; fi +} + +need_cmd() { + if ! cmd_chk "$1"; then + error "need $1 (command not found)" + fi +} + +prompt_confirm() { + if [ ! -t 1 ]; then + error "Unable to run interactively. Please execute this script using interactive shell" + fi + + while true; do + read -rp "Is this okay? (y/N): " _choice + _choice=$(echo "$_choice" | tr '[:upper:]' '[:lower:]') + + case "$_choice" in + y | yes) break ;; + n | no) error "Operation aborted" ;; + esac + done +} + +cargo_build() { + success "Compiling from source..." + + if ! cmd_chk "cargo"; then + success "Cargo will be installed." + prompt_confirm + + ensure curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh + + # shellcheck source=/dev/null + source "$HOME/.cargo/env" + fi + + RUSTFLAGS="-C target-cpu=native" ensure cargo install zoxide +} + +main "$@" || exit 1