Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: parse CLI into settings partial #1173

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::cli::self_update::SelfUpdate;
use clap::{FromArgMatches, Subcommand};
use color_eyre::Result;
use confique::Partial;

use crate::config::Config;
use crate::config::{Config, SettingsPartial};

mod activate;
mod alias;
Expand Down Expand Up @@ -194,26 +195,13 @@
)
}

pub fn run(self, mut config: Config, args: &Vec<String>) -> Result<()> {
pub fn run(self, config: Config, args: &Vec<String>) -> Result<()> {
debug!("{}", &args.join(" "));
if args[1..] == ["-v"] {
// normally this would be considered --verbose
return version::Version {}.run(config);
}
let matches = self.command.get_matches_from(args);
if let Some(true) = matches.get_one::<bool>("yes") {
config.settings.yes = true;
}
if let Some(true) = matches.get_one::<bool>("quiet") {
config.settings.quiet = true;
}
if *matches.get_one::<u8>("verbose").unwrap() > 0 {
config.settings.verbose = true;
}
if config.settings.raw {
config.settings.jobs = 1;
config.settings.verbose = true;
}
if let Some((command, sub_m)) = matches.subcommand() {
if command == "self-update" {
return SelfUpdate::from_arg_matches(sub_m)?.run(config);
Expand All @@ -223,6 +211,27 @@
let cmd = Commands::from_arg_matches(&matches)?;
cmd.run(config)
}

pub fn settings(self, args: &Vec<String>) -> SettingsPartial {
let mut s = SettingsPartial::empty();
if let Ok(m) = self.command.try_get_matches_from(args) {
if let Some(true) = m.get_one::<bool>("yes") {
s.yes = Some(true);

Check warning on line 219 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L219

Added line #L219 was not covered by tests
}
if let Some(true) = m.get_one::<bool>("quiet") {
s.quiet = Some(true);

Check warning on line 222 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L222

Added line #L222 was not covered by tests
}
if *m.get_one::<u8>("verbose").unwrap() > 0 {
s.verbose = Some(true);

Check warning on line 225 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L225

Added line #L225 was not covered by tests
}
}
// if let Some(true) = m.get_one::<bool>("trace") {
// s.log_level = Some(true);
// }
// TODO: log_level/debug/trace

s
}
}

impl Default for Cli {
Expand Down
11 changes: 3 additions & 8 deletions src/config/config_file/rtx_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,9 @@
match v.as_str() {
Some(filename) => {
let path = self.path.parent().unwrap().join(filename);
let dotenv = match dotenvy::from_path_iter(&path) {
Ok(dotenv) => dotenv,
Err(e) => Err(eyre!(
"failed to parse dotenv file: {}\n{:#}",
path.display(),
e
))?,
};
let dotenv = dotenvy::from_path_iter(&path).wrap_err_with(|| {
eyre!("failed to parse dotenv file: {}", display_path(&path))

Check warning on line 94 in src/config/config_file/rtx_toml.rs

View check run for this annotation

Codecov / codecov/patch

src/config/config_file/rtx_toml.rs#L94

Added line #L94 was not covered by tests
})?;
for item in dotenv {
let (k, v) = item?;
self.env.insert(k, v);
Expand Down
8 changes: 6 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use itertools::Itertools;
use once_cell::sync::OnceCell;
use rayon::prelude::*;

pub use settings::Settings;
pub use settings::{Settings, SettingsPartial};

use crate::config::config_file::legacy_version::LegacyVersionFile;
use crate::config::config_file::rtx_toml::RtxToml;
Expand Down Expand Up @@ -68,7 +68,11 @@ impl Config {
for cf in config_files.values() {
settings = settings.preloaded(cf.settings()?);
}
let settings = settings.load()?;
let mut settings = settings.load()?;
if settings.raw {
settings.verbose = true;
settings.jobs = 1;
}
trace!("Settings: {:#?}", settings);

let legacy_files = load_legacy_files(&settings, &plugins);
Expand Down
7 changes: 5 additions & 2 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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};
Expand Down Expand Up @@ -56,21 +57,23 @@ impl Default for Settings {
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 &*env::ARGS.read().unwrap() {
for arg in &*args {
if arg == "--" {
break;
}
if arg == "--raw" {
p.raw = Some(true);
}
}
Self::builder().preloaded(p).env()
let cli = Cli::new().settings(&args);
Self::builder().preloaded(cli).preloaded(p).env()
}

pub fn to_index_map(&self) -> BTreeMap<String, String> {
Expand Down