Skip to content

Commit

Permalink
Merge pull request #36 from Rynaro/chore/packages/small-qol-enhancements
Browse files Browse the repository at this point in the history
Make better usage of unpack_it and log functions
  • Loading branch information
Rynaro authored Nov 8, 2024
2 parents 9fe3944 + 49d92ed commit 486466c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 76 deletions.
66 changes: 32 additions & 34 deletions packages/accessories.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ POTIONS_HOME="$USER_HOME_FOLDER/.potions"
ZDOTDIR=$POTIONS_HOME

update_repositories() {
echo "Updating repositories..."
log "Updating repositories..."
if is_macos; then
brew update
elif is_termux; then
pkg update
elif is_wsl || is_linux; then
if is_apt_package_manager; then
echo "If you do not need sudo for any reason, modify this script!"
log "If you do not need sudo for any reason, modify this script!"
sudo apt-get update
else
exit_with_message "No supported package manager have been found! Consider move to another environment supported! Or create a patch! :)"
Expand Down Expand Up @@ -83,46 +83,44 @@ is_apt_package_manager() {

# Function to check and create directory
ensure_directory() {
local dir="$1"
if [ -d "$dir" ]; then
log "Directory '$dir' already exists."
local dir="$1"
if [ -d "$dir" ]; then
log "Directory '$dir' already exists."
else
log "Directory '$dir' does not exist. Creating..."
mkdir -p "$dir"
if [ $? -eq 0 ]; then
log "Successfully created directory '$dir'."
else
log "Directory '$dir' does not exist. Creating..."
mkdir -p "$dir"
if [ $? -eq 0 ]; then
log "Successfully created directory '$dir'."
else
log "Error: Failed to create directory '$dir'." >&2
exit 1
fi
exit_with_message "Error: Failed to create directory '$dir'."
fi
fi
}

ensure_files() {
local dir="$1"
shift
local files=("$@")

for file in "${files[@]}"; do
local filepath="$dir/$file"
if [ -f "$filepath" ]; then
echo "File '$filepath' already exists."
else
echo "File '$filepath' does not exist. Creating..."
touch "$filepath"
if [ $? -eq 0 ]; then
echo "Successfully created file '$filepath'."
else
echo "Error: Failed to create file '$filepath'." >&2
exit 1
fi
fi
done
local dir="$1"
shift
local files=("$@")

for file in "${files[@]}"; do
local filepath="$dir/$file"
if [ -f "$filepath" ]; then
log "File '$filepath' already exists."
else
log "File '$filepath' does not exist. Creating..."
touch "$filepath"
if [ $? -eq 0 ]; then
log "Successfully created file '$filepath'."
else
exit_with_message "Error: Failed to create file '$filepath'."
fi
fi
done
}

# Beautifully message and go!
exit_with_message() {
echo $1
echo "Terminating Potions Routines..."
log $1
log "Terminating Potions Routines..."
exit 1
}
6 changes: 3 additions & 3 deletions packages/common/antidote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install Antidote
install_package() {
if [ -d "${ZDOTDIR:-$POTIONS_HOME}/.antidote" ]; then
echo "Antidote is already installed."
log "Antidote is already installed."
else
echo "Installing Antidote..."
git clone --depth=1 https://github.com/mattmc3/antidote.git ${POTIONS_HOME:-$POTIONS_HOME}/.antidote
log "Installing Antidote..."
git clone --depth=1 https://github.com/mattmc3/antidote.git ${ZDOTDIR:-$POTIONS_HOME}/.antidote
fi
}

Expand Down
12 changes: 6 additions & 6 deletions packages/common/curl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install curl
install_package() {
if command_exists curl; then
echo "curl is already installed."
log "curl is already installed."
else
echo "Installing curl..."
log "Installing curl..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/curl.sh"
unpack_it 'macos/curl'
elif is_termux; then
safe_source "$(dirname "$0")/packages/termux/curl.sh"
unpack_it 'termux/curl'
elif is_wsl; then
safe_source "$(dirname "$0")/packages/wsl/curl.sh"
unpack_it 'wsl/curl'
elif is_linux; then
safe_source "$(dirname "$0")/packages/debian/curl.sh"
unpack_it 'debian/curl'
fi
fi
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install Git
install_package() {
if command_exists git; then
echo "Git is already installed."
log "Git is already installed."
else
echo "Installing Git..."
log "Installing Git..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/git.sh"
elif is_termux; then
Expand Down
12 changes: 6 additions & 6 deletions packages/common/openvpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install OpenVPN
install_package() {
if command_exists openvpn; then
echo "OpenVPN is already installed."
log "OpenVPN is already installed."
else
echo "Installing ..."
log "Installing OpenVPN..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/openvpn.sh"
unpack_it 'macos/openvpn'
elif is_termux; then
safe_source "$(dirname "$0")/packages/termux/openvpn.sh"
unpack_it 'termux/openvpn'
elif is_wsl; then
safe_source "$(dirname "$0")/packages/wsl/openvpn.sh"
unpack_it 'wsl/openvpn'
elif is_linux; then
safe_source "$(dirname "$0")/packages/debian/openvpn.sh"
unpack_it 'debian/openvpn'
fi
fi
}
Expand Down
15 changes: 7 additions & 8 deletions packages/common/tmux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

source "$(dirname "$0")/packages/accessories.sh"


# Function to install TMUX
install_package() {
if command_exists tmux; then
echo "TMUX is already installed."
log "TMUX is already installed."
else
echo "Installing TMUX..."
log "Installing TMUX..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/tmux.sh"
unpack_it 'macos/tmux'
elif is_termux; then
safe_source "$(dirname "$0")/packages/termux/tmux.sh"
unpack_it 'termux/tmux'
elif is_wsl; then
safe_source "$(dirname "$0")/packages/wsl/tmux.sh"
unpack_it 'wsl/tmux'
elif is_linux; then
safe_source "$(dirname "$0")/packages/debian/tmux.sh"
unpack_it 'debian/tmux'
fi
fi
}

prepare_package() {
mkdir -p $POTIONS_HOME/tmux/plugins
ensure_directory "$POTIONS_HOME/tmux/plugins"
}

configure_package() {
Expand Down
8 changes: 4 additions & 4 deletions packages/common/vim-plug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install Vim-Plug
install_package() {
if [ -d "${XDG_DATA_HOME:-$USER_HOME_FOLDER/.local/share}/nvim/site/autoload" ]; then
echo "Vim-Plug is already installed."
log "Vim-Plug is already installed."
else
echo "Installing Vim-Plug..."
log "Installing Vim-Plug..."
sh -c 'curl -fLo "${XDG_DATA_HOME:-$USER_HOME_FOLDER/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
fi
}

configure_package() {
mkdir -p $USER_HOME_FOLDER/.config/nvim
mkdir -p $POTIONS_HOME/plugged
ensure_directory "$USER_HOME_FOLDER/.config/nvim"
ensure_directory "$POTIONS_HOME/plugged"
echo "source $POTIONS_HOME/nvim/init.vim" > ~/.config/nvim/init.vim
}

Expand Down
12 changes: 6 additions & 6 deletions packages/common/wget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install wget
install_package() {
if command_exists wget; then
echo "wget is already installed."
log "wget is already installed."
else
echo "Installing wget..."
log "Installing wget..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/wget.sh"
unpack_it 'macos/wget'
elif is_termux; then
safe_source "$(dirname "$0")/packages/termux/wget.sh"
unpack_it 'termux/wget'
elif is_wsl; then
safe_source "$(dirname "$0")/packages/wsl/wget.sh"
unpack_it 'wsl/wget'
elif is_linux; then
safe_source "$(dirname "$0")/packages/debian/wget.sh"
unpack_it 'debian/wget'
fi
fi
}
Expand Down
16 changes: 9 additions & 7 deletions packages/common/zsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "$(dirname "$0")/packages/accessories.sh"
# Function to install Zsh
install_package() {
if command_exists zsh; then
echo "Zsh is already installed."
log "Zsh is already installed."
else
echo "Installing Zsh..."
log "Installing Zsh..."
if is_macos; then
safe_source "$(dirname "$0")/packages/macos/zsh.sh"
unpack_it 'macos/zsh'
elif is_termux; then
safe_source "$(dirname "$0")/packages/termux/zsh.sh"
unpack_it 'termux/zsh'
elif is_wsl; then
safe_source "$(dirname "$0")/packages/wsl/zsh.sh"
unpack_it 'wsl/zsh'
elif is_linux; then
safe_source "$(dirname "$0")/packages/debian/zsh.sh"
unpack_it 'debian/zsh'
fi
fi
}
Expand All @@ -27,7 +27,9 @@ configure_package() {
source .zshenv
fi

chsh -s zsh
if is_termux; then
chsh -s zsh
fi
}

install_package
Expand Down

0 comments on commit 486466c

Please sign in to comment.