start implementing update function

This commit is contained in:
Paul Wilde 2025-01-02 13:09:59 +00:00
parent bba41d1225
commit bc815f46fd
5 changed files with 45 additions and 0 deletions

View file

@ -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

View file

@ -4,6 +4,7 @@ export command_type
type
NorgArgs* = object
show_version*: bool
update*: bool
config_file*: string
extract_destination*: string
command*: Command

View file

@ -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 != "":

28
norg/utils/update.nim Normal file
View file

@ -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()

View file

@ -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