Watch config path even if it didn't exist at startup

This commit is contained in:
Ivan Molodetskikh
2024-01-18 11:02:15 +04:00
parent 6beef26662
commit 0f85c79548
5 changed files with 36 additions and 29 deletions
Generated
-1
View File
@@ -1744,7 +1744,6 @@ name = "niri-config"
version = "0.1.0-alpha.3"
dependencies = [
"bitflags 2.4.1",
"directories",
"knuffel",
"miette",
"smithay",
-1
View File
@@ -9,7 +9,6 @@ repository.workspace = true
[dependencies]
bitflags.workspace = true
directories.workspace = true
knuffel = "3.2.0"
miette = "5.10.0"
smithay.workspace = true
+5 -17
View File
@@ -1,11 +1,10 @@
#[macro_use]
extern crate tracing;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use bitflags::bitflags;
use directories::ProjectDirs;
use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler};
use smithay::input::keyboard::keysyms::KEY_NoSymbol;
use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE};
@@ -479,30 +478,19 @@ impl Default for DebugConfig {
}
impl Config {
pub fn load(path: Option<PathBuf>) -> miette::Result<(Self, PathBuf)> {
pub fn load(path: &Path) -> miette::Result<Self> {
let _span = tracy_client::span!("Config::load");
Self::load_internal(path).context("error loading config")
}
fn load_internal(path: Option<PathBuf>) -> miette::Result<(Self, PathBuf)> {
let path = if let Some(path) = path {
path
} else {
let mut path = ProjectDirs::from("", "", "niri")
.ok_or_else(|| miette!("error retrieving home directory"))?
.config_dir()
.to_owned();
path.push("config.kdl");
path
};
let contents = std::fs::read_to_string(&path)
fn load_internal(path: &Path) -> miette::Result<Self> {
let contents = std::fs::read_to_string(path)
.into_diagnostic()
.with_context(|| format!("error reading {path:?}"))?;
let config = Self::parse("config.kdl", &contents).context("error parsing")?;
debug!("loaded config from {path:?}");
Ok((config, path))
Ok(config)
}
pub fn parse(filename: &str, text: &str) -> Result<Self, knuffel::Error> {
+29 -8
View File
@@ -28,6 +28,7 @@ use std::process::Command;
use std::{env, mem};
use clap::{Parser, Subcommand};
use directories::ProjectDirs;
#[cfg(not(feature = "xdp-gnome-screencast"))]
use dummy_pw_utils as pw_utils;
use git_version::git_version;
@@ -132,7 +133,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(subcommand) = cli.subcommand {
match subcommand {
Sub::Validate { config } => {
Config::load(config)?;
let path = config
.or_else(default_config_path)
.expect("error getting config path");
Config::load(&path)?;
info!("config is valid");
return Ok(());
}
@@ -151,13 +155,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);
// Load the config.
let (mut config, path) = match Config::load(cli.config) {
Ok((config, path)) => (config, Some(path)),
Err(err) => {
warn!("{err:?}");
(Config::default(), None)
}
};
let path = cli.config.or_else(default_config_path);
let mut config = path
.as_deref()
.and_then(|path| match Config::load(path) {
Ok(config) => Some(config),
Err(err) => {
warn!("{err:?}");
None
}
})
.unwrap_or_default();
animation::ANIMATION_SLOWDOWN.store(config.debug.animation_slowdown, Ordering::Relaxed);
let spawn_at_startup = mem::take(&mut config.spawn_at_startup);
@@ -264,3 +274,14 @@ fn import_env_to_systemd() {
}
}
}
fn default_config_path() -> Option<PathBuf> {
let Some(dirs) = ProjectDirs::from("", "", "niri") else {
warn!("error retrieving home directory");
return None;
};
let mut path = dirs.config_dir().to_owned();
path.push("config.kdl");
Some(path)
}
+2 -2
View File
@@ -536,8 +536,8 @@ impl State {
pub fn reload_config(&mut self, path: PathBuf) {
let _span = tracy_client::span!("State::reload_config");
let config = match Config::load(Some(path)) {
Ok((config, _)) => config,
let config = match Config::load(&path) {
Ok(config) => config,
Err(err) => {
warn!("{:?}", err.context("error loading config"));
return;