diff --git a/pkgs/oizys/nimble.lock b/pkgs/oizys/nimble.lock index 0d0f2d1..43e4254 100644 --- a/pkgs/oizys/nimble.lock +++ b/pkgs/oizys/nimble.lock @@ -21,6 +21,16 @@ "sha1": "6aeb83e7481ca8686396a568096054bc668294df" } }, + "resultz": { + "version": "0.1.0", + "vcsRevision": "9708a3f10bf3fa54907f52c28ede41cab6838aa6", + "url": "https://github.com/daylinmorgan/resultz", + "downloadMethod": "git", + "dependencies": [], + "checksums": { + "sha1": "edf54faf880744d61c48900273d2a55ce1d89c36" + } + }, "zippy": { "version": "0.10.16", "vcsRevision": "a99f6a7d8a8e3e0213b3cad0daf0ea974bf58e3f", diff --git a/pkgs/oizys/oizys.nimble b/pkgs/oizys/oizys.nimble index 56cf783..f59bfd0 100644 --- a/pkgs/oizys/oizys.nimble +++ b/pkgs/oizys/oizys.nimble @@ -14,4 +14,4 @@ requires "nim >= 2.0.8" requires "jsony" requires "zippy" requires "https://github.com/daylinmorgan/hwylterm#6a6bd269" - +requires "https://github.com/daylinmorgan/resultz" diff --git a/pkgs/oizys/oizys/utils.nim b/pkgs/oizys/oizys/utils.nim new file mode 100644 index 0000000..e69de29 diff --git a/pkgs/oizys/src/oizys/github.nim b/pkgs/oizys/src/oizys/github.nim index 2cdf5ea..fe96f80 100644 --- a/pkgs/oizys/src/oizys/github.nim +++ b/pkgs/oizys/src/oizys/github.nim @@ -2,6 +2,7 @@ import std/[httpclient,logging, os, strformat, strutils, json, tables, tempfiles import jsony, hwylterm, hwylterm/logging, zippy/ziparchives import ./[exec, context] + template withTmpDir(body: untyped): untyped = let tmpDir {.inject.} = createTempDir("oizys","") body diff --git a/pkgs/oizys/src/oizys/utils.nim b/pkgs/oizys/src/oizys/utils.nim index 82a88e9..77caece 100644 --- a/pkgs/oizys/src/oizys/utils.nim +++ b/pkgs/oizys/src/oizys/utils.nim @@ -1,8 +1,7 @@ import std/[strformat, strutils, osproc, sugar, httpclient, terminal, wordwrap] -import hwylterm +import hwylterm,resultz import ./[nix, exec, logging] - # TODO: refactor runCmdCaptWithSpinner so it works in getBuildHash proc checkBuild(installable: string): tuple[stdout: string, stderr: string] = var @@ -43,8 +42,7 @@ proc getCaches(): seq[string] = echo formatSubprocessError(output) fatalQuit "error running `nix config show`" - -proc hasNarinfo*(cache: string, path: string): tuple[exists:bool, narinfo:string] = +proc hasNarinfo*(cache: string, path: string): Opt[string] = debug fmt"checking {cache} for {path}" let hash = narHash(path) @@ -53,22 +51,15 @@ proc hasNarinfo*(cache: string, path: string): tuple[exists:bool, narinfo:string var client = newHttpClient() try: let res = client.get(url) - result.exists = res.code == Http200 - result.narinfo = res.body.strip() - # if result and getVerbosity() > 0: - # info "narinfo:\n" & indent(res.body.strip(),2) + if res.code == Http200: + result.ok res.body.strip() finally: client.close() proc prettyDerivation(path: string): BbString = - let drv = path.toDerivation() const maxLen = 40 - if drv.name.len < maxLen: - result.add drv.name - else: - result.add drv.name.trunc(maxLen) - result.add " " - result.add drv.hash.bb("faint") + let drv = path.toDerivation() + drv.name.trunc(maxLen) & " " & drv.hash.bb("faint") proc showNarInfo(s: string): BbString = let maxWidth = terminalWidth() @@ -83,6 +74,18 @@ proc showNarInfo(s: string): BbString = else: result.add v +# TODO: replace this with 'match Some() later for all Opts + +proc searchCaches(caches: seq[string], path: string): bool = + ## search all caches until a match is found + info "searching for: " & prettyDerivation(path) + for cache in caches: + match hasNarinfo(cache, path): + Ok(narinfo): + info fmt"exists in {cache}" + debug showNarinfo(narinfo) + Err(): discard + proc checkForCache*(installables: seq[string], caches: seq[string]) = let caches = if caches.len > 0: caches @@ -94,17 +97,5 @@ proc checkForCache*(installables: seq[string], caches: seq[string]) = {name: drv.outputs["out"].path} for name, path in outs: - var found = false - for cache in caches: - let (exists, narinfo) = hasNarinfo(cache, path) - if exists: - found = true - info prettyDerivation(path) - info fmt"exists in {cache}" - debug showNarinfo(narinfo) - break - - if not found: - info fmt"failed to find:" - info prettyDerivation(path) - + if not searchCaches(caches, path): + error "did not find above 'narinfo' in any caches"