play with results

This commit is contained in:
Daylin Morgan 2025-01-31 14:26:49 -06:00
parent 5dc2969a2b
commit 01a051eefa
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 32 additions and 30 deletions

View file

@ -21,6 +21,16 @@
"sha1": "6aeb83e7481ca8686396a568096054bc668294df" "sha1": "6aeb83e7481ca8686396a568096054bc668294df"
} }
}, },
"resultz": {
"version": "0.1.0",
"vcsRevision": "9708a3f10bf3fa54907f52c28ede41cab6838aa6",
"url": "https://github.com/daylinmorgan/resultz",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "edf54faf880744d61c48900273d2a55ce1d89c36"
}
},
"zippy": { "zippy": {
"version": "0.10.16", "version": "0.10.16",
"vcsRevision": "a99f6a7d8a8e3e0213b3cad0daf0ea974bf58e3f", "vcsRevision": "a99f6a7d8a8e3e0213b3cad0daf0ea974bf58e3f",

View file

@ -14,4 +14,4 @@ requires "nim >= 2.0.8"
requires "jsony" requires "jsony"
requires "zippy" requires "zippy"
requires "https://github.com/daylinmorgan/hwylterm#6a6bd269" requires "https://github.com/daylinmorgan/hwylterm#6a6bd269"
requires "https://github.com/daylinmorgan/resultz"

View file

View file

@ -2,6 +2,7 @@ import std/[httpclient,logging, os, strformat, strutils, json, tables, tempfiles
import jsony, hwylterm, hwylterm/logging, zippy/ziparchives import jsony, hwylterm, hwylterm/logging, zippy/ziparchives
import ./[exec, context] import ./[exec, context]
template withTmpDir(body: untyped): untyped = template withTmpDir(body: untyped): untyped =
let tmpDir {.inject.} = createTempDir("oizys","") let tmpDir {.inject.} = createTempDir("oizys","")
body body

View file

@ -1,8 +1,7 @@
import std/[strformat, strutils, osproc, sugar, httpclient, terminal, wordwrap] import std/[strformat, strutils, osproc, sugar, httpclient, terminal, wordwrap]
import hwylterm import hwylterm,resultz
import ./[nix, exec, logging] import ./[nix, exec, logging]
# TODO: refactor runCmdCaptWithSpinner so it works in getBuildHash # TODO: refactor runCmdCaptWithSpinner so it works in getBuildHash
proc checkBuild(installable: string): tuple[stdout: string, stderr: string] = proc checkBuild(installable: string): tuple[stdout: string, stderr: string] =
var var
@ -43,8 +42,7 @@ proc getCaches(): seq[string] =
echo formatSubprocessError(output) echo formatSubprocessError(output)
fatalQuit "error running `nix config show`" fatalQuit "error running `nix config show`"
proc hasNarinfo*(cache: string, path: string): Opt[string] =
proc hasNarinfo*(cache: string, path: string): tuple[exists:bool, narinfo:string] =
debug fmt"checking {cache} for {path}" debug fmt"checking {cache} for {path}"
let let
hash = narHash(path) hash = narHash(path)
@ -53,22 +51,15 @@ proc hasNarinfo*(cache: string, path: string): tuple[exists:bool, narinfo:string
var client = newHttpClient() var client = newHttpClient()
try: try:
let res = client.get(url) let res = client.get(url)
result.exists = res.code == Http200 if res.code == Http200:
result.narinfo = res.body.strip() result.ok res.body.strip()
# if result and getVerbosity() > 0:
# info "narinfo:\n" & indent(res.body.strip(),2)
finally: finally:
client.close() client.close()
proc prettyDerivation(path: string): BbString = proc prettyDerivation(path: string): BbString =
let drv = path.toDerivation()
const maxLen = 40 const maxLen = 40
if drv.name.len < maxLen: let drv = path.toDerivation()
result.add drv.name drv.name.trunc(maxLen) & " " & drv.hash.bb("faint")
else:
result.add drv.name.trunc(maxLen)
result.add " "
result.add drv.hash.bb("faint")
proc showNarInfo(s: string): BbString = proc showNarInfo(s: string): BbString =
let maxWidth = terminalWidth() let maxWidth = terminalWidth()
@ -83,6 +74,18 @@ proc showNarInfo(s: string): BbString =
else: else:
result.add v 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]) = proc checkForCache*(installables: seq[string], caches: seq[string]) =
let caches = let caches =
if caches.len > 0: caches if caches.len > 0: caches
@ -94,17 +97,5 @@ proc checkForCache*(installables: seq[string], caches: seq[string]) =
{name: drv.outputs["out"].path} {name: drv.outputs["out"].path}
for name, path in outs: for name, path in outs:
var found = false if not searchCaches(caches, path):
for cache in caches: error "did not find above 'narinfo' in any 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)