Skip to content

Commit

Permalink
Support for version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbahartr committed Nov 15, 2024
1 parent bedc9c0 commit ebf46ce
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions dotfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function _task_done {

# Arch setup function
function arch_setup {
for pkg in ansible curl python python-pip python-watchdog openssh rsync git noto-fonts-emoji; do
for pkg in ansible curl python python-pip python-watchdog openssh rsync git noto-fonts-emoji jq; do
if ! pacman -Q $pkg >/dev/null 2>&1; then
_task "Installing $pkg"
_cmd "sudo pacman -S --noconfirm $pkg" 5 2 "Failed to install $pkg."
Expand All @@ -153,18 +153,39 @@ function update_ansible_galaxy {
fi
}

# Get the latest release tag
function get_latest_release {
curl --silent "https://api.github.com/repos/$REPO/releases/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"([^"]+)".*/\1/'
# Get the latest version
function get_latest_version {
if [[ $MODE == "rolling" ]]; then
curl --silent "https://api.github.com/repos/$REPO/commits/heads/main" | \
jq -r .sha
else
curl --silent "https://api.github.com/repos/$REPO/releases/latest" | \
jq -r .tag_name
fi
}

# Get the installed version
function get_installed_version {
if [[ $MODE == "rolling" ]] && [[ -f "$DOTFILES_DIR/.git/ORIG_HEAD" ]]; then
cat $DOTFILES_DIR/.git/ORIG_HEAD
elif [[ -f "$DOTFILES_DIR/VERSION" ]]; then
cat $DOTFILES_DIR/VERSION
fi
}

# Check if we have the latest version
function check_version {
local latest_version=$(get_latest_version)
local installed_version=$(get_installed_version)

_task "Checking for updates..."
_cmd "[[ \"${latest_version}\" != \"${installed_version}\" ]]" 1 0 "You already have the latest version."
}

# Get the latest release zip URL
function get_latest_zip {
curl --silent "https://api.github.com/repos/$REPO/releases/latest" | \
grep '"zipball_url":' | \
sed -E 's/.*"([^"]+)".*/\1/'
jq -r .zipball_url
}

# Download the latest release and extract it
Expand All @@ -179,6 +200,7 @@ function download_latest_release {
_cmd "unzip -o $zip_file -d $HOME/dotfiles_temp" 3 2 "Failed to extract the latest release."

_task "Renaming the extracted folder to dotfiles"
_cmd "rm -rf $DOTFILES_DIR" 3 2 "Failed to remove old dotfiles directory."
_cmd "mv $HOME/dotfiles_temp/*dotfiles-* $DOTFILES_DIR" 3 2 "Failed to rename extracted folder."

_task "Cleaning up"
Expand All @@ -187,9 +209,15 @@ function download_latest_release {

# Clone repository for rolling updates
function clone_repository {
_task "Cloning the repository"
_cmd "rm -rf $DOTFILES_DIR" 3 2 "Failed to remove old dotfiles directory."
_cmd "git clone $REPO_URL $DOTFILES_DIR" 3 2 "Failed to clone repository."
if [[ ! -d "$DOTFILES_DIR/.git" ]]; then
_task "Cloning the repository"
_cmd "rm -rf $DOTFILES_DIR" 3 2 "Failed to remove old dotfiles directory."
_cmd "git clone $REPO_URL $DOTFILES_DIR" 3 2 "Failed to clone repository."
else
_task "Pulling latest changes"
_cmd "cd $DOTFILES_DIR" 3 2 "Failed to enter into existing dotfiles directory."
_cmd "git pull $REPO_URL main --rebase" 3 2 "Failed to pull the latest changes."
fi
}

# Load OS and setup based on detected OS
Expand All @@ -205,6 +233,8 @@ case $ID in
;;
esac

check_version

if [[ $MODE == "rolling" ]]; then
clone_repository
else
Expand Down

0 comments on commit ebf46ce

Please sign in to comment.