From e28bca88dd9dda6fbfcec996e01c8cd031479349 Mon Sep 17 00:00:00 2001 From: Munif Tanjim Date: Mon, 4 Jan 2021 21:55:50 +0600 Subject: [PATCH] update script: .init.linux.sh --- .init.linux.sh | 289 +++++++++++++++++++++++++++++++------------------ 1 file changed, 182 insertions(+), 107 deletions(-) diff --git a/.init.linux.sh b/.init.linux.sh index 0da2509..01374c0 100755 --- a/.init.linux.sh +++ b/.init.linux.sh @@ -1,6 +1,21 @@ #!/usr/bin/env bash -section_header() { +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +export PATH="${DIR}/scripts.sh:${PATH}" + +command_exists() { + type "${1}" >/dev/null 2>&1 +} + +ensure_linux() { + if [[ $OSTYPE != linux* ]]; then + exit 1 + fi +} + + +TASK() { local -r str="$1" local -r str_len=$(( 4 + ${#str} )) local -r char="${2:-"="}" @@ -12,23 +27,86 @@ section_header() { echo "" } -task_header() { +SUB_TASK() { local -r str="$1" - local -r char="${2:-"*"}" + local -r char="${2:-"="}" echo "" - echo "$char $str" + echo "$char" + echo "$str" + echo "$char" echo "" } -command_exists() { - type "${1}" >/dev/null 2>&1 +ensure_secret_manager() { + if ! command_exists bw; then + echo "command not found: bw" + exit 1 + fi + + local status="$(bw status)" + status="${status##*\"status\":\"}" + status="${status%%\"*}" + + if [[ $status = "unauthenticated" ]]; then + echo "secret manager is not authenticated, run:" + echo "" + echo " bw login" + echo "" + exit 1 + fi + + if [[ $status = "locked" ]]; then + echo "secret manager is locked, run:" + echo "" + echo " export BW_SESSION=\$(bw unlock --raw)" + echo "" + exit 1 + fi + + if [[ $status != "unlocked" ]]; then + echo "secret manager is not unlocked" + echo "" + exit 1 + fi } -if command_exists apt; then - section_header "APT Packages" +install_apt_packages() { + if ! command_exists apt; then + exit 1 + fi + + TASK "Install APT Packages" + + apt_update() { + SUB_TASK "sudo apt update" + sudo apt update + } + + apt_install() { + SUB_TASK "sudo apt install --yes $*" + if command_exists apt-fast; then + sudo apt install --yes $@ + else + sudo apt-fast install --yes $@ + fi + } + + if ! command_exists add-apt-repository; then + apt_install software-properties-common + fi + + add_apt_repository() { + SUB_TASK "sudo add-add-apt-repository --yes --no-update $@" + sudo add-apt-repository --yes --no-update "$@" + } + + declare APT_REPOSITORIES=( + ppa:mmstick76/alacritty + ) declare APT_PACKAGES=( + alacritty chafa curl filezilla git gpa gparted grc htop jq neofetch @@ -37,71 +115,60 @@ if command_exists apt; then tmux tree vim xclip xsel zsh ibus-avro translate-shell - software-properties-common - ) - - echo "${APT_PACKAGES[@]}" - echo "" - - task_header "Updating Cache..." - sudo apt update - task_header "Installing Packages..." - sudo apt install --yes "${APT_PACKAGES[@]}" - - section_header "APT Packages (3rd Party)" - - declare THIRD_PARTY_APT_PACKAGES=( - alacritty ) - echo "${THIRD_PARTY_APT_PACKAGES[@]}" - echo "" - - declare APT_REPOSITORIES=( - ppa:mmstick76/alacritty - ) for apt_repo in "${APT_REPOSITORIES[@]}"; do - task_header "Adding APT Repository: ${apt_repo}" - sudo add-apt-repository --yes --no-update "${apt_repo}" + add_apt_repository ${apt_repo} done - task_header "Updating Cache..." - sudo apt update - task_header "Installing Packages..." - sudo apt install --yes "${THIRD_PARTY_APT_PACKAGES[@]}" -fi + apt_update -if command_exists snap; then - section_header "Snap Packages (classic)" - - declare SNAP_CLASSIC_PACKAGES=( - asciinema code go - ) + apt_install "${APT_PACKAGES[@]}" +} - echo "${SNAP_CLASSIC_PACKAGES[@]}" - echo "" +install_snap_packages() { + if ! command_exists snap; then + exit 1 + fi - for package in "${SNAP_CLASSIC_PACKAGES[@]}"; do - snap install "${package}" --classic - done + TASK "Install Snap Packages" - section_header "Installing Snap Packages (strict)" + snap_install() { + SUB_TASK "snap install $*" + snap install $@ + } declare SNAP_PACKAGES=( - "--beta authy" bucklespring - hugo opera vlc + "--classic asciinema" + "--classic code" + "--classic go" + "--beta authy" + "hugo" + "opera" + "vlc" ) - echo "${SNAP_PACKAGES[@]}" - echo "" - for package in "${SNAP_PACKAGES[@]}"; do - snap install ${package} + snap_install ${package} done -fi +} + +install_github_release_packages() { + if ! command_exists setup-from-github-release; then + exit 1 + fi + + TASK "Install Packages from GitHub Release" -if command_exists setup-from-github-release; then - section_header "Installing Packages from GitHub Release" + setup_from_github_release() { + local -r bin_name="${1}" + local -r repo="${2}" + local -r url_pattern="${3}" + SUB_TASK "setup-from-github-release ${repo} ${url_pattern}" + if ! command_exists "${bin_name}"; then + setup-from-github-release "${repo}" "${url_pattern}" + fi + } declare -A GITHUB_PACKAGE_REPOSITORY=( [bat]="sharkdp/bat" @@ -119,60 +186,68 @@ if command_exists setup-from-github-release; then [rg]="amd64.deb" ) - echo "${!GITHUB_PACKAGE_REPOSITORY[@]}" - echo "" - for bin_name in "${!GITHUB_PACKAGE_REPOSITORY[@]}"; do - if ! command_exists "${bin_name}"; then - setup-from-github-release \ - "${GITHUB_PACKAGE_REPOSITORY[${bin_name}]}" \ - "${GITHUB_PACKAGE_URL_PATTERN[${bin_name}]}" - fi + setup_from_github_release "${bin_name}" \ + "${GITHUB_PACKAGE_REPOSITORY[${bin_name}]}" \ + "${GITHUB_PACKAGE_URL_PATTERN[${bin_name}]}" done -fi - -section_header "Running Setup Scripts" - -declare SETUP_SCRIPTS=( - setup-nvm - setup-pyenv - setup-rbenv - setup-volta - setup-zinit - setup-apt-fast - setup-docker - setup-fzf - setup-git-credential-libsecret - setup-keybase - setup-neovim - setup-postman - setup-rofi - setup-starship - setup-youtube-dl -) - -for script in "${SETUP_SCRIPTS[@]}"; do - echo "" - if command_exists "${script}"; then - echo "[${script}] started" +} + +run_setup_scripts() { + TASK "Run Setup Scripts" + + declare SETUP_SCRIPTS=( + setup-nvm + setup-pyenv + setup-rbenv + setup-volta + setup-zinit + setup-apt-fast + setup-docker + setup-fzf + setup-git-credential-libsecret + setup-keybase + setup-neovim + setup-postman + setup-rofi + setup-starship + setup-youtube-dl + ) + + for script in "${SETUP_SCRIPTS[@]}"; do echo "" - ${script} + if command_exists ${script}; then + echo "[${script}] started" + echo "" + ${script} + echo "" + echo "[${script}] ended" + else + echo "[${script}] not found!" + fi echo "" - echo "[${script}] ended" - else - echo "[${script}] not found!" - fi - echo "" -done + done +} -section_header "Creating Necessary Directories" +create_necessary_directories() { + TASK "Creating Necessary Directories" + + declare NECESSARY_DIRECTORIES=( + ~/.cache/nano/backup + ~/.local/share/mpd/playlists + ~/.local/share/{nvim,vim}/{backup,swap,undo} + ) + + printf '> %s\n' "${NECESSARY_DIRECTORIES[@]}" + echo "" + mkdir -p "${NECESSARY_DIRECTORIES[@]}" +} -declare NECESSARY_DIRECTORIES=( - ~/.cache/nano/backup - ~/.local/share/mpd/playlists - ~/.local/share/{nvim,vim}/{backup,swap,undo} -) +ensure_linux +ensure_secret_manager -printf '> %s\n' "${NECESSARY_DIRECTORIES[@]}" -echo "" -mkdir -p "${NECESSARY_DIRECTORIES[@]}" +install_apt_packages +install_snap_packages +install_github_release_packages +run_setup_scripts +create_necessary_directories -- knotmirror 0.0.1