use std::path::Path;
use aristo_core::auth::{
data_plane_base, derive_repo_full_name, fetch_org_repos, repo_segment_for, AuthError,
ResolvedCreds, SERVER_ENV_VAR,
};
use crate::workspace::Workspace;
pub(crate) struct Target {
pub base_url: String,
pub repo: String,
}
pub(crate) fn github_repo_for(start: &Path) -> Result<String, AuthError> {
if let Some(r) = std::env::var("ARISTO_REPO")
.ok()
.filter(|v| !v.trim().is_empty())
{
return Ok(r.trim().to_string());
}
derive_repo_full_name(start)
}
pub(crate) fn resolve_target(creds: &ResolvedCreds, start: &Path) -> Result<Target, AuthError> {
warn_if_instance_url_set();
let base_url = data_plane_base(std::env::var(SERVER_ENV_VAR).ok().as_deref(), &creds.server);
let github_repo = github_repo_for(start)?;
let repos = fetch_org_repos(&base_url, &creds.token)?;
let repo = repo_segment_for(&repos, &github_repo)
.ok_or_else(|| AuthError::RepoNotInOrg {
github_repo: github_repo.clone(),
server: base_url.clone(),
})?
.to_string();
Ok(Target { base_url, repo })
}
pub(crate) fn checkout_start() -> std::path::PathBuf {
Workspace::find(None)
.map(|ws| ws.root)
.or_else(|_| std::env::current_dir())
.unwrap_or_else(|_| std::path::PathBuf::from("."))
}
fn warn_if_instance_url_set() {
let Some(url) = Workspace::find(None)
.ok()
.and_then(|ws| ws.load_config().instance.url)
else {
return;
};
eprintln!(
"warning: aristo.toml `[instance] url = \"{url}\"` is ignored — the data plane is \
the credential's server, and {SERVER_ENV_VAR} overrides it. Remove the section."
);
}