diff --git a/Cargo.lock b/Cargo.lock index 7e3a92e..f502af4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,6 +538,7 @@ dependencies = [ "anyhow", "async-trait", "clap 4.4.4", + "crunch-file", "thiserror", "tokio", "tracing", @@ -572,6 +573,7 @@ dependencies = [ "serde", "tokio", "toml_edit", + "tracing", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9c610ad..7ce294b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ crunch-traits = { path = "crates/crunch-traits" } crunch-envelope = { path = "crates/crunch-envelope" } crunch-in-memory = { path = "crates/crunch-in-memory" } crunch-nats = { path = "crates/crunch-nats" } +crunch-file = {path = "crates/crunch-file"} anyhow = { version = "1.0.71" } tokio = { version = "1", features = ["full"] } diff --git a/crates/crunch-cli/Cargo.toml b/crates/crunch-cli/Cargo.toml index c442cf0..d2a9053 100644 --- a/crates/crunch-cli/Cargo.toml +++ b/crates/crunch-cli/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +crunch-file.workspace = true + anyhow.workspace = true tracing.workspace = true tokio.workspace = true diff --git a/crates/crunch-cli/src/logging.rs b/crates/crunch-cli/src/logging.rs new file mode 100644 index 0000000..091beba --- /dev/null +++ b/crates/crunch-cli/src/logging.rs @@ -0,0 +1,41 @@ +use clap::ValueEnum; +use tracing::Level; + +#[derive(Clone, ValueEnum)] +pub enum LogArg { + None, + Trace, + Debug, + Info, + Warn, + Error, +} + +impl LogArg { + pub fn init_logging(&self) { + match self { + LogArg::None => {} + LogArg::Trace => { + tracing_subscriber::fmt() + .with_max_level(Level::TRACE) + .init(); + } + LogArg::Debug => { + tracing_subscriber::fmt() + .with_max_level(Level::DEBUG) + .init(); + } + LogArg::Info => { + tracing_subscriber::fmt().with_max_level(Level::INFO).init(); + } + LogArg::Warn => { + tracing_subscriber::fmt().with_max_level(Level::WARN).init(); + } + LogArg::Error => { + tracing_subscriber::fmt() + .with_max_level(Level::ERROR) + .init(); + } + } + } +} diff --git a/crates/crunch-cli/src/main.rs b/crates/crunch-cli/src/main.rs index c0164b1..0ff08aa 100644 --- a/crates/crunch-cli/src/main.rs +++ b/crates/crunch-cli/src/main.rs @@ -1,4 +1,10 @@ +mod logging; + +use std::path::PathBuf; + +use anyhow::anyhow; use clap::{Args, Parser, Subcommand, ValueEnum}; +use logging::LogArg; use tracing::Level; #[derive(Parser, Clone)] @@ -27,52 +33,33 @@ struct GlobalArgs { global = true, help_heading = "Global" )] - crunch_file: String, -} - -#[derive(Clone, ValueEnum)] -enum LogArg { - None, - Trace, - Debug, - Info, - Warn, - Error, + crunch_file: PathBuf, } #[tokio::main] -async fn main() { +async fn main() -> anyhow::Result<()> { let cli = Cli::parse(); - init_logging(&cli.global_args.log); + cli.global_args.log.init_logging(); match &cli.commands { - Commands::Generate {} => {} + Commands::Generate {} => { + let config_file = config::get_file(&cli.global_args.crunch_file) + .await + .map_err(|e| anyhow!("failed to load config: {}", e))? + .get_config() + .map_err(|e| anyhow!("invalid config: {}", e))?; + + tracing::info!("generating crunch code") + } } + + Ok(()) } -fn init_logging(log: &LogArg) { - match log { - LogArg::None => {} - LogArg::Trace => { - tracing_subscriber::fmt() - .with_max_level(Level::TRACE) - .init(); - } - LogArg::Debug => { - tracing_subscriber::fmt() - .with_max_level(Level::DEBUG) - .init(); - } - LogArg::Info => { - tracing_subscriber::fmt().with_max_level(Level::INFO).init(); - } - LogArg::Warn => { - tracing_subscriber::fmt().with_max_level(Level::WARN).init(); - } - LogArg::Error => { - tracing_subscriber::fmt() - .with_max_level(Level::ERROR) - .init(); - } +mod config { + pub async fn get_file(path: &std::path::Path) -> anyhow::Result { + let file = crunch_file::File::parse_file(path).await?; + + Ok(file) } } diff --git a/crates/crunch-file/Cargo.toml b/crates/crunch-file/Cargo.toml index 537c0df..5191e3a 100644 --- a/crates/crunch-file/Cargo.toml +++ b/crates/crunch-file/Cargo.toml @@ -13,6 +13,7 @@ tokio.workspace = true async-trait.workspace = true toml_edit.workspace = true serde.workspace = true +tracing.workspace = true [dev-dependencies] pretty_assertions.workspace = true diff --git a/crates/crunch-file/src/lib.rs b/crates/crunch-file/src/lib.rs index bba806f..7ce6a3c 100644 --- a/crates/crunch-file/src/lib.rs +++ b/crates/crunch-file/src/lib.rs @@ -31,12 +31,15 @@ pub struct Publish { #[allow(dead_code)] impl File { pub async fn parse_file(path: &std::path::Path) -> anyhow::Result { + tracing::debug!("loading crunch file at: {}", path.display()); + let file = tokio::fs::read_to_string(path).await?; Self::parse(&file).await } pub async fn parse(content: &str) -> anyhow::Result { + tracing::debug!("parsing crunch file"); let config: Document = content.parse::()?; Ok(File { doc: config }) @@ -44,6 +47,8 @@ impl File { pub async fn write_file(&self, path: &std::path::Path) -> anyhow::Result<()> { let content = self.write().await?; + + tracing::debug!("writing to file: {}", path.display()); let mut file = tokio::fs::File::create(path).await?; file.write_all(content.as_bytes()).await?; @@ -53,6 +58,8 @@ impl File { } pub async fn write(&self) -> anyhow::Result { + tracing::debug!("converting crunch config into file"); + let content = self.doc.to_string(); Ok(content) @@ -64,9 +71,11 @@ impl File { publish["output-path"] = value(output_path); if !self.doc.contains_key("publish") { + tracing::debug!("publish key not existing, adding new"); self.doc["publish"] = toml_edit::array() } + tracing::debug!("adding new publish item"); self.doc["publish"] .as_array_of_tables_mut() .expect("publish to be present and be array of tables [[publish]]") @@ -78,6 +87,7 @@ impl File { pub fn get_config(&self) -> anyhow::Result { let content = self.doc.to_string(); + tracing::debug!("converting config into read only copy"); let config: Config = toml_edit::de::from_str(&content)?; Ok(config)