Skip to content

Commit

Permalink
Refactored bootstrap.sh CLI arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceiphr committed Jun 28, 2023
1 parent 61413a3 commit cad6524
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
85 changes: 69 additions & 16 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
# MIT License
# Copyright (c) 2023 Ari Birnbaum.

# Options -> Configure from command line...
INSTALL_PKGS=true
SYNC_DOTFILES=true
SYNC_GNOME=true
UNATTENDED=false

# For echo -e color support.
TXT_DEFAULT='\033[0m'
TXT_GREY='\033[2m'
TXT_GREEN='\033[0;32m'
TXT_YELLOW='\033[0;33m'
TXT_RED='\033[0;31m'
Expand All @@ -15,6 +22,7 @@ TXT_BOLD='\033[1m'
# https://no-color.org/
if [[ -n "${NO_COLOR}" ]]; then
TXT_DEFAULT='\033[0m'
TXT_GREY='\033[0m'
TXT_YELLOW='\033[0m'
TXT_GREEN='\033[0m'
TXT_RED='\033[0m'
Expand All @@ -25,7 +33,7 @@ PATH="$PATH:$HOME/.local/bin"
# Clean up after ourselves.
function bootstrap_reset() {
unset -f yes_or_no error install_omz install_dnf install_flatpak install_apt \
install_python install_node install_pkgs install_fzf install_gnome_extensions \
install_python install_node install_pkgs install_fzf install_gnome_extensions help \
install_gnome_theme sync_gnome_settings sync_dotfiles install bootstrap_reset handle_sigint
}

Expand All @@ -38,9 +46,6 @@ function handle_sigint() {

trap 'handle_sigint' INT

# Pull latest changes from repo and submodules.
git pull --recurse-submodules origin >/dev/null 2>&1 || error "Unable to pull latest changes."

# Error handling and cleanup.
# Usage: echo "Hello world!" || error "Error message"
function error() {
Expand Down Expand Up @@ -99,7 +104,7 @@ function install_omz() {
function install_dnf() {
# Add third-party repos.
echo -e "${TXT_YELLOW}+${TXT_DEFAULT} Adding third-party DNF repos..."
[ ! "$CODESPACES" ] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."
[[ ! "$CODESPACES" ]] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."

# RPM Fusion
echo -e "${TXT_YELLOW}+${TXT_DEFAULT} Adding RPM Fusion repo..."
Expand Down Expand Up @@ -151,7 +156,7 @@ function install_flatpak() {

function install_apt() {
echo -e "${TXT_GREEN}>${TXT_DEFAULT} Installing APT packages..."
[ ! "$CODESPACES" ] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."
[[ ! "$CODESPACES" ]] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."

sudo apt-get update -y >/dev/null 2>&1 || error "Unable to update packages."
sudo apt install -y $(cat packages/apt) || error "Unable to install packages."
Expand Down Expand Up @@ -233,46 +238,94 @@ function sync_dotfiles() {
}

function install() {
# Pull latest changes from repo and submodules.
git pull --recurse-submodules origin >/dev/null 2>&1 || error "Unable to pull latest changes."

# Needs to be run first to set up environment variables
source src/.zshenv

# Install packages
[[ ! "$CI" ]] && install_pkgs
# Note: We don't install packages in CI. Erorr prone and not necessary.
[[ ! "$CI" ]] && ([[ "$INSTALL_PKGS" == true ]] && install_pkgs)

# Install various plugins and extensions
install_fzf
install_omz

# Install GNOME extensions and theme if running GNOME
if [[ "$DESKTOP_SESSION" == "gnome" ]]; then
if [[ "$DESKTOP_SESSION" == "gnome" ]] && [[ "$SYNC_GNOME" == true ]]; then
install_gnome_extensions
install_gnome_theme
sync_gnome_settings
fi

sync_dotfiles
[[ "$SYNC_DOTFILES" == true ]] && sync_dotfiles

# Change default shell to zsh if it's not already
if [[ "$SHELL" != "/usr/bin/zsh" ]] && [[ ! "$CI" ]]; then
echo -e "${TXT_GREEN}>${TXT_DEFAULT} Changing shell to zsh..."
[ ! "$CODESPACES" ] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."
[[ ! "$CODESPACES" ]] && echo -e "${TXT_GREEN}>${TXT_DEFAULT} Enter your password if prompted."
sudo chsh "$(id -un)" --shell "/usr/bin/zsh" >/dev/null 2>&1 || error "Unable to change shell. Change it manually."
fi

zsh -c "source ~/.zshrc" >/dev/null 2>&1 || error "Unable to reload terminal."
echo -e "${TXT_GREEN}>${TXT_DEFAULT} Done."
}

function help() {
echo -e "$(
cat <<-EOF
Usage: $(basename "$0") [options]
${TXT_GREY}
Ari's dotfiles bootstrap script.
${TXT_DEFAULT}
Options:
-np, --no-packages Skip installing packages
-ns, --no-symlinks Skip syncing dotfiles
-ng, --no-gnome Skip configuring GNOME
-u, --unattended Skip all prompts
-h, --help Show this help message and exit
EOF
)"
}

# TODO: Implement documented flags and --help
if [ "$1" == "--force" ] || [ "$1" == "-f" ] || [ "$CODESPACES" ]; then
while [[ $# -gt 0 ]]; do
case $1 in
-np | --no-packages)
INSTALL_PKGS=false
shift
;;
-ns | --no-symlinks)
SYNC_DOTFILES=false
shift
;;
-ng | --no-gnome)
SYNC_GNOME=false
shift
;;
-u | --unattended)
UNATTENDED=true
shift
;;
-h | --help)
help
exit 0
;;
*)
echo -e "${TXT_RED}!${TXT_DEFAULT} Unknown option: $1"
exit 1
;;
esac
done

if [[ "$UNATTENDED" == true ]] || [[ "$CODESPACES" ]]; then
install
elif [ "$1" == "--sync" ] || [ "$1" == "-s" ]; then
sync_dotfiles
else
echo -e "${TXT_GREEN}>${TXT_DEFAULT} This may overwrite existing files in your home directory."
yes_or_no "Are you sure?" || (bootstrap_reset && exit 1)
install
fi

# Reload terminal
zsh -c "source ~/.zshrc" >/dev/null 2>&1 || error "Unable to reload terminal."
echo -e "${TXT_GREEN}>${TXT_DEFAULT} Done."
bootstrap_reset
exit 0
4 changes: 2 additions & 2 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ setup() {
}

@test "Standard dotfiles bootstrap." {
run bootstrap.sh -f
run bootstrap.sh --no-packages --unattended
[ "$status" -eq 0 ]

assert_output --partial "Syncing dotfiles..."
Expand All @@ -22,7 +22,7 @@ setup() {
}

@test "Remote dotfiles install." {
run remote-install.sh -f
run remote-install.sh --no-packages --unattended
[ "$status" -eq 0 ]

assert_output --partial "Installing dotfiles..."
Expand Down

0 comments on commit cad6524

Please sign in to comment.