make utils narinfo more helpful

This commit is contained in:
Daylin Morgan 2025-01-29 11:44:26 -06:00
parent eb8ec7dabf
commit 46b1a2cdc5
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 30 additions and 10 deletions

View file

@ -185,6 +185,8 @@ hwylCli:
? "url of nix binary cache, can be repeated"
T seq[string]
run:
if installables.len == 0:
fatalQuit "expected at least one positional argument"
checkForCache(installables, cache)
[lock]

View file

@ -1,6 +1,6 @@
import std/[strformat, strutils, osproc, sugar, httpclient]
import std/[strformat, strutils, osproc, sugar, httpclient, terminal, wordwrap]
import hwylterm
import ./[nix, exec,logging]
import ./[nix, exec, logging, context]
# TODO: refactor runCmdCaptWithSpinner so it works in getBuildHash
@ -44,18 +44,21 @@ proc getCaches(): seq[string] =
fatalQuit "error running `nix config show`"
proc hasNarinfo*(cache: string, path: string): bool =
proc hasNarinfo*(cache: string, path: string): tuple[exists:bool, narinfo:string] =
debug fmt"checking {cache} for {path}"
let
hash = narHash(path)
url = cache & "/" & hash & ".narinfo"
var client = newHttpClient()
try:
let client = newHttpClient()
result = client.head(url).code == Http200
except:
result = false
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)
finally:
client.close()
proc prettyDerivation(path: string): BbString =
let drv = path.toDerivation()
@ -64,6 +67,19 @@ proc prettyDerivation(path: string): BbString =
result.add " "
result.add drv.hash.bb("faint")
proc showNarInfo(s: string): BbString =
let maxWidth = terminalWidth()
result.add "narinfo:"
for line in s.splitLines():
let
ss = line.split(": ", maxsplit = 1)
(k, v) = (ss[0], ss[1])
result.add bbfmt("\n[b]{k}[/]: ")
if (len(v) - len(k) + 2) > maxWidth:
result.add "\n " & wrapWords(v, maxLineWidth = maxWidth - 2, newLine="\n ")
else:
result.add v
proc checkForCache*(installables: seq[string], caches: seq[string]) =
let caches =
if caches.len > 0: caches
@ -77,10 +93,12 @@ proc checkForCache*(installables: seq[string], caches: seq[string]) =
for name, path in outs:
var found = false
for cache in caches:
if hasNarinfo(cache, path):
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: