@@ -230,6 +230,22 @@ function get_known_alias() {
230
230
jq -r --arg x " $1 " ' .aliases[$x]' " $( get_db_path) "
231
231
}
232
232
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
+
233
249
# endregion
234
250
# --------------------------------------------------
235
251
# region Command functions
@@ -238,6 +254,7 @@ function command_help() {
238
254
echo " gah"
239
255
echo " install <github_owner/github_repo_name | known_alias> [--tag=<git_tag>] [--use-default-names]"
240
256
echo " aliases <show | refresh>"
257
+ echo " update"
241
258
echo " help"
242
259
echo " version"
243
260
exit 0
@@ -334,11 +351,49 @@ function command_install() {
334
351
print_green " Done!"
335
352
}
336
353
337
- function command_show_aliases () {
354
+ function command_aliases_show () {
338
355
echo " Known aliases:"
339
356
jq -r ' .aliases' " $( get_db_path) "
340
357
}
341
358
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
+
342
397
# endregion
343
398
# --------------------------------------------------
344
399
@@ -397,17 +452,20 @@ function main() {
397
452
398
453
elif [[ " $1 " == " aliases" ]]; then
399
454
if [[ " $2 " == " show" ]]; then
400
- command_show_aliases
455
+ command_aliases_show
401
456
402
457
elif [[ " $2 " == " refresh" ]]; then
403
- fetch_db
458
+ command_aliases_refresh
404
459
405
460
else
406
461
throw_error 4 " Unknown subcommand.\n$HELP_STRING "
407
462
fi
408
463
464
+ elif [[ " $1 " == " update" ]]; then
465
+ command_update
466
+
409
467
else
410
- throw_error 5 " Unknown command '$command '.\n$HELP_STRING "
468
+ throw_error 5 " Unknown command '$1 '.\n$HELP_STRING "
411
469
fi
412
470
}
413
471
0 commit comments