use std::path::{Path, PathBuf};
use std::time::Duration;
pub const DETECT_TIMEOUT: Duration = Duration::from_secs(5);
pub const CLUES_TIMEOUT: Duration = Duration::from_secs(3);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailureKind {
Unreachable,
Timeout,
Hub,
NoCwd,
}
#[derive(Debug, Clone)]
pub struct DetectFailure {
pub kind: FailureKind,
pub cwd: String,
pub detail: String,
}
impl DetectFailure {
pub fn new(kind: FailureKind, cwd: &str, detail: impl Into<String>) -> Self {
Self {
kind,
cwd: cwd.to_string(),
detail: detail.into(),
}
}
fn from_reqwest(server: &str, cwd: &str, e: &reqwest::Error) -> Self {
if e.is_timeout() {
let mut detail = format!(
"the hub at {server} did not answer within {}s",
DETECT_TIMEOUT.as_secs()
);
if cfg!(target_os = "macos") {
detail.push_str(
" — if macOS is showing a privacy prompt for ctxone-hub \
(common right after an upgrade), answer it",
);
}
Self::new(FailureKind::Timeout, cwd, detail)
} else if e.is_connect() {
Self::new(
FailureKind::Unreachable,
cwd,
format!("hub unreachable at {server} ({})", root_cause(e)),
)
} else {
Self::new(
FailureKind::Hub,
cwd,
format!("request to {server} failed ({e})"),
)
}
}
pub fn exit_code(&self) -> i32 {
match self.kind {
FailureKind::Unreachable => crate::EX_UNAVAILABLE,
FailureKind::Timeout => crate::EX_TEMPFAIL,
FailureKind::Hub => crate::EX_PROTOCOL,
FailureKind::NoCwd => crate::EX_NOINPUT,
}
}
pub fn headline(&self) -> String {
format!(
"can't tell which workspace {} belongs to: {}",
self.cwd, self.detail
)
}
pub fn report(&self) {
eprintln!("ctx: {}", self.headline());
eprintln!("{}", NAMESPACE_HINT);
}
pub fn exit(&self) -> ! {
self.report();
std::process::exit(self.exit_code());
}
}
pub const NAMESPACE_HINT: &str = " Not falling back to the 'default' workspace, which would read and \
write the wrong data.\n Pass --namespace <workspace> (or set CTX_NAMESPACE) to choose one \
and skip detection.";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Clues {
pub ctxproject: Option<String>,
pub ctxproject_error: Option<String>,
pub remote_url: Option<String>,
}
impl Clues {
pub fn read(cwd: &Path) -> Self {
let (ctxproject, ctxproject_error) = match find_ctxproject(cwd) {
Ok(id) => (id, None),
Err((path, e)) => (None, Some(unreadable_marker_message(&path, &e))),
};
Self {
ctxproject,
ctxproject_error,
remote_url: crate::read_git_remote(cwd),
}
}
pub fn query(&self, cwd: &str) -> Vec<(&'static str, String)> {
let mut q = vec![("cwd", cwd.to_string()), ("client_read", "true".into())];
let fields = [
("ctxproject", &self.ctxproject),
("ctxproject_error", &self.ctxproject_error),
("remote_url", &self.remote_url),
];
for (name, value) in fields {
if let Some(v) = value {
q.push((name, v.clone()));
}
}
q
}
}
pub async fn read_clues(cwd: &str) -> Result<Clues, DetectFailure> {
let dir = PathBuf::from(cwd);
let read = tokio::task::spawn_blocking(move || Clues::read(&dir));
match tokio::time::timeout(CLUES_TIMEOUT, read).await {
Ok(Ok(clues)) => Ok(clues),
Ok(Err(e)) => Err(DetectFailure::new(
FailureKind::Hub,
cwd,
format!("reading the directory failed ({e})"),
)),
Err(_) => {
let mut detail = format!(
"reading {cwd} (its .ctxproject and git remote) took over {}s",
CLUES_TIMEOUT.as_secs()
);
if cfg!(target_os = "macos") {
detail.push_str(" — if macOS is showing a privacy prompt, answer it");
}
Err(DetectFailure::new(FailureKind::Timeout, cwd, detail))
}
}
}
pub async fn query_for(cwd: &str) -> Result<Vec<(&'static str, String)>, DetectFailure> {
Ok(read_clues(cwd).await?.query(cwd))
}
fn find_ctxproject(start: &Path) -> Result<Option<String>, (PathBuf, std::io::Error)> {
let mut dir = Some(start);
while let Some(d) = dir {
let candidate = d.join(".ctxproject");
let content = match std::fs::metadata(&candidate) {
Ok(meta) if meta.is_file() => std::fs::read_to_string(&candidate),
Ok(_) => Err(std::io::ErrorKind::InvalidInput.into()),
Err(e) => Err(e),
};
match content {
Ok(content) => {
if let Some(id) = content.lines().map(str::trim).find(|l| !l.is_empty()) {
return Ok(Some(id.to_string()));
}
}
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
return Err((candidate, e));
}
Err(_) => {}
}
dir = d.parent();
}
Ok(None)
}
fn unreadable_marker_message(path: &Path, e: &std::io::Error) -> String {
let mut msg = format!("cannot read {}: {e}", path.display());
if cfg!(target_os = "macos") {
msg.push_str(
" — grant the app running ctx (your terminal, or ctxone-hub for its \
background sync) access in System Settings → Privacy & Security → \
Files and Folders (or Full Disk Access), then retry",
);
}
msg
}
pub async fn detect(
client: &reqwest::Client,
server: &str,
cwd: &str,
) -> Result<Option<String>, DetectFailure> {
let query = query_for(cwd).await?;
let resp = client
.get(format!("{server}/api/projects/detect"))
.query(&query)
.send()
.await
.map_err(|e| DetectFailure::from_reqwest(server, cwd, &e))?;
let status = resp.status();
let body = resp
.text()
.await
.map_err(|e| DetectFailure::from_reqwest(server, cwd, &e))?;
interpret(status, &body, cwd)
}
pub fn interpret(
status: reqwest::StatusCode,
body: &str,
cwd: &str,
) -> Result<Option<String>, DetectFailure> {
let parsed: Option<serde_json::Value> = serde_json::from_str(body).ok();
if !status.is_success() {
let detail = parsed
.as_ref()
.and_then(|v| v["error"].as_str())
.map(str::to_string)
.unwrap_or_else(|| format!("hub answered {status}: {}", snippet(body)));
let kind = if status == reqwest::StatusCode::GATEWAY_TIMEOUT {
FailureKind::Timeout
} else {
FailureKind::Hub
};
return Err(DetectFailure::new(kind, cwd, detail));
}
let unexpected = || {
DetectFailure::new(
FailureKind::Hub,
cwd,
format!("unexpected answer from the hub: {}", snippet(body)),
)
};
let v = parsed.ok_or_else(unexpected)?;
match v["status"].as_str() {
Some("found") => v["namespace"]
.as_str()
.map(|ns| Some(ns.to_string()))
.ok_or_else(unexpected),
Some("not_found") | Some("registry_unavailable") => Ok(None),
_ => Err(unexpected()),
}
}
fn root_cause(e: &reqwest::Error) -> String {
let mut cur: &dyn std::error::Error = e;
while let Some(next) = cur.source() {
cur = next;
}
cur.to_string()
}
fn snippet(body: &str) -> String {
let flat = body.split_whitespace().collect::<Vec<_>>().join(" ");
if flat.is_empty() {
return "(empty body)".to_string();
}
match flat.char_indices().nth(200) {
Some((i, _)) => format!("{}…", &flat[..i]),
None => flat,
}
}
#[cfg(test)]
mod tests {
use super::*;
use reqwest::StatusCode;
const CWD: &str = "/Users/me/Documents/repo";
#[test]
fn found_is_the_project_namespace() {
let body = r#"{"status":"found","via":"ctxproject","project_id":"p","namespace":"p-ns"}"#;
assert_eq!(
interpret(StatusCode::OK, body, CWD).unwrap(),
Some("p-ns".into())
);
}
#[test]
fn not_found_and_no_registry_are_the_default_workspace() {
for status in ["not_found", "registry_unavailable"] {
let body = format!(r#"{{"status":"{status}","namespace":"default"}}"#);
assert_eq!(interpret(StatusCode::OK, &body, CWD).unwrap(), None);
}
}
#[test]
fn hub_timeout_is_a_timeout_failure_with_its_explanation() {
let body = r#"{"status":"timeout","error":"project detection did not finish within 3s"}"#;
let f = interpret(StatusCode::GATEWAY_TIMEOUT, body, CWD).unwrap_err();
assert_eq!(f.kind, FailureKind::Timeout);
assert_eq!(f.exit_code(), crate::EX_TEMPFAIL);
assert!(
f.detail.contains("did not finish within 3s"),
"{}",
f.detail
);
assert!(f.headline().contains(CWD));
}
#[test]
fn hub_errors_are_failures_not_not_found() {
let body =
r#"{"status":"error","error":"cannot read /r/.ctxproject: Operation not permitted"}"#;
let f = interpret(StatusCode::INTERNAL_SERVER_ERROR, body, CWD).unwrap_err();
assert_eq!(f.kind, FailureKind::Hub);
assert!(f.detail.contains("Operation not permitted"));
let f = interpret(
StatusCode::SERVICE_UNAVAILABLE,
r#"{"status":"busy","error":"x"}"#,
CWD,
)
.unwrap_err();
assert_eq!(f.kind, FailureKind::Hub);
let f = interpret(StatusCode::UNAUTHORIZED, "missing bearer token", CWD).unwrap_err();
assert!(f.detail.contains("missing bearer token"), "{}", f.detail);
}
#[test]
fn unparseable_or_unknown_answers_are_failures() {
assert!(interpret(StatusCode::OK, "<html>proxy</html>", CWD).is_err());
assert!(interpret(StatusCode::OK, r#"{"status":"maybe"}"#, CWD).is_err());
assert!(interpret(StatusCode::OK, r#"{"status":"found"}"#, CWD).is_err());
}
#[test]
fn snippet_is_bounded_and_single_line() {
let long = "word ".repeat(100);
let s = snippet(&long);
assert!(s.chars().count() <= 201);
assert!(!s.contains('\n'));
assert_eq!(snippet(" "), "(empty body)");
}
fn client(timeout: Duration) -> reqwest::Client {
reqwest::Client::builder().timeout(timeout).build().unwrap()
}
#[tokio::test]
async fn silent_hub_is_a_timeout_failure() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let server = format!("http://{}", listener.local_addr().unwrap());
let hold = tokio::spawn(async move {
let (_sock, _) = listener.accept().await.unwrap();
tokio::time::sleep(Duration::from_secs(30)).await;
});
let f = detect(&client(Duration::from_millis(200)), &server, CWD)
.await
.unwrap_err();
assert_eq!(f.kind, FailureKind::Timeout, "{f:?}");
hold.abort();
}
#[tokio::test]
async fn closed_port_is_an_unreachable_failure() {
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let server = format!("http://127.0.0.1:{port}");
let f = detect(&client(Duration::from_secs(2)), &server, CWD)
.await
.unwrap_err();
assert_eq!(f.kind, FailureKind::Unreachable, "{f:?}");
assert_eq!(f.exit_code(), crate::EX_UNAVAILABLE);
}
#[test]
fn clues_come_from_the_nearest_marker_with_an_id() {
let root = tempfile::tempdir().unwrap();
let outer = root.path().join("outer");
let inner = outer.join("inner");
let deep = inner.join("src").join("deep");
std::fs::create_dir_all(&deep).unwrap();
std::fs::write(outer.join(".ctxproject"), "outer-id\n").unwrap();
assert_eq!(Clues::read(&deep).ctxproject.as_deref(), Some("outer-id"));
std::fs::write(inner.join(".ctxproject"), " \n\n").unwrap();
assert_eq!(Clues::read(&deep).ctxproject.as_deref(), Some("outer-id"));
std::fs::write(inner.join(".ctxproject"), "\n inner-id \nignored\n").unwrap();
let clues = Clues::read(&deep);
assert_eq!(clues.ctxproject.as_deref(), Some("inner-id"));
assert_eq!(clues.ctxproject_error, None);
}
#[cfg(unix)]
#[test]
fn an_unreadable_marker_is_reported_not_skipped() {
use std::os::unix::fs::PermissionsExt;
let root = tempfile::tempdir().unwrap();
let inner = root.path().join("inner");
std::fs::create_dir_all(&inner).unwrap();
std::fs::write(root.path().join(".ctxproject"), "outer-id\n").unwrap();
let marker = inner.join(".ctxproject");
std::fs::write(&marker, "inner-id\n").unwrap();
std::fs::set_permissions(&marker, std::fs::Permissions::from_mode(0o000)).unwrap();
if std::fs::read_to_string(&marker).is_ok() {
return; }
let clues = Clues::read(&inner);
assert_eq!(clues.ctxproject, None);
let err = clues.ctxproject_error.expect("an error");
assert!(err.contains(&marker.display().to_string()), "{err}");
}
#[test]
fn clues_carry_the_git_remote() {
let repo = tempfile::tempdir().unwrap();
let git = |args: &[&str]| {
let ok = std::process::Command::new("git")
.args(args)
.current_dir(repo.path())
.output()
.map(|o| o.status.success())
.unwrap_or(false);
assert!(ok, "git {args:?}");
};
git(&["init", "-q"]);
assert_eq!(Clues::read(repo.path()).remote_url, None);
git(&[
"remote",
"add",
"origin",
"https://git.example.com/g/app.git",
]);
let sub = repo.path().join("sub");
std::fs::create_dir(&sub).unwrap();
assert_eq!(
Clues::read(&sub).remote_url.as_deref(),
Some("https://git.example.com/g/app.git")
);
}
#[test]
fn the_query_says_the_client_read_the_directory() {
let none = Clues::default().query(CWD);
assert_eq!(
none,
[
("cwd", CWD.to_string()),
("client_read", "true".to_string())
]
);
let all = Clues {
ctxproject: Some("p".into()),
ctxproject_error: Some("e".into()),
remote_url: Some("https://r".into()),
}
.query(CWD);
for (k, v) in [
("ctxproject", "p"),
("ctxproject_error", "e"),
("remote_url", "https://r"),
] {
assert!(all.contains(&(k, v.to_string())), "{all:?}");
}
}
#[tokio::test]
async fn detect_sends_the_directorys_clues() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(".ctxproject"), "my-proj\n").unwrap();
let cwd = dir.path().to_str().unwrap().to_string();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let server = format!("http://{}", listener.local_addr().unwrap());
let serve = tokio::spawn(async move {
let (mut sock, _) = listener.accept().await.unwrap();
let mut buf = vec![0u8; 8192];
let n = sock.read(&mut buf).await.unwrap();
let request = String::from_utf8_lossy(&buf[..n]).into_owned();
let body = r#"{"status":"found","via":"ctxproject","project_id":"my-proj","namespace":"my-ns"}"#;
let resp = format!(
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
body.len(),
body
);
sock.write_all(resp.as_bytes()).await.unwrap();
request
});
let got = detect(&client(Duration::from_secs(2)), &server, &cwd).await;
assert_eq!(got.unwrap(), Some("my-ns".into()));
let request_line = serve.await.unwrap().lines().next().unwrap().to_string();
assert!(
request_line.starts_with("GET /api/projects/detect?"),
"{request_line}"
);
assert!(request_line.contains("client_read=true"), "{request_line}");
assert!(
request_line.contains("ctxproject=my-proj"),
"{request_line}"
);
}
#[tokio::test]
async fn answering_hub_round_trips() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let server = format!("http://{}", listener.local_addr().unwrap());
let serve = tokio::spawn(async move {
let (mut sock, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = sock.read(&mut buf).await;
let body = r#"{"status":"not_found","namespace":"default"}"#;
let resp = format!(
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
body.len(),
body
);
sock.write_all(resp.as_bytes()).await.unwrap();
});
let got = detect(&client(Duration::from_secs(2)), &server, CWD).await;
assert_eq!(got.unwrap(), None);
serve.await.unwrap();
}
}