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>
This commit is contained in:
Matt Cavanagh
2025-02-05 21:29:21 +00:00
committed by GitHub
parent 83e2161456
commit 1a01034816
3 changed files with 32 additions and 11 deletions
+5
View File
@@ -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
+26 -9
View File
@@ -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
;;
+1 -2
View File
@@ -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