diff --git a/Cargo.lock b/Cargo.lock index 362597b..9656140 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -842,6 +842,15 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "matchit" version = "0.7.3" @@ -934,6 +943,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "object" version = "0.36.4" @@ -965,6 +984,12 @@ dependencies = [ "hashbrown 0.13.2", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "owo-colors" version = "4.1.0" @@ -1241,8 +1266,17 @@ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -1253,9 +1287,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.4", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.4" @@ -1347,6 +1387,8 @@ dependencies = [ "thiserror", "tokio", "toml", + "tracing", + "tracing-subscriber", ] [[package]] @@ -1416,6 +1458,15 @@ 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 = "signal-hook-registry" version = "1.4.2" @@ -1560,6 +1611,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -1775,6 +1836,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" 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.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -1845,6 +1936,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.5" @@ -1921,6 +2018,28 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 9acfe88..bbd0126 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ tokio = { version = "*", features = ["full"] } humantime = "2.1.0" miette = { version = "7.2.0", features = ["fancy"] } color-print = "0.3.6" +tracing = "0.1.40" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/src/config.rs b/src/config.rs index e04aff6..aed02c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,4 @@ -use std::{ - env, - path::{Path, PathBuf}, -}; +use std::path::{Path, PathBuf}; use config::{Config, FileFormat}; use miette::Diagnostic; @@ -12,7 +9,7 @@ use crate::error::S2CliError; #[derive(Debug, Deserialize, Serialize)] pub struct S2Config { - pub token: String, + pub auth_token: String, } #[cfg(target_os = "windows")] @@ -33,24 +30,20 @@ pub fn config_path() -> Result { } pub fn load_config(path: &Path) -> Result { - if let Ok(env_token) = env::var("S2_AUTH_TOKEN") { - return Ok(S2Config { token: env_token }); - } Config::builder() .add_source(config::File::new( path.to_str().ok_or(S2ConfigError::PathError)?, FileFormat::Toml, )) + .add_source(config::Environment::with_prefix("S2")) .build() .map_err(|_| S2ConfigError::LoadError)? .try_deserialize::() .map_err(|_| S2ConfigError::LoadError) } -pub fn create_config(config_path: &PathBuf, token: String) -> Result<(), S2ConfigError> { - let cfg = S2Config { - token: token.to_string(), - }; +pub fn create_config(config_path: &PathBuf, auth_token: String) -> Result<(), S2ConfigError> { + let cfg = S2Config { auth_token }; if let Some(parent) = config_path.parent() { std::fs::create_dir_all(parent).map_err(|_| S2ConfigError::WriteError)?; diff --git a/src/main.rs b/src/main.rs index bb3b633..36b08d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use s2::{ client::{Client, ClientConfig, HostCloud}, types::{BasinMetadata, StorageClass}, }; +use tracing_subscriber::{fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt}; mod account; mod basin; @@ -60,7 +61,7 @@ enum ConfigActions { /// Set the authentication token Set { #[arg(short, long)] - token: String, + auth_token: String, }, } @@ -123,10 +124,10 @@ enum BasinActions { }, } -async fn s2_client(token: String) -> Result { +async fn s2_client(auth_token: String) -> Result { let config = ClientConfig::builder() .host_uri(HostCloud::Local) - .token(token.to_string()) + .token(auth_token.to_string()) .connection_timeout(std::time::Duration::from_secs(5)) .build(); @@ -143,10 +144,21 @@ async fn run() -> Result<(), S2CliError> { let commands = Cli::parse(); let config_path = config_path()?; + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .pretty() + .with_span_events(FmtSpan::NEW) + .compact() + .with_writer(std::io::stderr), + ) + .with(tracing_subscriber::EnvFilter::from_default_env()) + .init(); + match commands.command { Commands::Config { action } => match action { - ConfigActions::Set { token } => { - create_config(&config_path, token)?; + ConfigActions::Set { auth_token } => { + create_config(&config_path, auth_token)?; println!("{}", "✓ Token set successfully".green().bold()); println!( " Configuration saved to: {}", @@ -157,7 +169,8 @@ async fn run() -> Result<(), S2CliError> { Commands::Account { action } => { let cfg = config::load_config(&config_path)?; - let account_service = AccountService::new(s2_client(cfg.token).await?); + println!("cfg: {:?}", cfg); + let account_service = AccountService::new(s2_client(cfg.auth_token).await?); match action { AccountActions::ListBasins { prefix, @@ -203,7 +216,7 @@ async fn run() -> Result<(), S2CliError> { } Commands::Basins { action } => { let cfg = config::load_config(&config_path)?; - let client = s2_client(cfg.token).await?; + let client = s2_client(cfg.auth_token).await?; match action { BasinActions::ListStreams { basin,