From bc815f46fdee22e7214488ed3cc739a2f5d733f7 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 2 Jan 2025 13:09:59 +0000 Subject: [PATCH] start implementing update function --- norg/config/args.nim | 2 ++ norg/model/args_type.nim | 1 + norg/norg.nim | 3 +++ norg/utils/update.nim | 28 ++++++++++++++++++++++++++++ norg/utils/version.nim | 11 +++++++++++ 5 files changed, 45 insertions(+) create mode 100644 norg/utils/update.nim diff --git a/norg/config/args.nim b/norg/config/args.nim index 4ad5afc..76c04cc 100644 --- a/norg/config/args.nim +++ b/norg/config/args.nim @@ -14,6 +14,7 @@ proc parseArgs*() = arg("command", help="The command to run, defaults to 'create' which will perform a backup.", default=some("create")) arg("further_args", nargs = -1, help="Any further arguments to send onto borg or restic.") flag("-v","--version",help="Shows the current norg version.") + flag("-u","--update", help="Checks for a Norg update and attempts to install it.") try: var opts = p.parse(commandLineParams()) norg_args.config_file = opts.config @@ -24,6 +25,7 @@ proc parseArgs*() = norg_args.stats = opts.stats norg_args.further_args = opts.further_args norg_args.show_version = opts.version + norg_args.update = opts.update except ShortCircuit as err: if err.flag == "argparse_help": echo err.help diff --git a/norg/model/args_type.nim b/norg/model/args_type.nim index 6160345..c44e90d 100644 --- a/norg/model/args_type.nim +++ b/norg/model/args_type.nim @@ -4,6 +4,7 @@ export command_type type NorgArgs* = object show_version*: bool + update*: bool config_file*: string extract_destination*: string command*: Command diff --git a/norg/norg.nim b/norg/norg.nim index 7a8905d..3c0b3f5 100644 --- a/norg/norg.nim +++ b/norg/norg.nim @@ -4,6 +4,7 @@ import borg/borg import restic/restic import model/encryption_type import utils/actions +import utils/update import model/log_type import os @@ -19,6 +20,8 @@ proc start() = norg_config.args = norg_args if norg_args.show_version: showVersion() + elif norg_args.update: + updateNorg(VERSION) elif norg_args.command == GEN_CONF: var config_file = "norg_backup.toml" if norg_args.config_file != "": diff --git a/norg/utils/update.nim b/norg/utils/update.nim new file mode 100644 index 0000000..47c4633 --- /dev/null +++ b/norg/utils/update.nim @@ -0,0 +1,28 @@ +##curl -X 'GET' 'https://codeberg.org/api/v1/repos/pswilde/norgbackup/releases/latest -H 'accept: application/json' +## +import httpclient +import json + +import ../model/log_type +import version + +proc updateNorg*(cur_vers: string)= + initLogger(strfmt = "[$levelname] ") + let latest_url = "https://codeberg.org/api/v1/repos/pswilde/norgbackup/releases/latest" + var client = newHttpClient() + try: + let res = client.get(latest_url) + if res.code == Http200: + let j = res.body.parseJson() + let new_vers = j["tag_name"].getStr() + if new_vers.newerThan(cur_vers): + info "Update available" + else: + info "No new Update available." + else: + debug res.code + debug res.body + error "Cannot access update URL." + except: + error getCurrentExceptionMsg() + diff --git a/norg/utils/version.nim b/norg/utils/version.nim index f164364..7b09233 100644 --- a/norg/utils/version.nim +++ b/norg/utils/version.nim @@ -1,5 +1,7 @@ import parsecfg import streams +import strutils +import sequtils proc getVersion*(): string = let nimble = staticRead("../../norg.nimble") @@ -8,3 +10,12 @@ proc getVersion*(): string = echo "Compiling version: v", vers return "v" & vers +proc newerThan*(a,b: string): bool = + let new = a[1..^1].split(".").map(proc(s: string): int = s.parseInt()) + let cur = b[1..^1].split(".").map(proc(s: string): int = s.parseInt()) + echo "Latest Version: " & a + echo "Current Version: " & b + if new[0] > cur[0]: return true + elif new[1] > cur[1]: return true + elif new[2] > cur[2]: return true + return false