Skip to content

Commit

Permalink
Allow etc system-wide config (#137)
Browse files Browse the repository at this point in the history
 Support /etc/ system-wide configuration
  • Loading branch information
C-Loftus authored Mar 10, 2024
1 parent 3dd192f commit dc84bd9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 43 additions & 14 deletions odilia/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ pub struct ScreenReaderState {
pub cache: Arc<Cache>,
}

enum ConfigType {
CliOverride,
XDGConfigHome,
Etc,
CreateDefault,
}

impl ScreenReaderState {
#[tracing::instrument]
pub async fn new(
Expand All @@ -54,25 +61,47 @@ impl ScreenReaderState {

tracing::debug!("Reading configuration");

let config_path = if config_override.is_none() {
let xdg_dirs = xdg::BaseDirectories::with_prefix("odilia").expect(
"unable to find the odilia config directory according to the xdg dirs specification",
);
let config_path = xdg_dirs.place_config_file("config.toml").expect(
"unable to place configuration file. Maybe your system is readonly?",
);
// In order of prioritization, do configuration via cli, then XDG_CONFIG_HOME, then /etc/,
// Otherwise create it in XDG_CONFIG_HOME
let config_type = if config_override.is_some() {
ConfigType::CliOverride

if !config_path.exists() {
fs::write(&config_path, include_str!("../config.toml"))
.expect("Unable to copy default config file.");
}
config_path.to_str().ok_or(ConfigError::PathNotFound)?.to_owned()
// First check makes sure unwrap is safe
} else if xdg::BaseDirectories::with_prefix("odilia").is_ok()
&& xdg::BaseDirectories::with_prefix("odilia")
.expect("This error should never occur")
.find_config_file("config.toml")
.is_some()
{
ConfigType::XDGConfigHome
} else if std::path::Path::new("/etc/odilia/config.toml").exists() {
ConfigType::Etc
} else {
config_override
ConfigType::CreateDefault
};

let config_path = match config_type {
ConfigType::CliOverride => config_override
.expect("Config override was provided but is None")
.to_str()
.ok_or(ConfigError::PathNotFound)?
.to_owned()
.to_owned(),
ConfigType::XDGConfigHome | ConfigType::CreateDefault => {
let xdg_dirs = xdg::BaseDirectories::with_prefix("odilia").expect(
"unable to find the odilia config directory according to the xdg dirs specification",
);

let config_path = xdg_dirs.place_config_file("config.toml").expect(
"unable to place configuration file. Maybe your system is readonly?",
);

if !config_path.exists() {
fs::write(&config_path, include_str!("../config.toml"))
.expect("Unable to copy default config file.");
}
config_path.to_str().ok_or(ConfigError::PathNotFound)?.to_owned()
}
ConfigType::Etc => "/etc/odilia/config.toml".to_owned(),
};

tracing::debug!(path=%config_path, "loading configuration file");
Expand Down

0 comments on commit dc84bd9

Please sign in to comment.