The built-in main channel is always included. Priority is strict, packages remember their channel, and only a missing repository falls through to the next channel. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
358 lines
9.6 KiB
MoonBit
358 lines
9.6 KiB
MoonBit
// Channels: the registries packages come from, listed highest priority first.
|
|
// "main" is built in. Configuration lives in <orbit root>/config.toml.
|
|
|
|
///|
|
|
const MAIN_CHANNEL : String = "main"
|
|
|
|
///|
|
|
const MAIN_URL : String = "git.site.latha.org/nandi"
|
|
|
|
///|
|
|
struct Channel {
|
|
name : String
|
|
url : String // <host>/<namespace>; a package lives at <url>/<name>
|
|
plain_http : Bool
|
|
}
|
|
|
|
///|
|
|
/// What `orbit init` writes. Everything is commented out, so the built-in
|
|
/// main channel stays implicit.
|
|
let config_template : String =
|
|
#|# Orbit configuration.
|
|
#|#
|
|
#|# Channels are registries that packages come from, highest priority first.
|
|
#|# The built-in "main" channel (git.site.latha.org/nandi) is always included.
|
|
#|# It has top priority unless you list it lower down.
|
|
#|#
|
|
#|# A plain `orbit install <pkg>` uses the first channel that has <pkg>.
|
|
#|# `orbit install <channel>::<pkg>` picks one.
|
|
#|
|
|
#|# [[channels]]
|
|
#|# name = "example"
|
|
#|# url = "git.site.latha.org/nandi/example"
|
|
#|
|
|
#|# For a local registry without TLS:
|
|
#|# [[channels]]
|
|
#|# name = "local"
|
|
#|# url = "localhost:5000/pkgs"
|
|
#|# plain-http = true
|
|
#|
|
|
|
|
///|
|
|
fn valid_url(url : String) -> Bool {
|
|
if url == "" || has_prefix(url, "-") || contains_str(url, "://") {
|
|
return false
|
|
}
|
|
for c in url {
|
|
if is_space(c) || c == '"' || c == '\\' {
|
|
return false
|
|
}
|
|
}
|
|
!has_prefix(url, "/") &&
|
|
!has_prefix(drop_chars(url, chars(url).length() - 1), "/")
|
|
}
|
|
|
|
///|
|
|
/// The channels a config file lists, in file order. "main" may omit its url.
|
|
/// Unknown keys are errors, so that a typo can't silently change priority.
|
|
fn parse_config(text : String) -> Array[Channel] raise OrbitError {
|
|
let doc = @toml.parse(text) catch {
|
|
e => raise OrbitError("config.toml: invalid TOML: \{e}")
|
|
}
|
|
let out : Array[Channel] = []
|
|
let top = match doc {
|
|
TomlTable(t) => t
|
|
_ => raise OrbitError("config.toml: expected a table")
|
|
}
|
|
for key, value in top {
|
|
if key != "channels" {
|
|
raise OrbitError("config.toml: unknown key '\{key}'")
|
|
}
|
|
let items = match value {
|
|
TomlArray(a) => a
|
|
_ => raise OrbitError("config.toml: use [[channels]] blocks")
|
|
}
|
|
for item in items {
|
|
let t = match item {
|
|
TomlTable(t) => t
|
|
_ => raise OrbitError("config.toml: use [[channels]] blocks")
|
|
}
|
|
let mut name = ""
|
|
let mut url = ""
|
|
let mut plain_http = false
|
|
for k, v in t {
|
|
match (k, v) {
|
|
("name", TomlString(s)) => name = s
|
|
("url", TomlString(s)) => url = s
|
|
("plain-http", TomlBoolean(b)) => plain_http = b
|
|
_ => raise OrbitError("config.toml: bad channel key '\{k}'")
|
|
}
|
|
}
|
|
if !valid_name(name) {
|
|
raise OrbitError("config.toml: invalid channel name '\{name}'")
|
|
}
|
|
if url == "" && name == MAIN_CHANNEL {
|
|
url = MAIN_URL
|
|
}
|
|
if !valid_url(url) {
|
|
raise OrbitError(
|
|
"config.toml: channel '\{name}' needs a url like host/namespace (no https://)",
|
|
)
|
|
}
|
|
for c in out {
|
|
if c.name == name {
|
|
raise OrbitError("config.toml: channel '\{name}' is listed twice")
|
|
}
|
|
}
|
|
out.push(Channel::{ name, url, plain_http, })
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// Add the built-in main channel if it isn't listed, then apply ORBIT_REGISTRY
|
|
/// and ORBIT_PLAIN_HTTP, which override main only.
|
|
fn resolve_channels(
|
|
listed : Array[Channel],
|
|
env_url : String,
|
|
env_plain_http : Bool,
|
|
) -> Array[Channel] {
|
|
let all : Array[Channel] = []
|
|
if !listed.iter().any(fn(c) { c.name == MAIN_CHANNEL }) {
|
|
all.push(Channel::{ name: MAIN_CHANNEL, url: MAIN_URL, plain_http: false, })
|
|
}
|
|
for c in listed {
|
|
all.push(c)
|
|
}
|
|
all.map(fn(c) {
|
|
if c.name == MAIN_CHANNEL {
|
|
Channel::{
|
|
name: c.name,
|
|
url: if env_url != "" {
|
|
env_url
|
|
} else {
|
|
c.url
|
|
},
|
|
plain_http: c.plain_http || env_plain_http,
|
|
}
|
|
} else {
|
|
c
|
|
}
|
|
})
|
|
}
|
|
|
|
///|
|
|
/// config.json from earlier versions holds a single registry, which stands in
|
|
/// for main. It is only read while there is no config.toml.
|
|
fn parse_legacy_config(text : String) -> Array[Channel] raise OrbitError {
|
|
let o = parse_object(text, "config.json")
|
|
let url = jstr(o, "registry", "")
|
|
if url == "" {
|
|
[]
|
|
} else {
|
|
[
|
|
Channel::{
|
|
name: MAIN_CHANNEL,
|
|
url,
|
|
plain_http: jbool(o, "plain_http", false),
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn load_listed_channels(root : String) -> Array[Channel] raise OrbitError {
|
|
match read_text(root + "/config.toml") {
|
|
Some(t) => parse_config(t)
|
|
None =>
|
|
match read_text(root + "/config.json") {
|
|
Some(t) => parse_legacy_config(t)
|
|
None => []
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn channel_toml(c : Channel) -> String {
|
|
let url = if c.name == MAIN_CHANNEL && c.url == MAIN_URL {
|
|
""
|
|
} else {
|
|
"url = \"\{c.url}\"\n"
|
|
}
|
|
let plain = if c.plain_http { "plain-http = true\n" } else { "" }
|
|
"[[channels]]\nname = \"\{c.name}\"\n" + url + plain
|
|
}
|
|
|
|
///|
|
|
/// Append a channel at the lowest priority, keeping the file's comments.
|
|
fn cmd_channel_add(
|
|
root : String,
|
|
name : String,
|
|
url : String,
|
|
plain_http : Bool,
|
|
) -> Unit raise OrbitError {
|
|
let path = root + "/config.toml"
|
|
let text = match read_text(path) {
|
|
Some(t) => t
|
|
None => {
|
|
// First config.toml: carry over anything set in a legacy config.json.
|
|
let mut t = config_template
|
|
for c in load_listed_channels(root) {
|
|
if c.url != MAIN_URL || c.plain_http {
|
|
t = t + "\n" + channel_toml(c)
|
|
}
|
|
}
|
|
t
|
|
}
|
|
}
|
|
for c in parse_config(text) {
|
|
if c.name == name {
|
|
raise OrbitError("channel '\{name}' already exists; edit \{path}")
|
|
}
|
|
}
|
|
if name == MAIN_CHANNEL {
|
|
raise OrbitError(
|
|
"main is built in; to move or repoint it, add [[channels]] to \{path}",
|
|
)
|
|
}
|
|
let sep = if has_prefix(drop_chars(text, chars(text).length() - 1), "\n") {
|
|
"\n"
|
|
} else {
|
|
"\n\n"
|
|
}
|
|
let updated = text + sep + channel_toml(Channel::{ name, url, plain_http, })
|
|
parse_config(updated) |> ignore // never write a file we can't read back
|
|
sh_ok("mkdir -p " + sq(root))
|
|
write_text_atomic(path, updated)
|
|
println("added channel \{name} (\{url}) to \{path}")
|
|
}
|
|
|
|
///|
|
|
fn print_channels(channels : Array[Channel]) -> Unit {
|
|
for i, c in channels {
|
|
let plain = if c.plain_http { " (plain http)" } else { "" }
|
|
println("\{i + 1}. \{c.name} \{c.url}\{plain}")
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn find_channel(ctx : Ctx, name : String) -> Channel raise OrbitError {
|
|
for c in ctx.channels {
|
|
if c.name == name {
|
|
return c
|
|
}
|
|
}
|
|
raise OrbitError(
|
|
"channel '\{name}' is not configured (see `orbit channel list`)",
|
|
)
|
|
}
|
|
|
|
///|
|
|
/// "channel::rest" or just "rest".
|
|
fn split_channel(spec : String) -> (String?, String) {
|
|
let a = chars(spec)
|
|
for i = 0; i + 1 < a.length(); i = i + 1 {
|
|
if a[i] == ':' && a[i + 1] == ':' {
|
|
return (Some(slice(a, 0, i)), slice(a, i + 2, a.length()))
|
|
}
|
|
}
|
|
(None, spec)
|
|
}
|
|
|
|
///|
|
|
/// The channel to get `name` from, with its tags there. A pinned channel is
|
|
/// used as is. Otherwise the first channel that has the package wins, even if
|
|
/// a lower one has a newer version.
|
|
fn locate(
|
|
ctx : Ctx,
|
|
name : String,
|
|
pin : String?,
|
|
) -> (Channel, Array[String]) raise OrbitError {
|
|
let candidates = match pin {
|
|
Some(p) => [find_channel(ctx, p)]
|
|
None => ctx.channels
|
|
}
|
|
for c in candidates {
|
|
match list_tags(c, name) {
|
|
Some(tags) => return (c, tags)
|
|
None => ()
|
|
}
|
|
}
|
|
let searched = candidates.map(fn(c) { c.name })
|
|
raise OrbitError(
|
|
"package '\{name}' not found in channel(s): \{join(searched, ", ")}",
|
|
)
|
|
}
|
|
|
|
///|
|
|
test "main is always included, first unless listed" {
|
|
let added = resolve_channels(
|
|
parse_config(
|
|
"[[channels]]\nname = \"b\"\nurl = \"h/b\"\n\n[[channels]]\nname = \"a\"\nurl = \"h/a\"\nplain-http = true\n",
|
|
),
|
|
"",
|
|
false,
|
|
)
|
|
assert_eq(added.map(fn(c) { c.name }), ["main", "b", "a"])
|
|
assert_eq(added[0].url, MAIN_URL)
|
|
assert_true(added[2].plain_http)
|
|
let lowered = resolve_channels(
|
|
parse_config(
|
|
"[[channels]]\nname = \"a\"\nurl = \"h/a\"\n[[channels]]\nname = \"main\"\n",
|
|
),
|
|
"",
|
|
false,
|
|
)
|
|
assert_eq(lowered.map(fn(c) { c.name }), ["a", "main"])
|
|
assert_eq(lowered[1].url, MAIN_URL)
|
|
}
|
|
|
|
///|
|
|
test "env overrides main only" {
|
|
let cs = resolve_channels(
|
|
parse_config("[[channels]]\nname = \"a\"\nurl = \"h/a\"\n"),
|
|
"env/r",
|
|
true,
|
|
)
|
|
assert_eq(cs[0].url, "env/r")
|
|
assert_true(cs[0].plain_http)
|
|
assert_eq(cs[1].url, "h/a")
|
|
assert_true(!cs[1].plain_http)
|
|
}
|
|
|
|
///|
|
|
test "template and bad configs" {
|
|
assert_eq(parse_config(config_template).length(), 0)
|
|
for
|
|
bad in [
|
|
"[[channels]]\nname = \"a\"\n", "[[channels]]\nname = \"a\"\nurl = \"https://h/a\"\n",
|
|
"[[channels]]\nname = \"a\"\nurl = \"h/a\"\n[[channels]]\nname = \"a\"\nurl = \"h/b\"\n",
|
|
"[[channel]]\nname = \"a\"\nurl = \"h/a\"\n", "[[channels]]\nname = \"a\"\nurl = \"h/a\"\nplain_http = true\n",
|
|
"[[channels",
|
|
] {
|
|
let ok = try {
|
|
parse_config(bad) |> ignore
|
|
true
|
|
} catch {
|
|
_ => false
|
|
}
|
|
assert_false(ok)
|
|
}
|
|
}
|
|
|
|
///|
|
|
test "channel spec" {
|
|
assert_eq(split_channel("example::greet@^1"), (Some("example"), "greet@^1"))
|
|
assert_eq(split_channel("greet"), (None, "greet"))
|
|
}
|
|
|
|
///|
|
|
test "only a missing repository falls through to the next channel" {
|
|
assert_true(is_missing_repo("Error response from registry: name unknown"))
|
|
assert_true(is_missing_repo("GET \"u\": response status code 404: Not Found"))
|
|
assert_false(is_missing_repo("dial tcp: lookup x: no such host"))
|
|
assert_false(is_missing_repo("connect: connection refused"))
|
|
assert_false(is_missing_repo("response status code 401: Unauthorized"))
|
|
}
|