From 76225d1c5069992c97b0cd5605dfea2b19ceb7ba Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 11 Jan 2025 11:58:47 -0600 Subject: [PATCH] Add the `autoSync` config property to automatically sync contexts after relevant issues change. --- src/pit.nim | 22 +++++++++++++++++++++- src/pit/libpit.nim | 2 ++ src/pit/sync_pbm_vsb.nim | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/pit.nim b/src/pit.nim index 7c14dca..730bcb2 100644 --- a/src/pit.nim +++ b/src/pit.nim @@ -81,6 +81,7 @@ when isMainModule: trace "context initiated" + var updatedIssues = newSeq[Issue]() var propertiesOption = none(TableRef[string,string]) var exclPropsOption = none(TableRef[string,seq[string]]) var tagsOption = none(seq[string]) @@ -136,7 +137,7 @@ when isMainModule: else: newSeq[string]()) ctx.cfg.tasksDir.store(issue, state) - + updatedIssues.add(issue) stdout.writeLine formatIssue(issue) elif args["reorder"]: @@ -163,6 +164,7 @@ when isMainModule: for k,v in propsOption.get: issue[k] = v edit(issue) + updatedIssues.add(issue) else: let issue = ctx.cfg.tasksDir.loadIssueById(editRef) @@ -170,6 +172,7 @@ when isMainModule: for k,v in propertiesOption.get: issue[k] = v edit(issue) + updatedIssues.add(issue) elif args["tag"]: if tagsOption.isNone: raise newException(Exception, "no tags given") @@ -180,6 +183,7 @@ when isMainModule: var issue = ctx.cfg.tasksDir.loadIssueById(id) issue.tags = deduplicate(issue.tags & newTags) issue.store() + updatedIssues.add(issue) elif args["untag"]: let tagsToRemove: seq[string] = @@ -193,6 +197,7 @@ when isMainModule: proc (tag: string): bool = not tagsToRemove.anyIt(it == tag)) else: issue.tags = @[] issue.store() + updatedIssues.add(issue) elif args["start"] or args["todo-today"] or args["done"] or args["pending"] or args["todo"] or args["suspend"]: @@ -216,10 +221,12 @@ when isMainModule: let nextIssue = ctx.cfg.tasksDir.nextRecurrence(issue.getRecurrence.get, issue) ctx.cfg.tasksDir.store(nextIssue, TodoToday) info "created the next recurrence:" + updatedIssues.add(nextIssue) stdout.writeLine formatIssue(nextIssue) issue.changeState(ctx.cfg.tasksDir, targetState) + updatedIssues.add(issue) if ctx.triggerPtk or args["--ptk"]: if targetState == Current: @@ -244,6 +251,7 @@ when isMainModule: issue.setDateTime("hide-until", parseDate($args[""])) issue.store() + updatedIssues.add(issue) elif args["delegate"]: @@ -251,6 +259,7 @@ when isMainModule: issue["delegated-to"] = $args[""] issue.store() + updatedIssues.add(issue) elif args["delete"] or args["rm"]: for id in @(args[""]): @@ -263,6 +272,7 @@ when isMainModule: continue issue.delete + updatedIssues.add(issue) elif args["list"]: @@ -385,6 +395,7 @@ when isMainModule: finally: close(propIn) issue.store() + updatedIssues.add(issue) elif args["get-binary-property"]: let issue = ctx.cfg.tasksDir.loadIssueById($(args[""])) @@ -425,6 +436,15 @@ when isMainModule: sync(syncCtx, args["--dry-run"]) + # after doing stuff, sync if auto-sync is requested + if ctx.cfg.autoSync: + for syncTarget in ctx.cfg.syncTargets: + let syncCtx = initSyncContext(ctx.cfg, syncTarget) + if anyIt( + updatedIssues, + it.hasProp("context") and it["context"] == syncCtx.issueContext): + sync(syncCtx, false) + except CatchableError: fatal getCurrentExceptionMsg() debug getCurrentException().getStackTrace() diff --git a/src/pit/libpit.nim b/src/pit/libpit.nim index ffb294d..5d1e717 100644 --- a/src/pit/libpit.nim +++ b/src/pit/libpit.nim @@ -33,6 +33,7 @@ type PitConfig* = ref object tasksDir*: string contexts*: TableRef[string, string] + autoSync*: bool syncTargets*: seq[JsonNode] cfg*: CombinedConfig @@ -459,6 +460,7 @@ proc loadConfig*(args: Table[string, Value] = initTable[string, Value]()): PitCo result = PitConfig( cfg: cfg, + autoSync: parseBool(cfg.getVal("auto-sync", "false")), contexts: newTable[string,string](), tasksDir: cfg.getVal("tasks-dir", ""), syncTargets: cfg.getJson("sync-targets", newJArray()).getElems) diff --git a/src/pit/sync_pbm_vsb.nim b/src/pit/sync_pbm_vsb.nim index 905cbdd..8ac14dc 100644 --- a/src/pit/sync_pbm_vsb.nim +++ b/src/pit/sync_pbm_vsb.nim @@ -4,11 +4,11 @@ import timeutils, uuids, zero_functional import ./formatting, ./libpit type - PbmVsbSyncContext = object + PbmVsbSyncContext* = object + apiBaseUrl*: string + apiToken*: string + issueContext*: string pit: PitConfig - apiBaseUrl: string - apiToken: string - issueContext: string http: HttpClient ServerTask* = object