use super::base;
use std::io::Read;
use std::path::Path;
const HOSTS_FILE: &str = ".context-hosts";
const DEFAULT: &str = "localhost:11434";
pub(crate) fn bases(
arg: Option<&str>,
root: &Path,
) -> Result<Vec<String>, String> {
raw_hosts(arg, root).iter().map(|h| host_base(h)).collect()
}
fn raw_hosts(arg: Option<&str>, root: &Path) -> Vec<String> {
match arg {
Some("-") => split(&stdin()),
Some(list) => split(list),
None => from_file(root)
.or_else(from_env)
.unwrap_or_else(|| vec![DEFAULT.to_owned()]),
}
}
fn host_base(host: &str) -> Result<String, String> {
if is_cidr(host) {
return Err(format!(
"no CIDR '{host}' -- list hosts explicitly, itok never scans (V24)"
));
}
Ok(base(host))
}
fn is_cidr(host: &str) -> bool {
host.rsplit("://").next().unwrap_or(host).contains('/')
}
fn split(list: &str) -> Vec<String> {
list.split([',', '\n', ' ', '\t'])
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_owned)
.collect()
}
fn from_file(root: &Path) -> Option<Vec<String>> {
let text = std::fs::read_to_string(root.join(HOSTS_FILE)).ok()?;
let hosts: Vec<String> = text
.lines()
.map(str::trim)
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.map(str::to_owned)
.collect();
(!hosts.is_empty()).then_some(hosts)
}
fn from_env() -> Option<Vec<String>> {
std::env::var("OLLAMA_HOST")
.ok()
.filter(|s| !s.is_empty())
.map(|h| vec![h])
}
fn stdin() -> String {
let mut s = String::new();
let _ = std::io::stdin().read_to_string(&mut s);
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_comma_list_becomes_normalized_bases() {
let root = Path::new("/no/such/dir");
let got = bases(Some("box1,box2:6666"), root).unwrap_or_default();
assert_eq!(got, vec!["http://box1:11434", "http://box2:6666"]);
}
#[test]
fn whitespace_and_empties_are_dropped() {
assert_eq!(split(" a , , b\n"), vec!["a".to_owned(), "b".to_owned()]);
}
#[test]
fn a_cidr_is_rejected() {
let e = bases(Some("10.0.0.0/24"), Path::new("."))
.err()
.unwrap_or_default();
assert!(e.contains("CIDR"), "{e}");
assert!(!is_cidr("http://box:11434"), "scheme :// is not a CIDR");
assert!(is_cidr("10.0.0.0/24"));
}
#[test]
fn no_arg_no_file_no_env_is_localhost() {
let root = Path::new("/no/such/itok/dir");
let got = bases(None, root).unwrap_or_default();
assert_eq!(got.len(), 1);
assert!(got.first().is_some_and(|h| h.starts_with("http")));
}
#[test]
fn context_hosts_file_supplies_the_fleet() {
let dir = std::env::temp_dir()
.join(format!("itok-t18-hosts-{}", std::process::id()));
std::fs::create_dir_all(&dir).ok();
std::fs::write(dir.join(HOSTS_FILE), "# fleet\nbox1\nbox2:6666\n").ok();
let got = from_file(&dir).unwrap_or_default();
assert_eq!(got, vec!["box1".to_owned(), "box2:6666".to_owned()]);
}
}