use std::{
io::{self, Write},
path::{Path, PathBuf},
sync::Arc,
};
use fs_err as fs;
use tempfile::{TempDir, tempdir};
#[derive(Debug, Clone)]
pub struct StateStore {
root: PathBuf,
_temp_dir_drop: Option<Arc<TempDir>>,
}
impl StateStore {
pub fn from_path(root: impl Into<PathBuf>) -> Result<Self, io::Error> {
Ok(Self {
root: root.into(),
_temp_dir_drop: None,
})
}
pub fn temp() -> Result<Self, io::Error> {
let temp_dir = tempdir()?;
Ok(Self {
root: temp_dir.path().to_path_buf(),
_temp_dir_drop: Some(Arc::new(temp_dir)),
})
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn init(self) -> Result<Self, io::Error> {
let root = &self.root;
fs::create_dir_all(root)?;
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(root.join(".gitignore"))
{
Ok(mut file) => file.write_all(b"*")?,
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => (),
Err(err) => return Err(err),
}
Ok(Self {
root: fs::canonicalize(root)?,
..self
})
}
pub fn bucket(&self, state_bucket: StateBucket) -> PathBuf {
self.root.join(state_bucket.to_str())
}
pub fn from_settings(state_dir: Option<PathBuf>) -> Result<Self, io::Error> {
if let Some(state_dir) = state_dir {
Self::from_path(state_dir)
} else if let Some(data_dir) = uv_dirs::legacy_user_state_dir().filter(|dir| dir.exists()) {
Self::from_path(data_dir)
} else if let Some(data_dir) = uv_dirs::user_state_dir() {
Self::from_path(data_dir)
} else {
Self::from_path(".uv")
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum StateBucket {
ManagedPython,
Tools,
Credentials,
}
impl StateBucket {
fn to_str(self) -> &'static str {
match self {
Self::ManagedPython => "python",
Self::Tools => "tools",
Self::Credentials => "credentials",
}
}
}