use std::path::{Path, PathBuf};
use super::super::text::say;
use crate::profile::files::Endpoint;
use crate::remote::selection::Selection;
pub const CREDENTIAL_VARS: [&str; 4] = [
crate::remote::selection::TOKEN_ENV,
crate::memory::cli::DSN_ENV,
crate::login::local::TOKEN_ENV,
crate::serve::cli::TOKEN_ENV,
];
const STORE_LOCK: &str = ".store.lock";
const PROFILE_LOCK: &str = ".profile.lock";
const LOGIN: &str = "login";
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Stores {
pub protected: Vec<PathBuf>,
pub locks: Vec<PathBuf>,
pub hidden: Vec<PathBuf>,
pub unset: Vec<String>,
pub credential: bool,
pub login: bool,
}
impl Stores {
fn file(&mut self, root: PathBuf) {
if !self.protected.contains(&root) {
self.locks.push(root.join(STORE_LOCK));
self.protected.push(root);
}
}
}
pub(super) fn parent(selection: &[String], directory: &Path) -> Result<Selection, String> {
let flag = |name: &str| {
selection
.chunks(2)
.find(|pair| pair.len() == 2 && pair[0] == name)
.map(|pair| pair[1].as_str())
};
match (
flag("--profile"),
flag("--root"),
flag("--url"),
flag("--schema"),
) {
(Some(name), None, None, None) => crate::profile::resolve::selected(Some(name), directory)?
.map(|found| Selection::Profile(Box::new(found)))
.ok_or_else(|| format!("profile {name} is not installed")),
(None, Some(root), None, None) if Path::new(root).is_absolute() => {
Ok(Selection::File(root.into()))
}
(None, None, Some(url), None) => Ok(Selection::Remote {
url: url.to_string(),
token: String::new(),
}),
(None, None, None, Some(schema)) => Ok(Selection::Postgres {
dsn: String::new(),
schema: schema.to_string(),
}),
(None, None, None, None) => crate::home::home()
.map(Selection::File)
.map_err(|error| error.to_string()),
_ => Err("the server's selection does not name exactly one store".to_string()),
}
}
pub fn check(selection: &[String], directory: &Path) -> Result<Stores, String> {
let selected = parent(selection, directory)
.map_err(|error| say("sandbox_store_unknown", &[("error", &error)]))?;
let mut stores = Stores {
unset: CREDENTIAL_VARS.map(String::from).to_vec(),
..Stores::default()
};
let default = std::env::home_dir().map(|home| home.join(".neosian"));
for home in crate::home::home().ok().into_iter().chain(default) {
if !stores.protected.contains(&home) {
stores.locks.push(home.join(PROFILE_LOCK));
stores.hidden.push(home.join(LOGIN));
stores.file(home);
}
}
match selected {
Selection::File(root) => stores.file(root),
Selection::Profile(profile) => {
for binding in profile.bindings.values() {
if let Endpoint::File { root } = &binding.endpoint {
stores.file(root.clone());
}
if let Some(name) = binding.credential() {
stores.credential = true;
if !stores.unset.iter().any(|known| known == name) {
stores.unset.push(name.to_string());
}
}
}
stores.login = profile.login.is_some();
stores.credential |= stores.login;
}
Selection::Remote { .. } | Selection::Postgres { .. } => stores.credential = true,
}
Ok(stores)
}
pub fn ready(stores: &Stores) {
for lock in &stores.locks {
let store = lock.parent().is_some_and(Path::is_dir);
if store && lock.ends_with(STORE_LOCK) && lock.symlink_metadata().is_err() {
let _ = crate::store::lock::acquire(lock);
}
}
}