diff --git a/README.md b/README.md index 3ab1b0f..b620935 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ mypkg/ } ``` -In a channel this becomes `/mytool:1.4.0`, plus the `latest` tag: +In a channel this becomes `/mytool:1.4.0`, plus the `latest` tag. The manifest sets `artifactType` to `application/vnd.latha.orbit.v1`, which is how `orbit search` tells Orbit packages apart from other OCI artifacts such as Helm charts. Packages published before this have no `artifactType`, so search also recognises the Orbit config media type: | Part | Media type | |---|---| diff --git a/src/main/model.mbt b/src/main/model.mbt index d31f17d..2a2d10b 100644 --- a/src/main/model.mbt +++ b/src/main/model.mbt @@ -3,6 +3,10 @@ ///| const CONFIG_MEDIA_TYPE : String = "application/vnd.latha.orbit.config.v1+json" +///| +/// Manifest artifactType that marks a repository as an Orbit package. +const ORBIT_ARTIFACT_TYPE : String = "application/vnd.latha.orbit.v1" + ///| const LAYER_MEDIA_TYPE : String = "application/vnd.latha.orbit.layer.v1.tar+gzip" diff --git a/src/main/ops.mbt b/src/main/ops.mbt index 373b625..dcff54c 100644 --- a/src/main/ops.mbt +++ b/src/main/ops.mbt @@ -218,9 +218,7 @@ fn owner_of_bin(ctx : Ctx, bin : String) -> String? { async fn unlink_bins(ctx : Ctx, bins : Array[String]) -> Unit noraise { for b in bins { let path = bin_dir(ctx) + "/" + basename(b) - try { - @fs.remove(path) - } catch { + @fs.remove(path) catch { _ => () } } @@ -232,9 +230,7 @@ async fn link_bins( dir : String, bins : Array[String], ) -> Unit raise OrbitError { - try { - @fs.mkdir(bin_dir(ctx), recursive=true) - } catch { + @fs.mkdir(bin_dir(ctx), recursive=true) catch { e => raise OrbitError("cannot create bin dir: " + e.to_string()) } for b in bins { @@ -534,11 +530,7 @@ async fn cmd_gc(ctx : Ctx) -> Unit raise OrbitError { return } let mut freed = 0 - let pkgs = try { - @fs.readdir(store) - } catch { - _ => [] - } + let pkgs = @fs.readdir(store) catch { _ => [] } for pkg in pkgs { match ctx.state.get(pkg) { None => { @@ -550,11 +542,7 @@ async fn cmd_gc(ctx : Ctx) -> Unit raise OrbitError { for g in inst.history { keep.push(store_id(g.digest)) } - let ids = try { - @fs.readdir(store + "/" + pkg) - } catch { - _ => [] - } + let ids = @fs.readdir(store + "/" + pkg) catch { _ => [] } for id in ids { if !keep.contains(id) { sh_ok("rm -rf " + sq(store + "/" + pkg + "/" + id)) @@ -642,6 +630,88 @@ async fn cmd_versions(ctx : Ctx, spec : String) -> Unit raise OrbitError { } } +///| +/// Whether a manifest is an Orbit package: its artifactType says so, or, for +/// packages published before artifactType was set, its config has the Orbit +/// config media type. +fn is_orbit_manifest(text : String) -> Bool { + let parsed : Map[String, Json]? = Some(parse_object(text, "manifest")) catch { + _ => None + } + match parsed { + Some(o) => + if jstr(o, "artifactType", "") == ORBIT_ARTIFACT_TYPE { + true + } else { + match o.get("config") { + Some(Json::Object(c)) => jstr(c, "mediaType", "") == CONFIG_MEDIA_TYPE + _ => false + } + } + None => false + } +} + +///| +/// 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 +/// the others. +async fn is_orbit_repo(ch : Channel, repo : String) -> Bool noraise { + let tags = lines(capture(oras(ch, "repo tags " + sq(repo_of(ch, repo))))) catch { + _ => return false + } + if tags.length() == 0 { + return false + } + let tag = if tags.contains("latest") { "latest" } else { tags[0] } + let text = capture( + oras(ch, "manifest fetch " + sq(repo_of(ch, repo) + ":" + tag)), + ) catch { + _ => return false + } + is_orbit_manifest(text) +} + +///| +/// Check many repositories at once, with one spinner for the whole batch. +async fn find_orbit_repos( + ch : Channel, + repos : Array[String], +) -> Array[Bool] raise OrbitError { + if repos.length() == 0 { + return [] + } + let label = "Checking \{repos.length()} repositories in \{ch.name}" + let is_tty = c_isatty(2) != 0 && getenv("TERM") != "dumb" + @async.with_task_group(group => { + let spinner = if is_tty { + Some( + group.spawn(() => ignore(@async.handle_cancellation(() => spin(label)))), + ) + } else { + None + } + let tasks : Array[@async.Task[Bool]] = [] + for r in repos { + tasks.push(group.spawn(() => is_orbit_repo(ch, r))) + } + let results : Array[Bool] = [] + for t in tasks { + results.push(t.wait()) + } + match spinner { + Some(s) => s.cancel() + None => () + } + if is_tty { + c_print_err(utf8_encode("\r\u001B[2K")) + } + results + }) catch { + e => raise OrbitError(e.to_string()) + } +} + ///| async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError { let mut listed = 0 @@ -656,8 +726,15 @@ async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError { } } listed = listed + 1 + let candidates : Array[String] = [] for r in lines(out) { if contains_str(r, term) { + candidates.push(r) + } + } + let orbit = find_orbit_repos(ch, candidates) + for i, r in candidates { + if orbit[i] { println("\{ch.name}::\{r}") } } @@ -668,3 +745,24 @@ async fn cmd_search(ctx : Ctx, term : String) -> Unit raise OrbitError { ) } } + +///| +test "orbit manifests are recognised by artifactType or config media type" { + assert_true( + is_orbit_manifest( + "{\"artifactType\":\"application/vnd.latha.orbit.v1\",\"config\":{\"mediaType\":\"x\"}}", + ), + ) + assert_true( + is_orbit_manifest( + "{\"artifactType\":null,\"config\":{\"mediaType\":\"application/vnd.latha.orbit.config.v1+json\"}}", + ), + ) + assert_false( + is_orbit_manifest( + "{\"artifactType\":\"application/vnd.cncf.helm.chart.content.v1.tar+gzip\",\"config\":{\"mediaType\":\"application/vnd.cncf.helm.config.v1+json\"}}", + ), + ) + assert_false(is_orbit_manifest("not json")) + assert_false(is_orbit_manifest("{\"config\":\"string\"}")) +} diff --git a/src/main/registry.mbt b/src/main/registry.mbt index 928650b..2119c11 100644 --- a/src/main/registry.mbt +++ b/src/main/registry.mbt @@ -218,6 +218,8 @@ async fn publish( ch, "push " + sq(repo_of(ch, m.name) + ":" + tags) + + " --artifact-type " + + sq(ORBIT_ARTIFACT_TYPE) + " --config " + sq("orbit.json:" + CONFIG_MEDIA_TYPE) + " --annotation " +