Show orbit search results as a table

Search prints CHANNEL, PACKAGE, VERSION and DESCRIPTION columns. Version and
description come from the manifest annotations, so no extra requests are made.
Empty results say which term matched nothing.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Haiku 5.5 committed 2026-11-26 23:27:49 -08:00
1 parent 6a369dfa5e
commit 8eadda5815
1 file changed
+150 -32
+150 -32
View File
@@ -631,24 +631,48 @@ async fn cmd_versions(ctx : Ctx, spec : String) -> Unit raise OrbitError {
} }
///| ///|
/// Whether a manifest is an Orbit package: its artifactType says so, or, for /// What search shows about one Orbit package, read from its manifest.
/// packages published before artifactType was set, its config has the Orbit struct RepoInfo {
/// config media type. version : String
fn is_orbit_manifest(text : String) -> Bool { description : String
}
///|
fn is_orbit_object(o : Map[String, Json]) -> Bool {
if jstr(o, "artifactType", "") == ORBIT_ARTIFACT_TYPE {
return true
}
match o.get("config") {
Some(Json::Object(c)) => jstr(c, "mediaType", "") == CONFIG_MEDIA_TYPE
_ => false
}
}
///|
/// The package info from a manifest, or None if it isn't an Orbit package.
/// Version and description come from the standard OCI annotations that publish
/// writes, so no extra request is needed.
fn orbit_info(text : String) -> RepoInfo? {
let parsed : Map[String, Json]? = Some(parse_object(text, "manifest")) catch { let parsed : Map[String, Json]? = Some(parse_object(text, "manifest")) catch {
_ => None _ => None
} }
match parsed { match parsed {
Some(o) => Some(o) =>
if jstr(o, "artifactType", "") == ORBIT_ARTIFACT_TYPE { if is_orbit_object(o) {
true let annotations : Map[String, Json] = match o.get("annotations") {
} else { Some(Json::Object(a)) => a
match o.get("config") { _ => Map([])
Some(Json::Object(c)) => jstr(c, "mediaType", "") == CONFIG_MEDIA_TYPE
_ => false
} }
Some(RepoInfo::{
version: jstr(annotations, "org.opencontainers.image.version", ""),
description: jstr(
annotations, "org.opencontainers.image.description", "",
),
})
} else {
None
} }
None => false None => None
} }
} }
@@ -656,20 +680,20 @@ fn is_orbit_manifest(text : String) -> Bool {
/// Look at one repository's latest manifest (or any tag if there is no latest). /// Look at one repository's latest manifest (or any tag if there is no latest).
/// Any failure counts as "not an Orbit package", so one broken repo can't hide /// Any failure counts as "not an Orbit package", so one broken repo can't hide
/// the others. /// the others.
async fn is_orbit_repo(ch : Channel, repo : String) -> Bool noraise { async fn orbit_repo_info(ch : Channel, repo : String) -> RepoInfo? noraise {
let tags = lines(capture(oras(ch, "repo tags " + sq(repo_of(ch, repo))))) catch { let tags = lines(capture(oras(ch, "repo tags " + sq(repo_of(ch, repo))))) catch {
_ => return false _ => return None
} }
if tags.length() == 0 { if tags.length() == 0 {
return false return None
} }
let tag = if tags.contains("latest") { "latest" } else { tags[0] } let tag = if tags.contains("latest") { "latest" } else { tags[0] }
let text = capture( let text = capture(
oras(ch, "manifest fetch " + sq(repo_of(ch, repo) + ":" + tag)), oras(ch, "manifest fetch " + sq(repo_of(ch, repo) + ":" + tag)),
) catch { ) catch {
_ => return false _ => return None
} }
is_orbit_manifest(text) orbit_info(text)
} }
///| ///|
@@ -677,7 +701,7 @@ async fn is_orbit_repo(ch : Channel, repo : String) -> Bool noraise {
async fn find_orbit_repos( async fn find_orbit_repos(
ch : Channel, ch : Channel,
repos : Array[String], repos : Array[String],
) -> Array[Bool] raise OrbitError { ) -> Array[RepoInfo?] raise OrbitError {
if repos.length() == 0 { if repos.length() == 0 {
return [] return []
} }
@@ -691,11 +715,11 @@ async fn find_orbit_repos(
} else { } else {
None None
} }
let tasks : Array[@async.Task[Bool]] = [] let tasks : Array[@async.Task[RepoInfo?]] = []
for r in repos { for r in repos {
tasks.push(group.spawn(() => is_orbit_repo(ch, r))) tasks.push(group.spawn(() => orbit_repo_info(ch, r)))
} }
let results : Array[Bool] = [] let results : Array[RepoInfo?] = []
for t in tasks { for t in tasks {
results.push(t.wait()) results.push(t.wait())
} }
@@ -712,8 +736,57 @@ async fn find_orbit_repos(
} }
} }
///|
/// Pad on the right to `width` characters, for the table columns.
fn pad_right(s : String, width : Int32) -> String {
let sb = StringBuilder()
sb.write_string(s)
let mut n = chars(s).length()
while n < width {
sb.write_char(' ')
n = n + 1
}
sb.to_string()
}
///|
/// Print rows as a table. The first row is the header. The last column is not
/// padded, so long descriptions don't leave trailing spaces.
fn print_table(rows : Array[Array[String]]) -> Unit {
let ncols = rows[0].length()
let widths : Array[Int32] = []
for c = 0; c < ncols; c = c + 1 {
widths.push(0)
}
for row in rows {
for c = 0; c < ncols; c = c + 1 {
let n = chars(row[c]).length()
if n > widths[c] {
widths[c] = n
}
}
}
for row in rows {
let sb = StringBuilder()
for c = 0; c < ncols; c = c + 1 {
if c > 0 {
sb.write_string(" ")
}
if c < ncols - 1 {
sb.write_string(pad_right(row[c], widths[c]))
} else {
sb.write_string(row[c])
}
}
println(sb.to_string())
}
}
///| ///|
async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError { async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError {
let rows : Array[Array[String]] = [
["CHANNEL", "PACKAGE", "VERSION", "DESCRIPTION"],
]
let mut listed = 0 let mut listed = 0
for ch in ctx.channels { for ch in ctx.channels {
let out = capture( let out = capture(
@@ -732,10 +805,25 @@ async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError {
candidates.push(r) candidates.push(r)
} }
} }
let orbit = find_orbit_repos(ch, candidates) let found = find_orbit_repos(ch, candidates)
for i, r in candidates { for i, r in candidates {
if orbit[i] { match found[i] {
println("\{ch.name}::\{r}") Some(info) =>
rows.push([
ch.name,
r,
if info.version == "" {
"-"
} else {
info.version
},
if info.description == "" {
"-"
} else {
info.description
},
])
None => ()
} }
} }
} }
@@ -744,25 +832,55 @@ async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError {
"no channel allows listing packages (catalog API); try `orbit versions <name>`", "no channel allows listing packages (catalog API); try `orbit versions <name>`",
) )
} }
if rows.length() == 1 {
if term == "" {
println("no packages found")
} else {
println("no packages match '\{term}'")
}
return
}
print_table(rows)
} }
///| ///|
test "orbit manifests are recognised by artifactType or config media type" { test "orbit manifests are recognised by artifactType or config media type" {
assert_true( assert_true(
is_orbit_manifest( orbit_info(
"{\"artifactType\":\"application/vnd.latha.orbit.v1\",\"config\":{\"mediaType\":\"x\"}}", "{\"artifactType\":\"application/vnd.latha.orbit.v1\",\"config\":{\"mediaType\":\"x\"}}",
), )
is Some(_),
) )
assert_true( assert_true(
is_orbit_manifest( orbit_info(
"{\"artifactType\":null,\"config\":{\"mediaType\":\"application/vnd.latha.orbit.config.v1+json\"}}", "{\"artifactType\":null,\"config\":{\"mediaType\":\"application/vnd.latha.orbit.config.v1+json\"}}",
), )
is Some(_),
) )
assert_false( assert_true(
is_orbit_manifest( orbit_info(
"{\"artifactType\":\"application/vnd.cncf.helm.chart.content.v1.tar+gzip\",\"config\":{\"mediaType\":\"application/vnd.cncf.helm.config.v1+json\"}}", "{\"artifactType\":\"application/vnd.cncf.helm.chart.content.v1.tar+gzip\",\"config\":{\"mediaType\":\"application/vnd.cncf.helm.config.v1+json\"}}",
), )
is None,
) )
assert_false(is_orbit_manifest("not json")) assert_true(orbit_info("not json") is None)
assert_false(is_orbit_manifest("{\"config\":\"string\"}")) assert_true(orbit_info("{\"config\":\"string\"}") is None)
}
///|
test "search info comes from OCI annotations" {
let info = orbit_info(
"{\"artifactType\":\"application/vnd.latha.orbit.v1\",\"annotations\":{\"org.opencontainers.image.version\":\"1.2.3\",\"org.opencontainers.image.description\":\"A tool\"}}",
).unwrap()
assert_eq(info.version, "1.2.3")
assert_eq(info.description, "A tool")
let bare = orbit_info("{\"artifactType\":\"application/vnd.latha.orbit.v1\"}").unwrap()
assert_eq(bare.version, "")
assert_eq(bare.description, "")
}
///|
test "table columns line up" {
assert_eq(pad_right("ab", 4), "ab ")
assert_eq(pad_right("abcd", 2), "abcd")
} }