Files
uu-remote-linux/src/config.rs
T
VallierDevandClaude Opus 5.5 502635b156 Initial import of uu-remote-linux
Unofficial Linux host for NetEase UU Remote: X11 (production-tested on
GNOME/Ubuntu 24.04), experimental Wayland (Hyprland) and portal
(GNOME/KDE) backends; video, input, clipboard, file transfer, terminal,
sound, quality/fps/resolution settings, lock/unlock and system actions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 23:53:16 +08:00

44 lines
2.1 KiB
Rust

//! Runtime configuration. Nothing machine- or account-specific is compiled in.
//! File: $UU_CONFIG, else $XDG_CONFIG_HOME/uuremote/config.json, else ~/.config/uuremote/config.json.
//! Environment variables override file values. Keep the file private (chmod 600).
use std::path::PathBuf;
use std::sync::OnceLock;
pub fn path() -> PathBuf {
if let Some(p) = std::env::var_os("UU_CONFIG") { return PathBuf::from(p); }
let base = std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))
.unwrap_or_else(|| PathBuf::from("."));
base.join("uuremote/config.json")
}
/// Re-reads the config file (used after `login` rewrites it while the server runs).
pub fn read_fresh() -> Result<Option<serde_json::Value>, String> {
let p = path();
match std::fs::read(&p) {
Ok(bytes) => serde_json::from_slice(&bytes).map(Some).map_err(|e| format!("invalid config {}: {e}", p.display())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(format!("cannot read config {}: {e}", p.display())),
}
}
/// Parsed config file, or None when absent. A malformed file is an error, not silently ignored.
pub fn file() -> Result<Option<&'static serde_json::Value>, String> {
static FILE: OnceLock<Result<Option<serde_json::Value>, String>> = OnceLock::new();
FILE.get_or_init(|| {
let p = path();
match std::fs::read(&p) {
Ok(bytes) => serde_json::from_slice(&bytes).map(Some).map_err(|e| format!("invalid config {}: {e}", p.display())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(format!("cannot read config {}: {e}", p.display())),
}
}).as_ref().map(|v| v.as_ref()).map_err(Clone::clone)
}
/// Optional bundled FFmpeg prefix: UU_FFMPEG_ROOT or config "ffmpeg_root".
pub fn ffmpeg_root() -> Option<PathBuf> {
std::env::var_os("UU_FFMPEG_ROOT").filter(|v| !v.is_empty()).map(PathBuf::from)
.or_else(|| file().ok().flatten().and_then(|f| f.get("ffmpeg_root")).and_then(|v| v.as_str()).map(PathBuf::from))
}