diff --git a/README.md b/README.md index 039587a..4730e8f 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,14 @@ dotman ``` Leave the rest to it. +### Commands + +| Command | Description | +|---------|-------------| +| `dotman` | Run the interactive dotfiles manager | +| `dotman version` | Show current version | +| `dotman update` | Update dotman to the latest version | + ## What else 👀 diff --git a/dotman.sh b/dotman.sh index 7745c8e..ee57e90 100755 --- a/dotman.sh +++ b/dotman.sh @@ -262,5 +262,15 @@ if [[ $1 == "version" || $1 == "--version" || $1 == "-v" ]]; then exit fi +if [[ $1 == "update" || $1 == "--update" || $1 == "-u" ]]; then + if [[ -f "$HOME/dotman/tools/update.sh" ]]; then + sh "$HOME/dotman/tools/update.sh" + else + echo "${BOLD}[❌] Update script not found${RESET}" + echo "Try reinstalling dotman." + fi + exit +fi + intro init_check diff --git a/tools/update.sh b/tools/update.sh new file mode 100644 index 0000000..67c429b --- /dev/null +++ b/tools/update.sh @@ -0,0 +1,96 @@ +#!/bin/env sh + +# Script for updating dotman +# +# This script can be run via: +# dotman update +# or directly: +# sh ~/dotman/tools/update.sh +# +# Respects the following environment variables: +# DOTMAN - path to the dotman repository folder (default: $HOME/dotman) +# REPO - name of the GitHub repo (default: Bhupesh-V/dotman) +# BRANCH - the main branch of upstream dotman repo (default: master) + +DOTMAN=${DOTMAN:-$HOME/dotman} +REPO=${REPO:-Bhupesh-V/dotman} +BRANCH=${BRANCH:-master} +REMOTE="https://github.com/${REPO}.git" + +# check if tput exists +if ! command -v tput > /dev/null 2>&1; then + BOLD="" + RESET="" + FG_GREEN="" + FG_YELLOW="" +else + BOLD=$(tput bold) + RESET=$(tput sgr0) + FG_GREEN=$(tput setaf 2) + FG_YELLOW=$(tput setaf 3) +fi + +check_dotman_dir() { + if [ ! -d "$DOTMAN" ]; then + printf "%s\n" "${BOLD}[❌] dotman is not installed at $DOTMAN${RESET}" + printf "%s\n" "Run the install script first." + exit 1 + fi +} + +check_git() { + if ! command -v git > /dev/null 2>&1; then + printf "%s\n" "${BOLD}[❌] Git is required but not installed${RESET}" + exit 1 + fi +} + +get_current_version() { + if [ -d "$DOTMAN/.git" ]; then + git -C "$DOTMAN" describe --tags --abbrev=0 2>/dev/null || echo "unknown" + else + echo "unknown" + fi +} + +get_latest_version() { + git ls-remote --refs --tags --sort='-v:refname' "$REMOTE" 2>/dev/null | head -n 1 | sed 's/.*refs\/tags\///' +} + +update_dotman() { + current_version=$(get_current_version) + latest_version=$(get_latest_version) + + printf "\n%s\n" "${BOLD}Checking for updates...${RESET}" + printf "%s\n" "Current version: ${FG_YELLOW}${current_version}${RESET}" + printf "%s\n" "Latest version: ${FG_GREEN}${latest_version}${RESET}" + + if [ "$current_version" = "$latest_version" ]; then + printf "\n%s\n" "${BOLD}${FG_GREEN}[✔️ ] dotman is already up to date!${RESET}" + return 0 + fi + + if [ -z "$latest_version" ]; then + printf "\n%s\n" "${BOLD}[❌] Could not fetch latest version${RESET}" + return 1 + fi + + printf "\n%s\n" "${BOLD}Updating dotman...${RESET}" + + git -C "$DOTMAN" fetch --tags --force + + if git -C "$DOTMAN" checkout "$latest_version" 2>/dev/null; then + printf "\n%s\n" "${BOLD}${FG_GREEN}[✔️ ] Successfully updated dotman to ${latest_version}${RESET}" + else + printf "\n%s\n" "${BOLD}[❌] Failed to update dotman${RESET}" + return 1 + fi +} + +main() { + check_dotman_dir + check_git + update_dotman +} + +main