From 46b1a2cdc5bd234ea3e80ed95ce7cf6859898c9e Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 29 Jan 2025 11:44:26 -0600 Subject: [PATCH] make utils narinfo more helpful --- pkgs/oizys/src/oizys.nim | 2 ++ pkgs/oizys/src/oizys/utils.nim | 38 +++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pkgs/oizys/src/oizys.nim b/pkgs/oizys/src/oizys.nim index 3bd6e6d..da9e96e 100644 --- a/pkgs/oizys/src/oizys.nim +++ b/pkgs/oizys/src/oizys.nim @@ -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] diff --git a/pkgs/oizys/src/oizys/utils.nim b/pkgs/oizys/src/oizys/utils.nim index 66c6360..028cda7 100644 --- a/pkgs/oizys/src/oizys/utils.nim +++ b/pkgs/oizys/src/oizys/utils.nim @@ -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: