start implementing update function
This commit is contained in:
parent
bba41d1225
commit
bc815f46fd
5 changed files with 45 additions and 0 deletions
|
@ -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("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.")
|
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("-v","--version",help="Shows the current norg version.")
|
||||||
|
flag("-u","--update", help="Checks for a Norg update and attempts to install it.")
|
||||||
try:
|
try:
|
||||||
var opts = p.parse(commandLineParams())
|
var opts = p.parse(commandLineParams())
|
||||||
norg_args.config_file = opts.config
|
norg_args.config_file = opts.config
|
||||||
|
@ -24,6 +25,7 @@ proc parseArgs*() =
|
||||||
norg_args.stats = opts.stats
|
norg_args.stats = opts.stats
|
||||||
norg_args.further_args = opts.further_args
|
norg_args.further_args = opts.further_args
|
||||||
norg_args.show_version = opts.version
|
norg_args.show_version = opts.version
|
||||||
|
norg_args.update = opts.update
|
||||||
except ShortCircuit as err:
|
except ShortCircuit as err:
|
||||||
if err.flag == "argparse_help":
|
if err.flag == "argparse_help":
|
||||||
echo err.help
|
echo err.help
|
||||||
|
|
|
@ -4,6 +4,7 @@ export command_type
|
||||||
type
|
type
|
||||||
NorgArgs* = object
|
NorgArgs* = object
|
||||||
show_version*: bool
|
show_version*: bool
|
||||||
|
update*: bool
|
||||||
config_file*: string
|
config_file*: string
|
||||||
extract_destination*: string
|
extract_destination*: string
|
||||||
command*: Command
|
command*: Command
|
||||||
|
|
|
@ -4,6 +4,7 @@ import borg/borg
|
||||||
import restic/restic
|
import restic/restic
|
||||||
import model/encryption_type
|
import model/encryption_type
|
||||||
import utils/actions
|
import utils/actions
|
||||||
|
import utils/update
|
||||||
import model/log_type
|
import model/log_type
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -19,6 +20,8 @@ proc start() =
|
||||||
norg_config.args = norg_args
|
norg_config.args = norg_args
|
||||||
if norg_args.show_version:
|
if norg_args.show_version:
|
||||||
showVersion()
|
showVersion()
|
||||||
|
elif norg_args.update:
|
||||||
|
updateNorg(VERSION)
|
||||||
elif norg_args.command == GEN_CONF:
|
elif norg_args.command == GEN_CONF:
|
||||||
var config_file = "norg_backup.toml"
|
var config_file = "norg_backup.toml"
|
||||||
if norg_args.config_file != "":
|
if norg_args.config_file != "":
|
||||||
|
|
28
norg/utils/update.nim
Normal file
28
norg/utils/update.nim
Normal 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()
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import parsecfg
|
import parsecfg
|
||||||
import streams
|
import streams
|
||||||
|
import strutils
|
||||||
|
import sequtils
|
||||||
|
|
||||||
proc getVersion*(): string =
|
proc getVersion*(): string =
|
||||||
let nimble = staticRead("../../norg.nimble")
|
let nimble = staticRead("../../norg.nimble")
|
||||||
|
@ -8,3 +10,12 @@ proc getVersion*(): string =
|
||||||
echo "Compiling version: v", vers
|
echo "Compiling version: v", vers
|
||||||
return "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
|
||||||
|
|
Loading…
Reference in a new issue