From 1a010348165f8b64d214181c45575d068f762a01 Mon Sep 17 00:00:00 2001 From: Matt Cavanagh <1776058+Maelstromeous@users.noreply.github.com> Date: Wed, 5 Feb 2025 21:29:21 +0000 Subject: [PATCH] feat: added self update mechanism (#15) * feat: Added self update mechanism * fix: Formatting * fix: Formatting again * fix: Arghhh INDENTATION * feat: Updated readme with new command * feat: Added shortcut command * feat: Added more detail to the readme * feat: Removed the check for the last update file. It seems a bit pointless to delay checking for it, it's curling github so it's not like it's a DDOS concern, and it could be the tool is getting updated often, why delay their updates? It's also just a curl, it's very inexpensive. * feat: Removed REPO_URL, it was unused * feat: Reverted version testing change (oops) * feat: checking for updates timed at 1 hour * fix: info messages formatting --------- Co-authored-by: v4n <105587619+v4n00@users.noreply.github.com> --- README.md | 5 +++++ h2mm | 35 ++++++++++++++++++++++++++--------- install.sh | 3 +-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e8aace3..512bb56 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ h2mm - `modpack-overwrite` - Overwrite a modpack by name (or index). - `modpack-reset` - Reset all installed modpacks. - `reset` - Reset all installed mods. +- `update` - Update h2mm to latest version. - `help` - Display this help message. ### Basic usage @@ -86,6 +87,9 @@ h2mm disable -i 1 # disable mod with index 1 h2mm list ``` +#### Updating the tool +Simply run `h2mm update` and the tool will guide you through updating it to the latest version. + ## Compatibility The script is developed and tested on Arch Linux, but it should work on other Linux distributions as well. If you encounter any issues, please open an issue on the repository. @@ -118,6 +122,7 @@ You can use the short form of commands to save some time. The shortcuts are: - `mo` for `modpack-overwrite` - `mr` for `modpack-reset` - `r` for `reset` +- `up` for `update` ### Modpacks support diff --git a/h2mm b/h2mm index 3ee7274..7b00a4c 100755 --- a/h2mm +++ b/h2mm @@ -1,6 +1,6 @@ #!/usr/bin/env bash -VERSION="0.3.4" +VERSION="0.3.3" # --- Globals --- @@ -17,7 +17,6 @@ MODPACKS_DB_FILE="" LAST_CHECKED_UPDATE_FILE="${HOME}/.config/h2mm/last_update" VERSION_URL="https://raw.githubusercontent.com/v4n00/h2mm-cli/refs/heads/master/version" -REPO_URL="https://github.com/v4n00/h2mm-cli" # --- Utility Functions --- @@ -100,7 +99,7 @@ function find_game_directory() { echo "$saved_dir" return else - echo -e "${RED}!${NC} Saved game directory is invalid. Proceeding to get a new directory." >&2 + echo -e "${RED}Error${NC}: Saved game directory is invalid. Proceeding to get a new directory." >&2 fi fi @@ -174,6 +173,7 @@ function display_help() { echo " modpack-overwrite Overwrite a modpack by name (or index)." echo " modpack-reset Reset all installed modpacks." echo " reset Reset all installed mods." + echo " update Update h2mm to the latest version." echo " help Display this help message." echo "For more information on usage, use h2mm [command] --help." echo "Basic Usage:" @@ -310,11 +310,11 @@ function display_modpack_overwrite_help() { function check_for_updates() { if [[ -f "$LAST_CHECKED_UPDATE_FILE" ]]; then last_update=$(cat "$LAST_CHECKED_UPDATE_FILE") - if [[ "$(date +%Y-%m-%d)" > "$(date +%Y-%m-%d -d "$last_update + 3 days")" ]]; then + if [[ "$(date +%s)" -lt "$(date +%s -d "$last_update + 1 hour")" ]]; then return fi else - echo $(date +%Y-%m-%d) > "$LAST_CHECKED_UPDATE_FILE" + echo "$(date +%Y-%m-%dT%H:%M:%S)" > "$LAST_CHECKED_UPDATE_FILE" exit 0 fi @@ -325,14 +325,14 @@ function check_for_updates() { fi if [[ "$latest_version" != "$VERSION" ]]; then - echo -e "${RED}!${NC} A new version of h2mm is available: ${ORANGE}$VERSION${NC} -> ${GREEN}$latest_version${NC}" >&2 - echo -e "${RED}!${NC} You can download it from: $REPO_URL" >&2 + echo -e "${ORANGE}Info:${NC} A new version of h2mm is available: ${ORANGE}$VERSION${NC} -> ${GREEN}$latest_version${NC}" >&2 + echo -e "${ORANGE}Info:${NC} Run \"h2mm update\" to update." >&2 fi - echo $(date +%Y-%m-%d) > "$LAST_CHECKED_UPDATE_FILE" + echo "$(date +%Y-%m-%dT%H:%M:%S)" > "$LAST_CHECKED_UPDATE_FILE" } -# Upgade/downgrade logic +# Upgrade/downgrade logic function downgrade_mods() { local files="$1" @@ -1021,6 +1021,20 @@ function modpack_overwrite() { sed -i "/^$modpack_index,/s/DISABLED/ENABLED/" "$MODPACKS_DB_FILE" } +function self_update() { + latest_version=$(curl -sS "$VERSION_URL") + + if [[ "$latest_version" == "$VERSION" ]]; then + echo -e "h2mm is already up-to-date." >&2 + exit 0 + fi + + echo -e "Starting update script..." >&2 + + # Run the installer for the latest version + bash -c "$(curl -fsSL https://raw.githubusercontent.com/v4n00/h2mm-cli/refs/heads/master/install.sh)" +} + # --- Main --- function main() { @@ -1078,6 +1092,9 @@ function main() { version|v|-v|--version) echo "${VERSION}" ;; + update|up) + self_update + ;; help|--help|-h|h) display_help ;; diff --git a/install.sh b/install.sh index 04bf2eb..4405abb 100755 --- a/install.sh +++ b/install.sh @@ -58,8 +58,7 @@ if [[ -x "$(command -v $SCRIPT_NAME)" ]]; then if [[ $latest_major -gt $installed_major ]]; then echo -e "${ORANGE}Warning:${NC} Major version upgrade detected." - echo "${ORANGE}!${NC} Check out the changelogs here:" - echo "${ORANGE}!${NC} https://github.com/v4n00/h2mm-cli/releases" + echo "${ORANGE}Info${NC}: Check out the changelogs here -> https://github.com/v4n00/h2mm-cli/releases" echo "The script will proceed to upgrade ${SCRIPT_NAME} to avoid breaking changes." # find hd2 path