Skip to content

Commit 197be96

Browse files
committed
feat: Add update command
1 parent 29aadce commit 197be96

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ If you want to use the default names for the installed binaries, you can use the
5959
gah install getsops/sops --use-default-names
6060
```
6161

62+
### Update gah
63+
64+
```sh
65+
gah update
66+
```
67+
6268
## Examples
6369

6470
### Install latest version of gh (GitHub CLI)

gah

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ function get_known_alias() {
230230
jq -r --arg x "$1" '.aliases[$x]' "$(get_db_path)"
231231
}
232232

233+
#endregion
234+
#--------------------------------------------------
235+
#region Other functions
236+
237+
function semver_to_number() {
238+
if [[ "$1" =~ ^v ]]; then
239+
local version=${1:1}
240+
else
241+
local version="$1"
242+
fi
243+
local major=$(echo "$version" | cut -d '.' -f 1)
244+
local minor=$(echo "$version" | cut -d '.' -f 2)
245+
local patch=$(echo "$version" | cut -d '.' -f 3)
246+
echo $((major * 1000000 + minor * 1000 + patch))
247+
}
248+
233249
#endregion
234250
#--------------------------------------------------
235251
#region Command functions
@@ -238,6 +254,7 @@ function command_help() {
238254
echo "gah"
239255
echo " install <github_owner/github_repo_name | known_alias> [--tag=<git_tag>] [--use-default-names]"
240256
echo " aliases <show | refresh>"
257+
echo " update"
241258
echo " help"
242259
echo " version"
243260
exit 0
@@ -334,11 +351,49 @@ function command_install() {
334351
print_green "Done!"
335352
}
336353

337-
function command_show_aliases() {
354+
function command_aliases_show() {
338355
echo "Known aliases:"
339356
jq -r '.aliases' "$(get_db_path)"
340357
}
341358

359+
function command_aliases_refresh() {
360+
print_blue "Refreshing aliases"
361+
fetch_db
362+
print_green "Done!"
363+
}
364+
365+
function command_update() {
366+
local gah_repo="marverix/gah"
367+
local script_realpath=$(realpath "$0")
368+
369+
# Check if user has write permissions script_realpath
370+
if [[ ! -w "$script_realpath" ]]; then
371+
throw_error 15 "You don't have write permissions to $script_realpath.\nPlease run the script with sudo or change the permissions."
372+
fi
373+
374+
# Check gah latest tag
375+
print_blue "Checking latest gah release..."
376+
local tag=$(curl -s "$(get_fetch_release_info_url $gah_repo)" | jq -r '.tag_name')
377+
378+
# Compare versions
379+
local new_number=$(semver_to_number "$tag")
380+
local current_number=$(semver_to_number "$VERSION")
381+
if [[ "$new_number" -le "$current_number" ]]; then
382+
print_yellow "You are already using the latest version ($VERSION)."
383+
print_green "Done!"
384+
exit 0
385+
else
386+
print_yellow "Updating from $VERSION to $tag"
387+
fi
388+
389+
# Download gah! script
390+
curl -sL https://raw.githubusercontent.com/$gah_repo/refs/tags/$tag/gah -o "$script_realpath"
391+
chmod +x "$script_realpath"
392+
print_green "OK"
393+
394+
print_green "Done!"
395+
}
396+
342397
#endregion
343398
#--------------------------------------------------
344399

@@ -397,17 +452,20 @@ function main() {
397452

398453
elif [[ "$1" == "aliases" ]]; then
399454
if [[ "$2" == "show" ]]; then
400-
command_show_aliases
455+
command_aliases_show
401456

402457
elif [[ "$2" == "refresh" ]]; then
403-
fetch_db
458+
command_aliases_refresh
404459

405460
else
406461
throw_error 4 "Unknown subcommand.\n$HELP_STRING"
407462
fi
408463

464+
elif [[ "$1" == "update" ]]; then
465+
command_update
466+
409467
else
410-
throw_error 5 "Unknown command '$command'.\n$HELP_STRING"
468+
throw_error 5 "Unknown command '$1'.\n$HELP_STRING"
411469
fi
412470
}
413471

0 commit comments

Comments
 (0)