feat(info): add -Si package info display
This commit is contained in:
@@ -0,0 +1,432 @@
|
||||
/// tofu.commands.info — -Si package info display.
|
||||
///
|
||||
/// Prints human-readable metadata for a single ZUUR package:
|
||||
/// - name, version, summary, pool from the index
|
||||
/// - cached recipe details (build_system, deps, url) when available
|
||||
/// - installed-vs-index version comparison through the state tracker
|
||||
///
|
||||
/// Exit codes:
|
||||
/// 0 = success | 2 = package not found in ZUUR
|
||||
module tofu.commands.info;
|
||||
|
||||
import tofu.types : PackageIndex, Pool, poolToString;
|
||||
import tofu.index : fetchIndex;
|
||||
import tofu.config : Config;
|
||||
import tofu.cli : ParsedArgs;
|
||||
import tofu.log : logError, logInfo;
|
||||
import tofu.state : isInstalledByTofu, InstalledPkg;
|
||||
import tofu.vercmp : compare;
|
||||
import std.stdio : writeln;
|
||||
import std.file : exists, readText;
|
||||
import std.path : buildPath;
|
||||
import std.string : indexOf;
|
||||
|
||||
@safe:
|
||||
|
||||
// ────────────────────────────────────────────────────────────
|
||||
// Light recipe-file scanner helpers
|
||||
// ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Scan a Lua-like recipe file for `key = "value"` and return `value`.
|
||||
/// Handles `\"` and `\\` escapes. Returns `""` when the key is absent
|
||||
/// or the value is empty.
|
||||
private string scanStringField(string content, string key) {
|
||||
size_t pos = 0;
|
||||
while (pos < content.length) {
|
||||
auto kp = content[pos .. $].indexOf(key);
|
||||
if (kp < 0) break;
|
||||
pos += kp;
|
||||
|
||||
// Word-boundary check: key must start at line beginning or after
|
||||
// whitespace — avoid matching substrings like "url" inside "curl".
|
||||
if (pos > 0) {
|
||||
auto prev = content[pos - 1];
|
||||
if (prev != ' ' && prev != '\t' && prev != '\n' && prev != '\r')
|
||||
{
|
||||
pos += key.length;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
pos += key.length;
|
||||
|
||||
// Skip whitespace, `=`, whitespace
|
||||
while (pos < content.length &&
|
||||
(content[pos] == ' ' || content[pos] == '\t')) pos++;
|
||||
if (pos >= content.length || content[pos] != '=') continue;
|
||||
pos++;
|
||||
while (pos < content.length &&
|
||||
(content[pos] == ' ' || content[pos] == '\t')) pos++;
|
||||
|
||||
if (pos >= content.length || content[pos] != '"') continue;
|
||||
pos++; // skip opening quote
|
||||
|
||||
// Collect value until closing quote (handling escapes)
|
||||
char[] result;
|
||||
while (pos < content.length) {
|
||||
if (content[pos] == '\\' && pos + 1 < content.length) {
|
||||
pos++;
|
||||
result ~= content[pos];
|
||||
pos++;
|
||||
} else if (content[pos] == '"') {
|
||||
pos++;
|
||||
break;
|
||||
} else if (content[pos] == '\n') {
|
||||
break; // unexpected newline — stop
|
||||
} else {
|
||||
result ~= content[pos];
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
// @trusted: result is freshly allocated char[], safe to cast
|
||||
string s;
|
||||
() @trusted { s = cast(string) result; }();
|
||||
return s;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// Scan a Lua-like recipe file for `deps = { "v1", "v2", ... }`
|
||||
/// and return the joined dependency list as `"v1, v2, ..."`.
|
||||
/// Returns `""` when `deps` is absent or the table is empty.
|
||||
private string scanDepsArray(string content) {
|
||||
size_t pos = content.indexOf("deps");
|
||||
if (pos < 0) return "";
|
||||
|
||||
// Word-boundary check
|
||||
if (pos > 0) {
|
||||
auto prev = content[pos - 1];
|
||||
if (prev != ' ' && prev != '\t' && prev != '\n' && prev != '\r')
|
||||
return "";
|
||||
}
|
||||
pos += 4; // skip "deps"
|
||||
|
||||
// Whitespace, `=`, whitespace
|
||||
while (pos < content.length &&
|
||||
(content[pos] == ' ' || content[pos] == '\t')) pos++;
|
||||
if (pos >= content.length || content[pos] != '=') return "";
|
||||
pos++;
|
||||
while (pos < content.length &&
|
||||
(content[pos] == ' ' || content[pos] == '\t')) pos++;
|
||||
|
||||
if (pos >= content.length || content[pos] != '{') return "";
|
||||
pos++; // skip `{`
|
||||
|
||||
// Collect quoted strings inside { ... }
|
||||
string[] items;
|
||||
while (pos < content.length) {
|
||||
// Skip whitespace, commas, newlines
|
||||
while (pos < content.length &&
|
||||
(content[pos] == ' ' || content[pos] == '\t' ||
|
||||
content[pos] == '\n' || content[pos] == '\r' ||
|
||||
content[pos] == ',')) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
if (pos >= content.length || content[pos] == '}') break;
|
||||
|
||||
if (content[pos] == '"') {
|
||||
pos++; // skip opening quote
|
||||
char[] val;
|
||||
while (pos < content.length) {
|
||||
if (content[pos] == '\\' && pos + 1 < content.length) {
|
||||
pos++;
|
||||
val ~= content[pos];
|
||||
pos++;
|
||||
} else if (content[pos] == '"') {
|
||||
pos++;
|
||||
break;
|
||||
} else if (content[pos] == '\n') {
|
||||
break;
|
||||
} else {
|
||||
val ~= content[pos];
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
string item;
|
||||
() @trusted { item = cast(string) val; }();
|
||||
if (item.length > 0) items ~= item;
|
||||
} else {
|
||||
pos++; // skip unexpected char
|
||||
}
|
||||
}
|
||||
|
||||
if (items.length == 0) return "";
|
||||
|
||||
// Join with ", "
|
||||
string result = items[0];
|
||||
foreach (i; 1 .. items.length) {
|
||||
result ~= ", ";
|
||||
result ~= items[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────
|
||||
// Main command
|
||||
// ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Print package info for `-Si <name>`.
|
||||
///
|
||||
/// The `indexFetcher` delegate injects a test double for the index.
|
||||
/// When `null`, the real `fetchIndex(cfg)` is used.
|
||||
///
|
||||
/// Returns exit code: 0 = success, 2 = package not found in ZUUR.
|
||||
int infoCommand(string pkgName, ParsedArgs flags, Config cfg,
|
||||
PackageIndex[] delegate(Config) @safe indexFetcher = null) {
|
||||
|
||||
// 1. Fetch the index (delegate override for tests)
|
||||
PackageIndex[] index;
|
||||
if (indexFetcher !is null)
|
||||
index = indexFetcher(cfg);
|
||||
else
|
||||
index = fetchIndex(cfg);
|
||||
|
||||
// 2. Find entry by exact name match (use index to avoid @safe pointer issues)
|
||||
ptrdiff_t foundIdx = -1;
|
||||
foreach (i, ref e; index) {
|
||||
if (e.name == pkgName) {
|
||||
foundIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundIdx < 0) {
|
||||
logError("package '%s' not found in ZUUR", pkgName);
|
||||
return 2;
|
||||
}
|
||||
|
||||
auto entry = index[foundIdx];
|
||||
|
||||
// 3. Print core info from index
|
||||
writeln(pkgName);
|
||||
writeln(" version: ", entry.ver);
|
||||
writeln(" summary: ", entry.summary);
|
||||
writeln(" pool: ", poolToString(entry.pool));
|
||||
|
||||
// 4. Recipe section — only when pool is recipes or both
|
||||
if (entry.pool == Pool.recipes || entry.pool == Pool.both) {
|
||||
auto recipeDir = cfg.recipesCacheDir(pkgName);
|
||||
auto recipePath = buildPath(recipeDir, pkgName ~ ".recipe");
|
||||
|
||||
bool cached;
|
||||
() @trusted { cached = exists(recipePath); }();
|
||||
|
||||
if (cached) {
|
||||
string content;
|
||||
() @trusted { content = readText(recipePath); }();
|
||||
|
||||
auto buildSys = scanStringField(content, "build_system");
|
||||
if (buildSys.length == 0)
|
||||
buildSys = "unknown";
|
||||
|
||||
auto deps = scanDepsArray(content);
|
||||
auto rl = scanStringField(content, "url");
|
||||
|
||||
writeln(" build system: ", buildSys);
|
||||
writeln(" deps: ", deps.length > 0 ? deps : "none");
|
||||
if (rl.length > 0)
|
||||
writeln(" url: ", rl);
|
||||
} else {
|
||||
logInfo("recipe not cached — run 'tofu -S %s' to fetch", pkgName);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Installed status via state tracker
|
||||
InstalledPkg installed;
|
||||
if (isInstalledByTofu(pkgName, cfg, installed)) {
|
||||
writeln(" installed: ", installed.ver);
|
||||
|
||||
int cmp = compare(installed.ver, entry.ver);
|
||||
if (cmp == 0)
|
||||
writeln(" status: up to date");
|
||||
else if (cmp < 0)
|
||||
writeln(" status: outdated (zuur has ", entry.ver, ")");
|
||||
else
|
||||
writeln(" status: newer than zuur");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────
|
||||
// Unittests
|
||||
// ────────────────────────────────────────────────────────────
|
||||
|
||||
version (unittest) {
|
||||
import std.process : thisProcessID;
|
||||
import std.conv : to;
|
||||
import std.file : write, mkdirRecurse, rmdirRecurse, tempDir;
|
||||
import tofu.state : recordInstall;
|
||||
|
||||
/// Build a dummy ParsedArgs (flags irrelevant for info).
|
||||
private @safe ParsedArgs dummyArgs(string name) {
|
||||
ParsedArgs pa;
|
||||
pa.arg = name;
|
||||
return pa;
|
||||
}
|
||||
|
||||
/// Create a temp Config with an isolated cache dir.
|
||||
private @safe Config testCfg(string suffix) {
|
||||
Config cfg;
|
||||
cfg.cacheDir = buildPath(tempDir,
|
||||
"tofu-test-info-" ~ suffix ~ "-" ~ to!string(thisProcessID()));
|
||||
return cfg;
|
||||
}
|
||||
|
||||
private @trusted void ensureDir(string path) {
|
||||
try mkdirRecurse(path); catch (Exception) {}
|
||||
}
|
||||
|
||||
private @trusted void removeDir(string path) {
|
||||
try rmdirRecurse(path); catch (Exception) {}
|
||||
}
|
||||
|
||||
/// Write a fake recipe to the correct cache location.
|
||||
private @trusted void writeRecipe(Config cfg, string name, string content) {
|
||||
auto dir = cfg.recipesCacheDir(name);
|
||||
ensureDir(dir);
|
||||
auto path = buildPath(dir, name ~ ".recipe");
|
||||
write(path, content);
|
||||
}
|
||||
|
||||
/// Canned index for all tests — no network.
|
||||
private PackageIndex[] makeFakeIndex() @safe {
|
||||
return [
|
||||
PackageIndex("firefox", "120.0", "Web browser", Pool.binary),
|
||||
PackageIndex("neovim", "0.9.5", "Text editor", Pool.both),
|
||||
PackageIndex("ripgrep", "14.1.0", "Fast grep", Pool.recipes),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// ── Test (1): package in index (both) → exit 0 ───────────────
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("both");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
auto flags = dummyArgs("neovim");
|
||||
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("neovim", flags, cfg, fetcher);
|
||||
assert(ec == 0, "expected exit 0, got " ~ to!string(ec));
|
||||
}
|
||||
|
||||
// ── Test (2): package not in index → exit 2 ──────────────────
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("notfound");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
auto flags = dummyArgs("nonexistent");
|
||||
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("nonexistent", flags, cfg, fetcher);
|
||||
assert(ec == 2, "expected exit 2 for not-found, got " ~ to!string(ec));
|
||||
}
|
||||
|
||||
// ── Test (3): cached recipe → prints build system + deps + url ─
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("cached");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
|
||||
writeRecipe(cfg, "neovim", q"LUA
|
||||
return {
|
||||
name = "neovim",
|
||||
ver = "0.9.5",
|
||||
build_system = "cmake",
|
||||
url = "https://github.com/neovim/neovim",
|
||||
deps = { "libluv>=1.30", "msgpack-c" },
|
||||
}
|
||||
LUA");
|
||||
|
||||
auto flags = dummyArgs("neovim");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("neovim", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
|
||||
// ── Test (4): installed + up to date → status line ────────────
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("uptodate");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
ensureDir(cfg.cacheDir);
|
||||
|
||||
recordInstall("neovim", "0.9.5", cfg);
|
||||
|
||||
auto flags = dummyArgs("neovim");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("neovim", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
|
||||
// ── Test (5): installed + outdated → status line with zuur ver ─
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("outdated");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
ensureDir(cfg.cacheDir);
|
||||
|
||||
recordInstall("ripgrep", "13.0.0", cfg);
|
||||
|
||||
auto flags = dummyArgs("ripgrep");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("ripgrep", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
|
||||
// ── Test (6): not installed → no status line ─────────────────
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("notinst");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
|
||||
auto flags = dummyArgs("firefox");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("firefox", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
|
||||
// ── Test (7): pool=binary only → no recipe section ────────────
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("binaryonly");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
|
||||
auto flags = dummyArgs("firefox");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("firefox", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
|
||||
// ── Extra: installed version newer than index → "newer than zuur"
|
||||
@safe unittest {
|
||||
auto cfg = testCfg("newer");
|
||||
scope (exit) removeDir(cfg.cacheDir);
|
||||
ensureDir(cfg.cacheDir);
|
||||
|
||||
recordInstall("ripgrep", "15.0.0", cfg);
|
||||
|
||||
auto flags = dummyArgs("ripgrep");
|
||||
auto fetcher = delegate PackageIndex[](Config _) @safe {
|
||||
return makeFakeIndex();
|
||||
};
|
||||
|
||||
int ec = infoCommand("ripgrep", flags, cfg, fetcher);
|
||||
assert(ec == 0);
|
||||
}
|
||||
Reference in New Issue
Block a user