diff --git a/Cargo.lock b/Cargo.lock index 4bd5ba5..72d1976 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -310,6 +310,16 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-verbosity-flag" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d92b1fab272fe943881b77cc6e920d6543e5b1bfadbd5ed81c7c5a755742394" +dependencies = [ + "clap", + "log", +] + [[package]] name = "clap_builder" version = "4.5.23" @@ -2333,6 +2343,7 @@ version = "0.1.0-nightly" dependencies = [ "assert_cmd", "clap", + "clap-verbosity-flag", "clap_complete", "comrak", "console", @@ -3185,9 +3196,9 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" -version = "0.1.41" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "pin-project-lite", "tracing-core", @@ -3195,9 +3206,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.33" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", ] diff --git a/Cargo.toml b/Cargo.toml index fc74a22..a04294f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "rnk" description = "A command line interface to the Renku platform" homepage = "https://renkulab.io/" version = "0.1.0-nightly" -edition = "2021" +edition = "2024" license = "Apache-2.0" repository = "https://github.com/SwissDatascienceCenter/renku-cli" keywords = [ "renku", "cli" ] @@ -34,6 +34,7 @@ url = { version = "2.5.1" } openidconnect = { version = "3.5.0", default-features = false, features = [ "reqwest" ] } directories = { version = "5.0" } comrak = { version = "0.28.0", optional = true } +clap-verbosity-flag = "3.0.4" [features] default = ["reqwest/default-tls"] # link against system library diff --git a/rustfmt.toml b/rustfmt.toml index 36c419b..f216078 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1 @@ -edition = "2021" \ No newline at end of file +edition = "2024" diff --git a/src/cli/cmd.rs b/src/cli/cmd.rs index 4a5fa90..12a53f4 100644 --- a/src/cli/cmd.rs +++ b/src/cli/cmd.rs @@ -8,7 +8,7 @@ pub mod version; use super::sink::{Error as SinkError, Sink}; use crate::cli::opts::{CommonOpts, ProxySetting}; use crate::data::renku_url::RenkuUrl; -use crate::httpclient::{self, proxy, Client}; +use crate::httpclient::{self, Client, proxy}; use serde::Serialize; use snafu::{ResultExt, Snafu}; diff --git a/src/cli/cmd/login.rs b/src/cli/cmd/login.rs index 1a06006..88231c5 100644 --- a/src/cli/cmd/login.rs +++ b/src/cli/cmd/login.rs @@ -1,8 +1,8 @@ use std::path::{Path, PathBuf}; use super::Context; -use crate::httpclient::auth::{Response, UserCode}; use crate::httpclient::Error as HttpError; +use crate::httpclient::auth::{Response, UserCode}; use crate::{cli::sink::Error as SinkError, data::simple_message::SimpleMessage}; use clap::{Parser, ValueHint}; diff --git a/src/cli/cmd/project/clone.rs b/src/cli/cmd/project/clone.rs index 41da17b..d7ec42d 100644 --- a/src/cli/cmd/project/clone.rs +++ b/src/cli/cmd/project/clone.rs @@ -67,7 +67,10 @@ impl Input { pub async fn exec(&self, ctx: Context) -> Result<(), Error> { let opt_details = ctx .client - .get_project(&self.project_ref, ctx.opts.verbose > 1) + .get_project( + &self.project_ref, + ctx.opts.verbosity.log_level().unwrap_or(log::Level::Warn) > log::Level::Info, + ) .await .context(HttpClientSnafu)?; if let Some(details) = opt_details { @@ -169,7 +172,7 @@ async fn clone_repository( .await .context(TaskJoinSnafu)?; let git_repo = repo?; - if ctx.opts.verbose > 1 { + if ctx.opts.verbosity.log_level().unwrap_or(log::Level::Warn) > log::Level::Info { let head = git_repo .head() .ok() diff --git a/src/cli/cmd/shell_completion.rs b/src/cli/cmd/shell_completion.rs index d415ca2..ec9b0f2 100644 --- a/src/cli/cmd/shell_completion.rs +++ b/src/cli/cmd/shell_completion.rs @@ -1,5 +1,5 @@ use clap::{Command, Parser, ValueEnum}; -use clap_complete::{generate, Generator, Shell}; +use clap_complete::{Generator, Shell, generate}; /// Generates completions for some shells. /// @@ -37,6 +37,6 @@ impl Input { } } -fn generate_completions(gen: G, binary: &str, app: &mut Command) { - generate(gen, app, binary, &mut std::io::stdout()); +fn generate_completions(generator: G, binary: &str, app: &mut Command) { + generate(generator, app, binary, &mut std::io::stdout()); } diff --git a/src/cli/cmd/version.rs b/src/cli/cmd/version.rs index d03345a..a9fdee0 100644 --- a/src/cli/cmd/version.rs +++ b/src/cli/cmd/version.rs @@ -1,9 +1,9 @@ use super::Context; +use crate::cli::BuildInfo; use crate::cli::sink::Error as SinkError; use crate::cli::sink::Sink; -use crate::cli::BuildInfo; -use crate::httpclient::data::VersionInfo; use crate::httpclient::Error as HttpError; +use crate::httpclient::data::VersionInfo; use clap::Parser; use serde::Serialize; use snafu::{ResultExt, Snafu}; @@ -38,7 +38,9 @@ impl Input { } else { let result = ctx .client - .version(ctx.opts.verbose > 1) + .version( + ctx.opts.verbosity.log_level().unwrap_or(log::Level::Warn) > log::Level::Info, + ) .await .context(HttpClientSnafu)?; let urlstr = ctx.renku_url().as_str(); diff --git a/src/cli/opts.rs b/src/cli/opts.rs index 990b1db..4116635 100644 --- a/src/cli/opts.rs +++ b/src/cli/opts.rs @@ -1,7 +1,8 @@ use crate::data::renku_url::RenkuUrl; use super::cmd::*; -use clap::{ArgAction, Parser, ValueEnum, ValueHint}; +use clap::{Parser, ValueEnum, ValueHint}; +use clap_verbosity_flag::{Verbosity, WarnLevel}; use serde::{Deserialize, Serialize}; use std::str::FromStr; @@ -12,8 +13,8 @@ use std::str::FromStr; pub struct CommonOpts { /// Be more verbose when logging. Verbosity increases with each /// occurence of that option. - #[arg(short, long, action = ArgAction::Count)] - pub verbose: u8, + #[command(flatten)] + pub verbosity: Verbosity, /// How to format the output. The default is human readable which /// may choose to not show every detail for better readability. diff --git a/src/main.rs b/src/main.rs index 446e0eb..36f6fe7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,6 @@ use rnk::error::{Error, Result}; -use std::env; use std::process; -const LOG_LEVEL: &str = "RUST_LOG"; - #[tokio::main] async fn main() { let error_style = console::Style::new().red().bright(); @@ -16,36 +13,15 @@ async fn main() { async fn execute() -> Result<()> { let opts = rnk::read_args(); - let remove_env = match opts.common_opts.verbose { - 1 => set_log_level("info"), - n => { - if n > 1 { - set_log_level("debug") - } else { - false - } - } - }; - env_logger::init(); + env_logger::Builder::new() + .filter_level(opts.common_opts.verbosity.log_level_filter()) + .init(); let result = rnk::execute_cmd(opts).await; - if remove_env { - env::remove_var(LOG_LEVEL); - } result?; Ok(()) } -fn set_log_level(level: &str) -> bool { - let current = env::var_os(LOG_LEVEL); - if current.is_none() { - env::set_var(LOG_LEVEL, level); - true - } else { - false - } -} - fn exit_code(err: &Error) -> i32 { match err { Error::Cmd { source: _ } => 1, diff --git a/src/project_config.rs b/src/project_config.rs index f6fcb43..28d4325 100644 --- a/src/project_config.rs +++ b/src/project_config.rs @@ -70,14 +70,15 @@ impl RenkuProjectConfig { } pub fn write(&self, file: &Path) -> Result<(), ProjectConfigError> { - if !file.exists() { - if let Some(dir) = file.parent() { - std::fs::create_dir_all(dir).map_err(|e| ProjectConfigError::WriteFile { - source: e, - path: file.to_path_buf(), - })?; - } + if !file.exists() + && let Some(dir) = file.parent() + { + std::fs::create_dir_all(dir).map_err(|e| ProjectConfigError::WriteFile { + source: e, + path: file.to_path_buf(), + })?; } + let cnt = toml::to_string(self).map_err(|e| ProjectConfigError::WriteToml { source: e, path: file.to_path_buf(), diff --git a/src/util/file.rs b/src/util/file.rs index 7872627..4194f25 100644 --- a/src/util/file.rs +++ b/src/util/file.rs @@ -1,5 +1,5 @@ use futures::TryStreamExt; -use futures::{stream, Stream, StreamExt}; +use futures::{Stream, StreamExt, stream}; use serde::Serialize; use std::fmt; use std::io;