Skip to content

Commit

Permalink
Several changes:
Browse files Browse the repository at this point in the history
- Small fixes to output syntax.
- Verifies a user has permissions to install packages, before attempting
to install them.
- Checks if a package is installed, before attempting to install it.
  • Loading branch information
StrangeRanger committed Dec 5, 2024
1 parent 8713334 commit 2df126e
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions run_onchange_install-packages.bash.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]############################################################################
Expand All @@ -31,32 +51,53 @@ 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

# Install Pacman-specific packages
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

# Install APT-specific packages
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
;;
Expand All @@ -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 }}
;;

Expand All @@ -78,5 +124,5 @@ case "{{ .chezmoi.os }}" in
;;
esac

echo "${C_GREEN}Package installation complete"
echo "${C_SUCCESS}Package installation complete"

0 comments on commit 2df126e

Please sign in to comment.