norg --update works on linux

This commit is contained in:
Paul Wilde 2025-01-02 17:32:03 +00:00
parent bc815f46fd
commit c8ddbf3243
5 changed files with 103 additions and 40 deletions

View file

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.14" version = "0.1.13"
author = "Paul Wilde" author = "Paul Wilde"
description = "A Borg Backup Orchestration Tool" description = "A Borg Backup Orchestration Tool"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

View file

@ -21,7 +21,7 @@ proc start() =
if norg_args.show_version: if norg_args.show_version:
showVersion() showVersion()
elif norg_args.update: elif norg_args.update:
updateNorg(VERSION) checkUpdates(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 != "":

View file

@ -2,11 +2,67 @@
## ##
import httpclient import httpclient
import json import json
import strformat
import strutils
import terminal
import osproc
import os
import ../model/log_type import ../model/log_type
import version import version
proc updateNorg*(cur_vers: string)= const sh = """
#!/usr/bin/env sh
mv {new} {old}
{old} --version
rm -r tmp
"""
proc extractUpdate(name, file: string) =
var client = newHttpClient()
try:
createDir("tmp")
client.downloadFile(file,fmt"tmp/{name}")
let cwd = getAppDir()
let cwf = getAppFilename()
case hostOS
of "linux":
let ok = osproc.execCmd(fmt"tar xf tmp/{name} -C tmp/")
if ok == 0:
copyFile(cwf,fmt"{cwd}/norg_backup")
let content = sh.replace("{new}", "tmp/norg").replace("{old}", cwf)
writeFile("tmp/norg_update", content)
discard osproc.execCmd("sh tmp/norg_update")
quit(0)
of "windows":
let ok = osproc.execCmd(fmt"decompress tmp/{name} -C tmp/")
if ok == 0:
copyFile(cwf,fmt"{cwd}/norg_backup")
let content = sh.replace("{new}", "tmp/norg").replace("{old}", cwf)
writeFile("tmp/norg_update", content)
discard osproc.execCmd("sh tmp/norg_update")
quit(0)
except:
error getCurrentExceptionMsg()
finally:
removeDir("tmp")
proc updateNorg(version: string, j: JsonNode) =
let newvers = version[1..^1].replace(".","_")
#let check_file = fmt"norg_{hostOS}_{hostCPU}-{newvers}.tar.gz"
let check_file = fmt"norg_{hostOS}_{newvers}.tar.gz"
var update_found = false
for asset in j["assets"].getElems():
let name = asset["name"].getStr()
if checkfile == name:
let file = asset["browser_download_url"].getStr()
update_found = true
extractUpdate(name,file)
if not update_found:
info "Unable to find update file for your OS and architecture."
info "Please check https://codeberg.org/pswilde/norgbackup/releases/latest for more information"
proc checkUpdates*(cur_vers: string)=
initLogger(strfmt = "[$levelname] ") initLogger(strfmt = "[$levelname] ")
let latest_url = "https://codeberg.org/api/v1/repos/pswilde/norgbackup/releases/latest" let latest_url = "https://codeberg.org/api/v1/repos/pswilde/norgbackup/releases/latest"
var client = newHttpClient() var client = newHttpClient()
@ -16,7 +72,13 @@ proc updateNorg*(cur_vers: string)=
let j = res.body.parseJson() let j = res.body.parseJson()
let new_vers = j["tag_name"].getStr() let new_vers = j["tag_name"].getStr()
if new_vers.newerThan(cur_vers): if new_vers.newerThan(cur_vers):
info "Update available" info "An update is available, would you like to update? (y/N)"
let resp = getch()
case resp
of 'y', 'Y':
new_vers.updateNorg(j)
else:
info "Not updating"
else: else:
info "No new Update available." info "No new Update available."
else: else:

View file

@ -15,7 +15,6 @@ proc newerThan*(a,b: string): bool =
let cur = b[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 "Latest Version: " & a
echo "Current Version: " & b echo "Current Version: " & b
if new[0] > cur[0]: return true for i in countup(0, len(cur) - 1):
elif new[1] > cur[1]: return true if new[i] > cur[i]: return true
elif new[2] > cur[2]: return true
return false return false

View file

@ -3,36 +3,38 @@
version=$(grep -i version norg.nimble | awk '{print $3}' | sed "s/\"//g" | sed "s/\./_/g") version=$(grep -i version norg.nimble | awk '{print $3}' | sed "s/\"//g" | sed "s/\./_/g")
dir=$(pwd) dir=$(pwd)
echo Building for Linux echo Building for Linux
nimble build -d:linux for cpu in amd64; do
if [ $? != 1 ]; then nimble build -d:linux --cpu:$cpu
if [ $? != 1 ]; then
echo "Compressing..." echo "Compressing..."
cd bin || exit cd bin || exit
tar cvzf "norg_linux_$version.tar.gz" norg tar cvzf "norg_linux_$cpu-$version.tar.gz" norg
rm norg rm norg
cd "$dir" || exit cd "$dir" || exit
echo Done. echo Done.
fi fi
echo Building for FreeBSD echo Building for FreeBSD
nimble build -d:freebsd nimble build -d:freebsd --cpu:$cpu
if [ $? != 1 ]; then if [ $? != 1 ]; then
echo "Compressing..." echo "Compressing..."
cd bin || exit cd bin || exit
tar cvzf "norg_freebsd_$version.tar.gz" norg tar cvzf "norg_freebsd_$cpu-$version.tar.gz" norg
rm norg rm norg
cd "$dir" || exit cd "$dir" || exit
echo Done. echo Done.
fi fi
echo Building for Windows echo Building for Windows
nimble build -d:mingw nimble build -d:mingw --cpu:$cpu
if [ $? != 1 ]; then if [ $? != 1 ]; then
echo "Zipping..." echo "Zipping..."
cd bin || exit cd bin || exit
cp ../resources/windows/lib* ./ cp ../resources/windows/lib* ./
zip "norg_windows_$version.zip" norg.exe lib* zip "norg_windows_$cpu-$version.zip" norg.exe lib*
rm ./lib* rm ./lib*
cd "$dir" || exit cd "$dir" || exit
rm bin/norg.exe rm bin/norg.exe
echo Done. echo Done.
fi fi
done