diff --git a/run_onchange_install-packages.bash.tmpl b/run_onchange_install-packages.bash.tmpl index 6e3a07c..785609d 100644 --- a/run_onchange_install-packages.bash.tmpl +++ b/run_onchange_install-packages.bash.tmpl @@ -9,14 +9,34 @@ ## Used to colorize output. C_GREEN="$(printf '\033[0;32m')" C_BLUE="$(printf '\033[0;34m')" +C_CYAN="$(printf '\033[0;36m')" C_RED="$(printf '\033[1;31m')" C_NC="$(printf '\033[0m')" -readonly C_GREEN C_BLUE C_RED C_NC +readonly C_GREEN C_BLUE C_CYAN C_RED C_NC ## Short-hand colorized messages. readonly C_SUCCESS="${C_GREEN}==>${C_NC} " readonly C_ERROR="${C_RED}ERROR:${C_NC} " readonly C_INFO="${C_BLUE}==>${C_NC} " +readonly C_NOTE="${C_CYAN}==>${C_NC} " + + +####[ Prepping ]######################################################################## + + +# Permissions only need to be checked on Linux, as Homebrew on macOS doesn't require +# elevated permissions. +if [[ {{ .chezmoi.os }} == "linux" ]]; then + echo "${C_INFO}Checking user permissions..." + echo "${C_INFO} Executing: 'sudo -l'" + if sudo -l &> /dev/null; then + echo "${C_SUCCESS}'${USER}' has permissions to install packages" + else + echo "${C_ERROR}'${USER}' does not have permissions to install packages" + echo "${C_INFO}Skipping package installation" + exit 1 + fi +fi ####[ Main ]############################################################################ @@ -31,14 +51,24 @@ case "{{ .chezmoi.os }}" in if hash pacman &> /dev/null; then sudo pacman -Syu --noconfirm {{- range .packages.linux.general }} - echo "${C_INFO}Installing {{ . }}..." - sudo pacman -S --noconfirm --needed "{{ . }}" + if pacman -Q "{{ . }}" &> /dev/null; then + echo "${C_NOTE}'{{ . }}' is already installed" + echo "${C_INFO}Skipping..." + else + echo "${C_INFO}Installing '{{ . }}'..." + sudo pacman -S --noconfirm --needed "{{ . }}" + fi {{- end }} elif hash apt &> /dev/null; then sudo apt update {{- range .packages.linux.general }} - echo "${C_INFO}Installing {{ . }}..." - sudo apt install -y "{{ . }}" + if dpkg -l "{{ . }}" &> /dev/null; then + echo "${C_NOTE}'{{ . }}' is already installed" + echo "${C_INFO}Skipping..." + else + echo "${C_INFO}Installing '{{ . }}'..." + sudo apt install -y "{{ . }}" + fi {{- end }} fi @@ -46,8 +76,13 @@ case "{{ .chezmoi.os }}" in if hash pacman &> /dev/null; then echo "${C_INFO}Installing pacman-specific packages..." {{- range .packages.linux.pacman }} - echo "${C_INFO}Installing {{ . }}..." - sudo pacman -S --noconfirm --needed "{{ . }}" + if pacman -Q "{{ . }}" &> /dev/null; then + echo "${C_NOTE}'{{ . }}' is already installed" + echo "${C_INFO}Skipping..." + else + echo "${C_INFO}Installing '{{ . }}'..." + sudo pacman -S --noconfirm --needed "{{ . }}" + fi {{- end }} fi @@ -55,8 +90,14 @@ case "{{ .chezmoi.os }}" in if hash apt &> /dev/null; then echo "${C_INFO}Installing apt-specific packages..." {{- range .packages.linux.apt }} - echo "${C_INFO}Installing {{ . }}..." - sudo apt install -y "{{ . }}" + if dpkg -l "{{ . }}" &> /dev/null; then + echo "${C_NOTE}'{{ . }}' is already installed" + echo "${C_INFO}Skipping..." + continue + else + echo "${C_INFO}Installing '{{ . }}'..." + sudo apt install -y "{{ . }}" + fi {{- end }} fi ;; @@ -67,8 +108,13 @@ case "{{ .chezmoi.os }}" in # Install Homebrew packages echo "${C_INFO}Installing Homebrew formulas..." {{- range .packages.macos.homebrew.formulas }} - echo "${C_INFO}Installing {{ . }}..." - brew install "{{ . }}" + if brew list -1 | grep "{{ . }}" &> /dev/null; then + echo "${C_NOTE}'{{ . }}' is already installed" + echo "${C_INFO}Skipping..." + else + echo "${C_INFO}Installing '{{ . }}'..." + brew install "{{ . }}" + fi {{- end }} ;; @@ -78,5 +124,5 @@ case "{{ .chezmoi.os }}" in ;; esac -echo "${C_GREEN}Package installation complete" +echo "${C_SUCCESS}Package installation complete"