added generate_config command

This commit is contained in:
Paul Wilde 2024-11-25 11:15:15 +00:00
parent ce34554211
commit bb114f66e5
8 changed files with 39 additions and 2 deletions

View file

@ -64,3 +64,5 @@ proc execute*(nc: NorgConfig, repo: Repository): int {.discardable.} =
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)
else:
discard

View file

@ -3,6 +3,7 @@ import strformat
import ../model/command_type import ../model/command_type
import ../model/exclusions_type import ../model/exclusions_type
import ../model/log_type
import ../utils/run import ../utils/run
export run export run
@ -10,6 +11,7 @@ export run
proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string =
let args = further_args.join(" ") let args = further_args.join(" ")
let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}" let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}"
debug("Borg Command: " & cmd)
return cmd return cmd
proc genCreateCommand*(repo: string, sources: seq[string], stats: bool, exc: Exclusions, further_args: seq[string]): string = proc genCreateCommand*(repo: string, sources: seq[string], stats: bool, exc: Exclusions, further_args: seq[string]): string =
@ -29,10 +31,12 @@ proc genCreateCommand*(repo: string, sources: seq[string], stats: bool, exc: Exc
for pattern in exc.patterns: for pattern in exc.patterns:
cmd = fmt"{cmd} --pattern {pattern}" cmd = fmt"{cmd} --pattern {pattern}"
cmd = fmt"{cmd} {args}" cmd = fmt"{cmd} {args}"
debug("Borg Command: " & cmd)
return cmd return cmd
proc genDeleteCommand*(repo: string, archive: string, further_args: seq[string]): string = proc genDeleteCommand*(repo: string, archive: string, further_args: seq[string]): string =
let args = further_args.join(" ") let args = further_args.join(" ")
let cmd = fmt"{BORG_BIN} delete {repo} {archive} {args}" let cmd = fmt"{BORG_BIN} delete {repo} {archive} {args}"
debug("Borg Command: " & cmd)
return cmd return cmd

View file

@ -5,7 +5,7 @@ var norg_args*: NorgArgs = newNorgArgs()
proc parseArgs*() = proc parseArgs*() =
var p = newParser: var p = newParser:
help("Norg -- " & VERSION & "\r\nA portable borg backup wrapper utility.") help("Norg -- " & VERSION & "\r\nA portable borg backup and restic orchestration tool.")
option("-c", "--config", help="Config file to use.") option("-c", "--config", help="Config file to use.")
option("-d", "--destination", help="Destination when extracting backup,", required = false) option("-d", "--destination", help="Destination when extracting backup,", required = false)
option("-r", "--repository", help="Define an explicit repository to work on by either label or path.", required = false) option("-r", "--repository", help="Define an explicit repository to work on by either label or path.", required = false)

View file

@ -1,4 +1,6 @@
import parsetoml import parsetoml
import streams
import os
import ../model/config_type import ../model/config_type
import ../model/command_type import ../model/command_type
@ -86,3 +88,24 @@ proc parseConfigFile*(file: string): NorgConfig =
norg_config.parseLogging(in_conf{"logging"}) norg_config.parseLogging(in_conf{"logging"})
return norg_config return norg_config
const SAMPLE_CONFIG = staticRead("../../docs/static/downloads/norg.toml.sample")
proc generateConfigFile*(file: string) =
if fileExists(file):
echo "Sensibly not overwriting existing file: " & file
echo "Exiting"
return
echo "Generating Configuration to " & file
var r_strm = newStringStream(SAMPLE_CONFIG)
var w_strm = newFileStream(file, fmWrite)
var line = ""
if r_strm != nil and w_strm != nil:
while r_strm.readLine(line):
echo line
w_strm.writeLine("#" & line)
r_strm.close()
w_strm.close()
echo "Done. Now edit " & file & " to your requirements and run:"
echo "norg -c " & file & " init"
else:
echo "Error writing sample config file"

View file

@ -13,6 +13,7 @@ type
DELETE = "delete" DELETE = "delete"
CHECK = "check", CHECK = "check",
COMPACT = "compact" COMPACT = "compact"
GEN_CONF = "generate_config"
var BORG_BIN* = "borg" var BORG_BIN* = "borg"
var RESTIC_BIN* = "restic" var RESTIC_BIN* = "restic"

View file

@ -19,6 +19,11 @@ 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.command == GEN_CONF:
var config_file = "norg_backup.toml"
if norg_args.config_file != "":
config_file = norg_args.config_file
generateConfigFile(config_file)
elif norg_args.config_file != "": elif norg_args.config_file != "":
norg_config = parseConfigFile(norg_args.config_file) norg_config = parseConfigFile(norg_args.config_file)
echo norg_config.log_info.level echo norg_config.log_info.level

View file

@ -6,6 +6,7 @@ proc run*(cmd: string): int =
debug fmt"Trying to run : {cmd}" debug fmt"Trying to run : {cmd}"
try: try:
let res = execCmd(cmd) let res = execCmd(cmd)
debug("Run Command Exist Code: " & $res)
return res return res
except: except:
error getCurrentExceptionMsg() error getCurrentExceptionMsg()
@ -15,6 +16,7 @@ proc runDiscard*(cmd: string): int =
debug fmt"Trying to run : {cmd}" debug fmt"Trying to run : {cmd}"
try: try:
let res = execCmd(cmd) let res = execCmd(cmd)
debug("Run Command Exist Code: " & $res)
return res return res
except: except:
error getCurrentExceptionMsg() error getCurrentExceptionMsg()

View file

@ -1,6 +1,6 @@
# Norg # Norg
A simple, portable, wrapper for the [borg backup](https://www.borgbackup.org) and [restic](https://restic.net) utilities written in Nim. A simple, portable, orchestration tool for the [borg backup](https://www.borgbackup.org) and [restic](https://restic.net) utilities written in Nim.
Full documentation on the [Norg Backup Website](https://norgbackup.net/) Full documentation on the [Norg Backup Website](https://norgbackup.net/)