refactor to allow for restic implementation

This commit is contained in:
Paul Wilde 2024-08-23 09:50:53 +01:00
parent ea5ecafee9
commit aad6bf38d3
10 changed files with 147 additions and 64 deletions

View file

@ -1,6 +1,5 @@
import ../model/config_type import ../model/config_type
import ../model/encryption_type import ../model/command_type
import ../model/borg_type
import ../utils/actions import ../utils/actions
import execute import execute
@ -24,11 +23,8 @@ proc checkRepo(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented." echo "Not Yet Implemented."
discard discard
proc execute*(nc: NorgConfig) = proc execute*(nc: NorgConfig, repo: Repository) =
run_actions(norg_config.actions.before_everything) case nc.args.command
for repo in nc.repositories:
run_actions(norg_config.actions.before_actions)
case nc.args.borg_cmd
of INIT: of INIT:
discard initRepo(nc, repo) discard initRepo(nc, repo)
of CREATE: of CREATE:
@ -57,6 +53,3 @@ proc execute*(nc: NorgConfig) =
run_actions(norg_config.actions.before_check) run_actions(norg_config.actions.before_check)
discard checkRepo(nc, repo) discard checkRepo(nc, repo)
run_actions(norg_config.actions.after_check) run_actions(norg_config.actions.after_check)
run_actions(norg_config.actions.after_actions)
delEncryptionPassphraseInfo()
run_actions(norg_config.actions.after_everything)

View file

@ -1,28 +1,12 @@
import strutils import strutils
import strformat import strformat
import osproc
import ../model/borg_type import ../model/command_type
import ../utils/run
export run
proc genCommand*(cmd: string, repo: string, others: seq[string]): string = proc genCommand*(cmd: string, repo: string, others: seq[string]): string =
let args = others.join(" ") let args = others.join(" ")
let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}" let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}"
return cmd return cmd
proc run*(cmd: string): int =
echo fmt"Trying to run : {cmd}"
try:
let res = execCmd(cmd)
return res
except:
echo getCurrentExceptionMsg()
return 1
proc runDiscard*(cmd: string): int =
echo fmt"Trying to run : {cmd}"
try:
let res = execCmd(cmd)
return res
except:
echo getCurrentExceptionMsg()
return 1

View file

@ -1,11 +1,11 @@
import argparse import argparse
import ../model/borg_type import ../model/command_type
type type
NorgArgs* = object NorgArgs* = object
config_file*: string config_file*: string
extract_destination*: string extract_destination*: string
borg_cmd*: BorgCommand command*: Command
others*: seq[string] others*: seq[string]
var norg_args*: NorgArgs = NorgArgs() var norg_args*: NorgArgs = NorgArgs()
@ -21,7 +21,7 @@ proc parseArgs*() =
var opts = p.parse(commandLineParams()) var opts = p.parse(commandLineParams())
norg_args.config_file = opts.config norg_args.config_file = opts.config
norg_args.extract_destination = opts.destination norg_args.extract_destination = opts.destination
norg_args.borg_cmd = opts.borg_cmd.toBorgCommand() norg_args.command = opts.borg_cmd.toCommand()
norg_args.others = opts.others norg_args.others = opts.others
except ShortCircuit as err: except ShortCircuit as err:

View file

@ -1,7 +1,8 @@
const BORG_BIN* = "borg" const BORG_BIN* = "borg"
const RESTIC_BIN* = "restic"
type type
BorgCommand* = enum Command* = enum
LIST = "list" LIST = "list"
INIT = "init", INIT = "init",
CREATE = "create", CREATE = "create",
@ -12,8 +13,8 @@ type
CHECK = "check", CHECK = "check",
COMPACT = "compact" COMPACT = "compact"
proc toBorgCommand*(str: string): BorgCommand = proc toCommand*(str: string): Command =
for cmd in BorgCommand.items: for cmd in Command.items:
if str == $cmd: return cmd if str == $cmd: return cmd

View file

@ -1,6 +1,10 @@
import tool_type
export tool_type
type type
Repository* = object Repository* = object
path*: string path*: string
label*: string label*: string
tool*: BackupTool

9
norg/model/tool_type.nim Normal file
View file

@ -0,0 +1,9 @@
type
BackupTool* = enum
BORG = "borg",
RESTIC = "restic"
proc toBackupTool*(str: string): BackupTool =
for cmd in BackupTool.items:
if str == $cmd: return cmd

View file

@ -1,13 +1,26 @@
import config/init import config/init
import config/args import config/args
import borg/borg import borg/borg
import restic/restic
import model/encryption_type
import utils/actions
proc start() = proc start() =
parseArgs() parseArgs()
norg_config = parseConfigFile(norg_args.config_file) norg_config = parseConfigFile(norg_args.config_file)
norg_config.args = norg_args norg_config.args = norg_args
if norg_config.source_directories.len > 0 and norg_config.repositories.len > 0: if norg_config.source_directories.len > 0 and norg_config.repositories.len > 0:
borg.execute(norg_config) run_actions(norg_config.actions.before_everything)
for repo in norg_config.repositories:
run_actions(norg_config.actions.before_actions)
case repo.tool
of BORG:
borg.execute(norg_config, repo)
of RESTIC:
restic.execute(norg_config, repo)
run_actions(norg_config.actions.after_actions)
run_actions(norg_config.actions.after_everything)
delEncryptionPassphraseInfo()
when isMainModule: when isMainModule:
start() start()

12
norg/restic/execute.nim Normal file
View file

@ -0,0 +1,12 @@
import strutils
import strformat
import ../model/command_type
import ../utils/run
export run
proc genCommand*(cmd: string, repo: string, others: seq[string]): string =
let args = others.join(" ")
let cmd = fmt"{RESTIC_BIN} {cmd} {repo} {args}"
return cmd

47
norg/restic/restic.nim Normal file
View file

@ -0,0 +1,47 @@
import ../model/config_type
import ../utils/actions
proc initRepo*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented"
discard
proc createBackup*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented"
discard
proc listArchives*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented"
discard
proc mountArchive*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented"
discard
proc unmountArchive*(nc: NorgConfig): int =
echo "Not Yet Implemented"
discard
proc extractArchive*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented"
discard
proc execute*(nc: NorgConfig, repo: Repository) =
case nc.args.command
of INIT:
discard initRepo(nc, repo)
of CREATE:
run_actions(norg_config.actions.before_backup)
discard createBackup(nc, repo)
run_actions(norg_config.actions.after_backup)
of LIST:
discard listArchives(nc, repo)
of MOUNT:
discard mountArchive(nc, repo)
of UMOUNT:
discard unmountArchive(nc)
of EXTRACT:
run_actions(norg_config.actions.before_extract)
discard extractArchive(nc, repo)
run_actions(norg_config.actions.after_extract)
else:
discard

20
norg/utils/run.nim Normal file
View file

@ -0,0 +1,20 @@
import strformat
import osproc
proc run*(cmd: string): int =
echo fmt"Trying to run : {cmd}"
try:
let res = execCmd(cmd)
return res
except:
echo getCurrentExceptionMsg()
return 1
proc runDiscard*(cmd: string): int =
echo fmt"Trying to run : {cmd}"
try:
let res = execCmd(cmd)
return res
except:
echo getCurrentExceptionMsg()
return 1