feat(xy): XDG path resolution
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
mod paths;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
mod pidfile;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("xy: not implemented yet");
|
let p = paths::Paths::resolve().unwrap();
|
||||||
|
eprintln!("xy: socket would be at {}", p.socket.display());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
use etcetera::base_strategy::{BaseStrategy, Xdg};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Paths {
|
||||||
|
pub config_dir: PathBuf,
|
||||||
|
pub state_dir: PathBuf,
|
||||||
|
pub log_dir: PathBuf,
|
||||||
|
pub socket: PathBuf,
|
||||||
|
pub pidfile: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Paths {
|
||||||
|
pub fn resolve() -> std::io::Result<Self> {
|
||||||
|
let xdg = Xdg::new().map_err(std::io::Error::other)?;
|
||||||
|
let config_dir = xdg.config_dir().join("xy").join("servers");
|
||||||
|
let state_dir = xdg.state_dir().unwrap_or_else(|| xdg.data_dir()).join("xy");
|
||||||
|
let log_dir = state_dir.join("logs");
|
||||||
|
let socket = std::env::var_os("XDG_RUNTIME_DIR")
|
||||||
|
.map(|p| PathBuf::from(p).join("xy.sock"))
|
||||||
|
.unwrap_or_else(|| state_dir.join("xy.sock"));
|
||||||
|
let pidfile = state_dir.join("xy.pid");
|
||||||
|
Ok(Self { config_dir, state_dir, log_dir, socket, pidfile })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ensure_dirs(&self) -> std::io::Result<()> {
|
||||||
|
std::fs::create_dir_all(&self.state_dir)?;
|
||||||
|
std::fs::create_dir_all(&self.log_dir)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn resolves_without_panicking() {
|
||||||
|
let p = Paths::resolve().unwrap();
|
||||||
|
assert!(p.pidfile.starts_with(&p.state_dir));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user