Merge pull request 'add FJ_FALLBACK_HOST env var' (#82) from host-fallback into main

Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/82
This commit is contained in:
Cyborus 2024-07-05 02:38:27 +00:00
commit 02e27479fc

View file

@ -173,6 +173,8 @@ impl RepoInfo {
}
};
let url = url.or_else(fallback_host);
let info = match (url, name) {
(Some(url), name) => RepoInfo { url, name },
(None, Some(_)) => eyre::bail!("cannot find repo, no host specified"),
@ -191,6 +193,18 @@ impl RepoInfo {
}
}
fn fallback_host() -> Option<Url> {
if let Some(envvar) = std::env::var_os("FJ_FALLBACK_HOST") {
let out = envvar.to_str().and_then(|x| x.parse::<Url>().ok());
if out.is_none() {
println!("warn: `FJ_FALLBACK_HOST` is not set to a valid url");
}
out
} else {
None
}
}
fn url_strip_repo_name(mut url: Url) -> eyre::Result<(Url, RepoName)> {
let mut iter = url
.path_segments()