Skip to content

Commit

Permalink
settings refactor (#1174)
Browse files Browse the repository at this point in the history
* refactor: parse CLI into settings partial

* whitespace

* refactor: memoize partial settings

* readme
  • Loading branch information
jdx authored Dec 14, 2023
1 parent 2e74638 commit 6a6441e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub mod tests {
use super::*;

pub fn cli_run(args: &Vec<String>) -> Result<()> {
Config::reset();
*env::ARGS.write().unwrap() = args.clone();
STDOUT.lock().unwrap().clear();
STDERR.lock().unwrap().clear();
Expand Down
20 changes: 12 additions & 8 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use rayon::prelude::*;

pub use settings::{Settings, SettingsPartial};

use crate::cli::Cli;
use crate::config::config_file::legacy_version::LegacyVersionFile;
use crate::config::config_file::rtx_toml::RtxToml;
use crate::config::config_file::{ConfigFile, ConfigFileType};

use crate::config::tracking::Tracker;
use crate::file::display_path;
use crate::plugins::core::{PluginMap, CORE_PLUGINS, EXPERIMENTAL_CORE_PLUGINS};
Expand Down Expand Up @@ -51,11 +51,12 @@ pub struct Config {

impl Config {
pub fn load() -> Result<Self> {
let cli_settings = Cli::new().settings(&env::ARGS.read().unwrap());
Settings::add_partial(cli_settings);
let global_config = load_rtxrc()?;
Settings::add_partial(global_config.settings()?);
let config_filenames = load_config_filenames(&BTreeMap::new());
let settings = Settings::default_builder()
.preloaded(global_config.settings()?)
.load()?;
let settings = Settings::default_builder().load()?;
let plugins = load_plugins(&settings)?;
let config_files = load_all_config_files(
&settings,
Expand All @@ -64,11 +65,10 @@ impl Config {
&BTreeMap::new(),
ConfigMap::new(),
)?;
let mut settings = Settings::default_builder();
for cf in config_files.values() {
settings = settings.preloaded(cf.settings()?);
Settings::add_partial(cf.settings()?);
}
let mut settings = settings.load()?;
let mut settings = Settings::default_builder().load()?;
if settings.raw {
settings.verbose = true;
settings.jobs = 1;
Expand Down Expand Up @@ -123,7 +123,6 @@ impl Config {

Ok(config)
}

pub fn get_shorthands(&self) -> &Shorthands {
self.shorthands
.get_or_init(|| get_shorthands(&self.settings))
Expand Down Expand Up @@ -241,6 +240,11 @@ impl Config {
crate::runtime_symlinks::rebuild(self)?;
Ok(())
}

#[cfg(test)]
pub fn reset() {
Settings::reset();
}
}

fn get_project_root(config_files: &ConfigMap) -> Option<PathBuf> {
Expand Down
60 changes: 39 additions & 21 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use confique::env::parse::{list_by_colon, list_by_comma};
use confique::{Builder, Config, Partial};

use crate::cli::Cli;
use log::LevelFilter;
use serde_derive::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{Debug, Display, Formatter};
use std::path::PathBuf;
use std::sync::{Once, RwLock};

use confique::{Builder, Config, Partial};
use log::LevelFilter;
use serde_derive::{Deserialize, Serialize};

use crate::env;

#[derive(Config, Debug, Clone)]
#[config(partial_attr(derive(Debug)))]
#[config(partial_attr(derive(Debug, Clone)))]
pub struct Settings {
#[config(env = "RTX_EXPERIMENTAL", default = false)]
pub experimental: bool,
Expand Down Expand Up @@ -54,26 +55,38 @@ impl Default for Settings {
Settings::default_builder().load().unwrap()
}
}

static PARTIALS: RwLock<Vec<SettingsPartial>> = RwLock::new(Vec::new());

impl Settings {
pub fn default_builder() -> Builder<Self> {
let mut p = SettingsPartial::empty();
let args = env::ARGS.read().unwrap();
if *env::CI {
p.yes = Some(true);
}
if *env::RTX_LOG_LEVEL < LevelFilter::Info {
p.verbose = Some(true);
}
for arg in &*args {
if arg == "--" {
break;
pub fn add_partial(partial: SettingsPartial) {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let mut p = SettingsPartial::empty();
if *env::CI {
p.yes = Some(true);
}
if *env::RTX_LOG_LEVEL < LevelFilter::Info {
p.verbose = Some(true);
}
if arg == "--raw" {
p.raw = Some(true);
for arg in &*env::ARGS.read().unwrap() {
if arg == "--" {
break;
}
if arg == "--raw" {
p.raw = Some(true);
}
}
PARTIALS.write().unwrap().push(p);
});
PARTIALS.write().unwrap().push(partial);
}
pub fn default_builder() -> Builder<Self> {
let mut b = Self::builder();
for partial in PARTIALS.read().unwrap().iter() {
b = b.preloaded(partial.clone());
}
let cli = Cli::new().settings(&args);
Self::builder().preloaded(cli).preloaded(p).env()
b.env()
}

pub fn to_index_map(&self) -> BTreeMap<String, String> {
Expand Down Expand Up @@ -129,6 +142,11 @@ impl Settings {
map.insert("yes".into(), self.yes.to_string());
map
}

#[cfg(test)]
pub fn reset() {
PARTIALS.write().unwrap().clear();
}
}

impl Display for Settings {
Expand Down

0 comments on commit 6a6441e

Please sign in to comment.