From c8ddbf3243227fed76b940cada0cb00846d8682a Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 2 Jan 2025 17:32:03 +0000 Subject: [PATCH] norg --update works on linux --- norg.nimble | 2 +- norg/norg.nim | 2 +- norg/utils/update.nim | 66 ++++++++++++++++++++++++++++++++++++++-- norg/utils/version.nim | 5 ++-- scripts/build.sh | 68 ++++++++++++++++++++++-------------------- 5 files changed, 103 insertions(+), 40 deletions(-) diff --git a/norg.nimble b/norg.nimble index d913048..f30058a 100644 --- a/norg.nimble +++ b/norg.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.14" +version = "0.1.13" author = "Paul Wilde" description = "A Borg Backup Orchestration Tool" license = "AGPL-3.0-or-later" diff --git a/norg/norg.nim b/norg/norg.nim index 3c0b3f5..b436fa2 100644 --- a/norg/norg.nim +++ b/norg/norg.nim @@ -21,7 +21,7 @@ proc start() = if norg_args.show_version: showVersion() elif norg_args.update: - updateNorg(VERSION) + checkUpdates(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 index 47c4633..6b4903a 100644 --- a/norg/utils/update.nim +++ b/norg/utils/update.nim @@ -2,11 +2,67 @@ ## import httpclient import json +import strformat +import strutils +import terminal +import osproc +import os import ../model/log_type 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] ") let latest_url = "https://codeberg.org/api/v1/repos/pswilde/norgbackup/releases/latest" var client = newHttpClient() @@ -16,7 +72,13 @@ proc updateNorg*(cur_vers: string)= let j = res.body.parseJson() let new_vers = j["tag_name"].getStr() 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: info "No new Update available." else: diff --git a/norg/utils/version.nim b/norg/utils/version.nim index 7b09233..c2300e2 100644 --- a/norg/utils/version.nim +++ b/norg/utils/version.nim @@ -15,7 +15,6 @@ proc newerThan*(a,b: string): bool = 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 + for i in countup(0, len(cur) - 1): + if new[i] > cur[i]: return true return false diff --git a/scripts/build.sh b/scripts/build.sh index 21e41ec..6724b9e 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -3,36 +3,38 @@ version=$(grep -i version norg.nimble | awk '{print $3}' | sed "s/\"//g" | sed "s/\./_/g") dir=$(pwd) echo Building for Linux -nimble build -d:linux -if [ $? != 1 ]; then - echo "Compressing..." - cd bin || exit - tar cvzf "norg_linux_$version.tar.gz" norg - rm norg - cd "$dir" || exit - echo Done. -fi - -echo Building for FreeBSD -nimble build -d:freebsd -if [ $? != 1 ]; then - echo "Compressing..." - cd bin || exit - tar cvzf "norg_freebsd_$version.tar.gz" norg - rm norg - cd "$dir" || exit - echo Done. -fi - -echo Building for Windows -nimble build -d:mingw -if [ $? != 1 ]; then - echo "Zipping..." - cd bin || exit - cp ../resources/windows/lib* ./ - zip "norg_windows_$version.zip" norg.exe lib* - rm ./lib* - cd "$dir" || exit - rm bin/norg.exe - echo Done. -fi +for cpu in amd64; do + nimble build -d:linux --cpu:$cpu + if [ $? != 1 ]; then + echo "Compressing..." + cd bin || exit + tar cvzf "norg_linux_$cpu-$version.tar.gz" norg + rm norg + cd "$dir" || exit + echo Done. + fi + + echo Building for FreeBSD + nimble build -d:freebsd --cpu:$cpu + if [ $? != 1 ]; then + echo "Compressing..." + cd bin || exit + tar cvzf "norg_freebsd_$cpu-$version.tar.gz" norg + rm norg + cd "$dir" || exit + echo Done. + fi + + echo Building for Windows + nimble build -d:mingw --cpu:$cpu + if [ $? != 1 ]; then + echo "Zipping..." + cd bin || exit + cp ../resources/windows/lib* ./ + zip "norg_windows_$cpu-$version.zip" norg.exe lib* + rm ./lib* + cd "$dir" || exit + rm bin/norg.exe + echo Done. + fi +done