Skip to main content

blyte_cli/
approval_file.rs

1use std::ffi::OsStr;
2use std::path::PathBuf;
3
4pub fn path_from(xdg: Option<&OsStr>, home: Option<&OsStr>) -> Option<PathBuf> {
5    if let Some(dir) = xdg.filter(|v| !v.is_empty()) {
6        return Some(PathBuf::from(dir).join("blyte").join("allow-unsandboxed"));
7    }
8    home.filter(|v| !v.is_empty())
9        .map(|h| PathBuf::from(h).join(".config/blyte/allow-unsandboxed"))
10}
11
12pub fn load() -> Result<Vec<String>, String> {
13    let Some(path) = path_from(
14        std::env::var_os("XDG_CONFIG_HOME").as_deref(),
15        std::env::var_os("HOME").as_deref(),
16    ) else {
17        return Ok(Vec::new());
18    };
19    let raw = match std::fs::read_to_string(&path) {
20        Ok(text) => text,
21        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
22        Err(e) => return Err(format!("cannot read {}: {e}", path.display())),
23    };
24    Ok(raw
25        .lines()
26        .map(str::trim)
27        .filter(|line| !line.is_empty())
28        .map(String::from)
29        .collect())
30}