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"
}
},
"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",

View file

@ -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"

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 ./[exec, context]
template withTmpDir(body: untyped): untyped =
let tmpDir {.inject.} = createTempDir("oizys","")
body

View file

@ -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"