Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ impl Server {
&self.base
}

pub fn get(&self, path: &str) -> Result<Response> {
let url = format!("{}{path}", self.base);
pub fn get_from_root(&self, path: &str) -> Result<Response> {
self.fetch(format!("{}{path}", service_root(&self.base)))
}

fn fetch(&self, url: String) -> Result<Response> {
self.authenticated(self.client.get(&url))
.send()
.with_context(|| format!("could not reach {url}"))
Expand All @@ -57,6 +60,13 @@ impl Server {
}
}

pub fn service_root(base: &str) -> &str {
match base.rsplit_once("/-/") {
Some((root, forge)) if !forge.is_empty() && !forge.contains('/') => root,
_ => base,
}
}

pub fn resolve_token(explicit: Option<String>) -> Option<String> {
explicit
.or_else(|| std::env::var("LFSX_TOKEN").ok())
Expand All @@ -70,3 +80,6 @@ pub fn split_namespace(repository: &str) -> Result<(&str, &str)> {
_ => bail!("expected a repository as org/repo, got {repository:?}"),
}
}

#[cfg(test)]
mod tests;
29 changes: 29 additions & 0 deletions cli/src/client/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::*;

#[test]
fn the_health_routes_are_found_above_a_named_forge() {
assert_eq!(
service_root("https://lfs.example.com/-/work"),
"https://lfs.example.com"
);
assert_eq!(
service_root("https://example.com/lfs/-/work"),
"https://example.com/lfs"
);
}

#[test]
fn a_base_without_a_forge_is_its_own_root() {
assert_eq!(
service_root("https://lfs.example.com"),
"https://lfs.example.com"
);
assert_eq!(
service_root("https://example.com/lfs"),
"https://example.com/lfs"
);
assert_eq!(
service_root("https://lfs.example.com/-/"),
"https://lfs.example.com/-/"
);
}
4 changes: 2 additions & 2 deletions cli/src/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ impl Report {
pub fn run(server: &Server, repository: Option<&str>) -> Result<Report> {
let mut report = Report::new();

match server.get("/health") {
match server.get_from_root("/health") {
Ok(response) if response.status().is_success() => report.pass("the server is up"),
Ok(response) => report.fail(format!("/health answered {}", response.status())),
Err(error) => report.fail(format!("{error:#}")),
}

match server.get("/ready") {
match server.get_from_root("/ready") {
Ok(response) if response.status().is_success() => {
report.pass("the storage root is writable")
}
Expand Down
Loading