pub mod creds;
pub mod fetch;
pub mod types;
pub mod vendor;
use std::path::{Path, PathBuf};
use crate::config::ModelStudioConfig;
use crate::error::Result;
pub const APP_DIR_NAME: &str = ".bailian";
pub const CONFIG_FILE_NAME: &str = "config.json";
pub const CONFIG_DIR_ENV: &str = "BAILIAN_CONFIG_DIR";
pub fn config_path_in(cfg: &ModelStudioConfig, env_dir: Option<PathBuf>, home: &Path) -> PathBuf {
cfg.config_dir
.clone()
.or(env_dir)
.unwrap_or_else(|| home.join(APP_DIR_NAME))
.join(CONFIG_FILE_NAME)
}
pub fn config_path(cfg: &ModelStudioConfig) -> Result<PathBuf> {
let env_dir = std::env::var_os(CONFIG_DIR_ENV)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
.map(|p| expand_tilde(&p));
Ok(config_path_in(cfg, env_dir, &crate::cache::home_dir()?))
}
fn expand_tilde(p: &Path) -> PathBuf {
let home = crate::cache::home_dir().unwrap_or_else(|_| p.to_path_buf());
expand_tilde_in(p, &home)
}
fn expand_tilde_in(p: &Path, home: &Path) -> PathBuf {
let Some(s) = p.to_str() else {
return p.to_path_buf();
};
let rest = if s == "~" {
""
} else if let Some(r) = s.strip_prefix("~/") {
r
} else {
return p.to_path_buf();
};
if rest.is_empty() {
home.to_path_buf()
} else {
home.join(rest)
}
}
pub fn resolve_credentials(cfg: &ModelStudioConfig) -> Result<creds::Credentials> {
creds::read_from(&config_path(cfg)?)
}
pub use fetch::{FetchOutcome, fetch_snapshot_with};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_path_lives_in_the_bl_cli_dir() {
let cfg = ModelStudioConfig::default();
let path = config_path_in(&cfg, None, Path::new("/home/u"));
assert_eq!(path, PathBuf::from("/home/u/.bailian/config.json"));
}
#[test]
fn a_configured_dir_wins_over_the_env_and_the_default() {
let cfg = ModelStudioConfig {
config_dir: Some(PathBuf::from("/cfg/bl")),
..ModelStudioConfig::default()
};
let path = config_path_in(&cfg, Some(PathBuf::from("/env/bl")), Path::new("/home/u"));
assert_eq!(path, PathBuf::from("/cfg/bl/config.json"));
}
#[test]
fn the_env_dir_wins_over_the_default() {
let cfg = ModelStudioConfig::default();
let path = config_path_in(&cfg, Some(PathBuf::from("/env/bl")), Path::new("/home/u"));
assert_eq!(path, PathBuf::from("/env/bl/config.json"));
}
#[test]
fn a_tilde_in_the_env_dir_expands() {
assert_eq!(
expand_tilde_in(Path::new("~/bl"), Path::new("/home/u")),
PathBuf::from("/home/u/bl")
);
assert_eq!(
expand_tilde_in(Path::new("~"), Path::new("/home/u")),
PathBuf::from("/home/u")
);
assert_eq!(
expand_tilde_in(Path::new("~other/config"), Path::new("/home/u")),
PathBuf::from("~other/config")
);
}
}