diff --git a/norg/borg/borg.nim b/norg/borg/borg.nim index 4f192f0..a99a64c 100644 --- a/norg/borg/borg.nim +++ b/norg/borg/borg.nim @@ -1,6 +1,5 @@ import ../model/config_type -import ../model/encryption_type -import ../model/borg_type +import ../model/command_type import ../utils/actions import execute @@ -24,39 +23,33 @@ proc checkRepo(nc: NorgConfig, repo: Repository): int = echo "Not Yet Implemented." discard -proc execute*(nc: NorgConfig) = - run_actions(norg_config.actions.before_everything) - for repo in nc.repositories: - run_actions(norg_config.actions.before_actions) - case nc.args.borg_cmd - 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) - of PRUNE: - run_actions(norg_config.actions.before_prune) - discard pruneRepo(nc, repo) - run_actions(norg_config.actions.after_prune) - of COMPACT: - run_actions(norg_config.actions.before_compact) - discard compactRepo(nc, repo) - run_actions(norg_config.actions.after_compact) - of CHECK: - run_actions(norg_config.actions.before_check) - discard checkRepo(nc, repo) - run_actions(norg_config.actions.after_check) - run_actions(norg_config.actions.after_actions) - delEncryptionPassphraseInfo() - run_actions(norg_config.actions.after_everything) +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) + of PRUNE: + run_actions(norg_config.actions.before_prune) + discard pruneRepo(nc, repo) + run_actions(norg_config.actions.after_prune) + of COMPACT: + run_actions(norg_config.actions.before_compact) + discard compactRepo(nc, repo) + run_actions(norg_config.actions.after_compact) + of CHECK: + run_actions(norg_config.actions.before_check) + discard checkRepo(nc, repo) + run_actions(norg_config.actions.after_check) diff --git a/norg/borg/execute.nim b/norg/borg/execute.nim index 099ac7a..999a3bb 100644 --- a/norg/borg/execute.nim +++ b/norg/borg/execute.nim @@ -1,28 +1,12 @@ import strutils 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 = let args = others.join(" ") let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}" 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 diff --git a/norg/config/args.nim b/norg/config/args.nim index 02008db..278644b 100644 --- a/norg/config/args.nim +++ b/norg/config/args.nim @@ -1,11 +1,11 @@ import argparse -import ../model/borg_type +import ../model/command_type type NorgArgs* = object config_file*: string extract_destination*: string - borg_cmd*: BorgCommand + command*: Command others*: seq[string] var norg_args*: NorgArgs = NorgArgs() @@ -21,7 +21,7 @@ proc parseArgs*() = var opts = p.parse(commandLineParams()) norg_args.config_file = opts.config 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 except ShortCircuit as err: diff --git a/norg/model/borg_type.nim b/norg/model/command_type.nim similarity index 69% rename from norg/model/borg_type.nim rename to norg/model/command_type.nim index dcb57ed..82e7ae7 100644 --- a/norg/model/borg_type.nim +++ b/norg/model/command_type.nim @@ -1,7 +1,8 @@ const BORG_BIN* = "borg" +const RESTIC_BIN* = "restic" type - BorgCommand* = enum + Command* = enum LIST = "list" INIT = "init", CREATE = "create", @@ -12,8 +13,8 @@ type CHECK = "check", COMPACT = "compact" -proc toBorgCommand*(str: string): BorgCommand = - for cmd in BorgCommand.items: +proc toCommand*(str: string): Command = + for cmd in Command.items: if str == $cmd: return cmd diff --git a/norg/model/repository_type.nim b/norg/model/repository_type.nim index b8d2465..cd1bfd7 100644 --- a/norg/model/repository_type.nim +++ b/norg/model/repository_type.nim @@ -1,6 +1,10 @@ +import tool_type + +export tool_type type Repository* = object path*: string label*: string + tool*: BackupTool diff --git a/norg/model/tool_type.nim b/norg/model/tool_type.nim new file mode 100644 index 0000000..7604a12 --- /dev/null +++ b/norg/model/tool_type.nim @@ -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 diff --git a/norg/norg.nim b/norg/norg.nim index 7198ff8..6b51c98 100644 --- a/norg/norg.nim +++ b/norg/norg.nim @@ -1,13 +1,26 @@ import config/init import config/args import borg/borg +import restic/restic +import model/encryption_type +import utils/actions proc start() = parseArgs() norg_config = parseConfigFile(norg_args.config_file) norg_config.args = norg_args 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: start() diff --git a/norg/restic/execute.nim b/norg/restic/execute.nim new file mode 100644 index 0000000..ad2deaf --- /dev/null +++ b/norg/restic/execute.nim @@ -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 + diff --git a/norg/restic/restic.nim b/norg/restic/restic.nim new file mode 100644 index 0000000..4a6c7b2 --- /dev/null +++ b/norg/restic/restic.nim @@ -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 diff --git a/norg/utils/run.nim b/norg/utils/run.nim new file mode 100644 index 0000000..cc22ccf --- /dev/null +++ b/norg/utils/run.nim @@ -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