Get the default directories for the current platform. Returns an error if the platform director(y/ies) cannot be found.
()
| 223 | /// |
| 224 | /// Returns an error if the platform director(y/ies) cannot be found. |
| 225 | pub fn platform_defaults() -> anyhow::Result<Self> { |
| 226 | #[cfg(windows)] |
| 227 | { |
| 228 | let data_dir = dirs::data_local_dir().ok_or_else(|| anyhow::anyhow!("Could not find LocalAppData"))?; |
| 229 | let root_dir = RootDir(data_dir.joined("SpacetimeDB")); |
| 230 | Ok(Self::from_root_dir(&root_dir)) |
| 231 | } |
| 232 | #[cfg(not(windows))] |
| 233 | { |
| 234 | // `dirs` doesn't use XDG base dirs on macOS, which we want to do, |
| 235 | // so we use the `xdg` crate instead. |
| 236 | let base_dirs = xdg::BaseDirectories::with_prefix("spacetime")?; |
| 237 | // bin_home should really be in the xdg crate |
| 238 | let xdg_bin_home = std::env::var_os("XDG_BIN_HOME") |
| 239 | .map(std::path::PathBuf::from) |
| 240 | .filter(|p| p.is_absolute()) |
| 241 | .unwrap_or_else(|| { |
| 242 | #[allow(deprecated)] // this is fine on non-windows platforms |
| 243 | std::env::home_dir().unwrap().joined(".local/bin") |
| 244 | }); |
| 245 | |
| 246 | let exe_name = "spacetime"; |
| 247 | |
| 248 | Ok(Self { |
| 249 | cli_config_dir: cli::ConfigDir(base_dirs.get_config_home()), |
| 250 | cli_bin_file: cli::BinFile(xdg_bin_home.join(exe_name)), |
| 251 | cli_bin_dir: cli::BinDir(base_dirs.get_data_file("bin")), |
| 252 | data_dir: server::ServerDataDir(base_dirs.get_data_file("data")), |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | pub fn from_root_dir(dir: &RootDir) -> Self { |
| 258 | Self { |