From c5122a4f0e2a90ce6b6dd3f08fb2b95314a2580e Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Fri, 6 Mar 2026 23:44:41 +0000 Subject: [PATCH] Extract config module and add tracing-based logging --- Cargo.lock | 75 ++++++++++++++++- crates/bough-cli/Cargo.toml | 2 + crates/bough-cli/src/config.rs | 132 +++++++++++++++++++++++++++++ crates/bough-cli/src/main.rs | 148 ++++----------------------------- 4 files changed, 226 insertions(+), 131 deletions(-) create mode 100644 crates/bough-cli/src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 98d25daa..2ad4ae23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,8 @@ dependencies = [ "facet-toml", "facet-yaml", "figue", + "tracing", + "tracing-subscriber", ] [[package]] @@ -385,7 +387,7 @@ dependencies = [ "facet-path", "hashbrown 0.16.1", "regex", - "smallvec", + "smallvec 2.0.0-alpha.12", ] [[package]] @@ -659,6 +661,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "leb128fmt" version = "0.1.0" @@ -695,6 +703,15 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc0287524726960e07b119cebd01678f852f147742ae0d925e6a520dca956126" +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -910,12 +927,27 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + [[package]] name = "smallvec" version = "2.0.0-alpha.12" @@ -986,6 +1018,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + [[package]] name = "toml_parser" version = "1.0.9+spec-1.1.0" @@ -1024,6 +1065,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec 1.15.1", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -1111,6 +1178,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "version_check" version = "0.9.5" diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index 50e6cf9b..0e519378 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -9,3 +9,5 @@ facet = "0.44" facet-toml = "0.44" facet-yaml = "0.44" figue = "2" +tracing = "0.1" +tracing-subscriber = "0.3" diff --git a/crates/bough-cli/src/config.rs b/crates/bough-cli/src/config.rs new file mode 100644 index 00000000..641cf930 --- /dev/null +++ b/crates/bough-cli/src/config.rs @@ -0,0 +1,132 @@ +use std::env; + +use facet::Facet; +use figue::{self as args, ConfigFormat, ConfigFormatError, Driver, builder}; + +#[derive(Facet, Debug)] +pub struct Cli { + #[facet(args::named, args::short = 'v', args::counted, default)] + pub verbose: u8, + + #[facet(args::subcommand)] + pub command: Command, + + #[facet(args::config, args::env_prefix = "BOUGH")] + pub config: Config, +} + +#[derive(Facet, Debug)] +#[repr(u8)] +pub enum Command { + Show { + #[facet(args::subcommand)] + what: ShowCommand, + }, + Run, +} + +#[derive(Facet, Debug)] +#[repr(u8)] +pub enum ShowCommand { + Cli, + Config, + File, +} + +#[derive(Facet, Debug)] +pub struct Config { + #[facet(default = 4)] + pub workers: u64, + + #[facet(default = "default")] + pub id: String, +} + +struct TomlFormat; + +impl ConfigFormat for TomlFormat { + fn extensions(&self) -> &[&str] { + &["toml"] + } + + fn parse(&self, contents: &str) -> Result { + facet_toml::from_str(contents).map_err(|e| ConfigFormatError::new(e.to_string())) + } +} + +struct YamlFormat; + +impl ConfigFormat for YamlFormat { + fn extensions(&self) -> &[&str] { + &["yaml", "yml"] + } + + fn parse(&self, contents: &str) -> Result { + facet_yaml::from_str(contents).map_err(|e| ConfigFormatError::new(e.to_string())) + } +} + +const CONFIG_NAMES: &[&str] = &[ + "bough.config.toml", + "bough.config.yaml", + "bough.config.yml", + "bough.config.json", + ".bough.toml", + ".bough.yaml", + ".bough.yml", + ".bough.json", + ".config/bough.toml", + ".config/bough.yaml", + ".config/bough.yml", + ".config/bough.json", +]; + +fn find_config_paths() -> Vec { + let mut paths = Vec::new(); + let mut dir = env::current_dir().ok(); + while let Some(d) = dir { + for name in CONFIG_NAMES { + paths.push(d.join(name).to_string_lossy().into_owned()); + } + dir = d.parent().map(|p| p.to_path_buf()); + } + paths +} + +pub fn resolve_config_path() -> Option { + find_config_paths() + .into_iter() + .find(|p| std::path::Path::new(p).is_file()) +} + +pub fn parse() -> Cli { + let config = builder::() + .expect("schema should be valid") + .cli(|cli| cli) + .env(|env| env) + .file(|f| { + f.default_paths(find_config_paths()) + .format(TomlFormat) + .format(YamlFormat) + }) + .build(); + + let outcome = Driver::new(config).run(); + let output = match outcome.into_result() { + Ok(output) => output, + Err(figue::DriverError::Help { text }) => { + println!("{text}"); + std::process::exit(0); + } + Err(figue::DriverError::Failed { report }) => { + eprintln!("{}", report.render_pretty()); + std::process::exit(1); + } + Err(e) => { + eprintln!("{e:?}"); + std::process::exit(1); + } + }; + output.print_warnings(); + output.get_silent() +} diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 48d3674d..ec2739bb 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -1,133 +1,23 @@ -use std::env; +mod config; -use facet::Facet; -use figue::{self as args, ConfigFormat, ConfigFormatError, Driver, builder}; - -#[derive(Facet, Debug)] -struct Cli { - #[facet(args::subcommand)] - command: Command, - - #[facet(args::config, args::env_prefix = "BOUGH")] - config: Config, -} - -#[derive(Facet, Debug)] -#[repr(u8)] -enum Command { - Show { - #[facet(args::subcommand)] - what: ShowCommand, - }, - Run, -} - -#[derive(Facet, Debug)] -#[repr(u8)] -enum ShowCommand { - Cli, - Config, - File, -} - -#[derive(Facet, Debug)] -struct Config { - #[facet(default = 4)] - workers: u64, - - #[facet(default = "default")] - id: String, -} - -struct TomlFormat; - -impl ConfigFormat for TomlFormat { - fn extensions(&self) -> &[&str] { - &["toml"] - } - - fn parse(&self, contents: &str) -> Result { - facet_toml::from_str(contents).map_err(|e| ConfigFormatError::new(e.to_string())) - } -} - -struct YamlFormat; - -impl ConfigFormat for YamlFormat { - fn extensions(&self) -> &[&str] { - &["yaml", "yml"] - } - - fn parse(&self, contents: &str) -> Result { - facet_yaml::from_str(contents).map_err(|e| ConfigFormatError::new(e.to_string())) - } -} - -const CONFIG_NAMES: &[&str] = &[ - "bough.config.toml", - "bough.config.yaml", - "bough.config.yml", - "bough.config.json", - ".bough.toml", - ".bough.yaml", - ".bough.yml", - ".bough.json", - ".config/bough.toml", - ".config/bough.yaml", - ".config/bough.yml", - ".config/bough.json", -]; - -fn find_config_paths() -> Vec { - let mut paths = Vec::new(); - let mut dir = env::current_dir().ok(); - while let Some(d) = dir { - for name in CONFIG_NAMES { - paths.push(d.join(name).to_string_lossy().into_owned()); - } - dir = d.parent().map(|p| p.to_path_buf()); - } - paths -} - -fn resolve_config_path() -> Option { - find_config_paths() - .into_iter() - .find(|p| std::path::Path::new(p).is_file()) -} +use config::{Command, ShowCommand, parse, resolve_config_path}; +use tracing::Level; fn main() { - let config = builder::() - .expect("schema should be valid") - .cli(|cli| cli) - .env(|env| env) - .file(|f| { - f.default_paths(find_config_paths()) - .format(TomlFormat) - .format(YamlFormat) - }) - .build(); - - let outcome = Driver::new(config).run(); - let output = match outcome.into_result() { - Ok(output) => output, - Err(figue::DriverError::Help { text }) => { - println!("{text}"); - std::process::exit(0); - } - Err(figue::DriverError::Failed { report }) => { - eprintln!("{}", report.render_pretty()); - std::process::exit(1); - } - Err(e) => { - eprintln!("{e:?}"); - std::process::exit(1); - } + let cli = parse(); + + let log_level = match cli.verbose { + 0 => Level::ERROR, + 1 => Level::WARN, + 2 => Level::INFO, + 3 => Level::DEBUG, + _ => Level::TRACE, }; - output.print_warnings(); + tracing_subscriber::fmt() + .with_max_level(log_level) + .init(); let config_path = resolve_config_path(); - let (cli, _report) = output.into_parts(); match cli.command { Command::Show { ref what } => match what { @@ -144,12 +34,10 @@ fn main() { println!("workers: {}", cli.config.workers); println!("id: {}", cli.config.id); } - ShowCommand::File => { - match config_path { - Some(ref path) => println!("{path}"), - None => println!("no config file found"), - } - } + ShowCommand::File => match config_path { + Some(ref path) => println!("{path}"), + None => println!("no config file found"), + }, }, Command::Run => { println!("run: not yet implemented"); -- knotmirror 0.0.1