diff --git a/daily_notifier.nimble b/daily_notifier.nimble index a0506a9..7c0088d 100644 --- a/daily_notifier.nimble +++ b/daily_notifier.nimble @@ -4,7 +4,7 @@ version = "0.1.0" author = "Jonathan Bernard" description = "Little programs that reads my daily plan and notifies me of upcoming events." license = "MIT" -bin = @["daily_notifier", "deploy_plans_to_fastmail"] +bin = @["daily_notifier", "deploy_plans_via_ftp"] # Dependencies diff --git a/deploy_plans_via_ftp.nim b/deploy_plans_via_ftp.nim index 13a7e1c..5a7e9f9 100644 --- a/deploy_plans_via_ftp.nim +++ b/deploy_plans_via_ftp.nim @@ -1,19 +1,42 @@ -import json, nre, os, osproc, sequtils, sets, strutils, tempfile +import docopt, json, nre, os, osproc, sequtils, sets, strutils, tempfile -proc doStep(step, cmd: string): tuple[output: TaintedString, exitCode: int] = - echo "> " & cmd +proc doStep(step: string, verbose: bool, cmd: string): tuple[output: TaintedString, exitCode: int] = + if verbose: echo "> " & cmd result = execCmdEx(cmd, {poUsePath}) if result.exitCode != 0: writeLine(stderr, "Failed step [" & step & "] Received error code: " & $result.exitCode) quit(1) - echo "" + if verbose: echo "" when isMainModule: - let cfgFilePath = $getEnv("HOME") & "/.personal-planning.config.json" + let doc = """ +Usage: + deploy_plans_via_ftp [options] + +Options: + -c --config Use as the source of configuration. + -h --help Print this usage information. + -v --verbose Print verbose information about operations. + -V --version Print version information. +""" + + let args = docopt(doc, version = "deploy_plans_via_ftp 0.1.0") + + if args["--help"]: echo doc; quit() + + let verbose = args["--verbose"] + let cfgFilePaths = @[ + $getEnv("HOME") & "/.personal-planning.config.json", + if args["--config"]: $args["--config"] else:""] + + let cfgFilePath = + foldl(cfgFilePaths, if len(a) > 0: a elif existsFile(b): b else: "not-exists") if not existsFile(cfgFilePath): - quit("Cannot find config file: " & cfgFilePath, 2) + if args["--config"]: + quit("Cannot find config file: " & $args["--config"], 2) + else: quit("Cannot find config file: " & cfgFilePaths[0], 2) var cfg: JsonNode try: cfg = parseFile(cfgFilePath) @@ -54,7 +77,7 @@ when isMainModule: let tempdir = mkdtemp("personal-planning-") # Checkout personal development repository (local clone) - discard doStep("clone repo", "git clone " & repoUrl & " " & tempdir) + discard doStep("clone repo", verbose, "git clone " & repoUrl & " " & tempdir) for curDir in subDirs: let fullDirPath = tempdir & "/" & curDir @@ -63,7 +86,7 @@ when isMainModule: ftpRoot & "/" & curDir & "/' " # List the files on the server. - (output, exitCode) = doStep("read remote files (" & curDir & ")", + (output, exitCode) = doStep("read remote files (" & curDir & ")", verbose, "curl " & remoteOptions) let remoteFiles = output.splitLines @@ -87,15 +110,15 @@ when isMainModule: let filePath = fullDirPath & "/" & fileName # Compile the markdown into HTML - discard doStep("compile plan file (" & fileName & ")", + discard doStep("compile plan file (" & fileName & ")", verbose, "markdown " & filePath & ".md" & " > " & tempPath) # Concatenate the HTML template to create the final HTML - discard doStep("concatenate HTML template (" & fileName & ")", + discard doStep("concatenate HTML template (" & fileName & ")", verbose, "cat " & tempdir & "/code/start.html " & tempPath & " " & tempdir & "/code/end.html > " & filePath & ".html") # Upload the new file to FastMail - discard doStep("upload file to FastMail (" & fileName & ")", + discard doStep("upload file to FastMail (" & fileName & ")", verbose, "curl -T '" & filePath & ".html' " & remoteOptions) # Delete local temp repo