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

ref: Simplify --log-level parsing #2356

Merged
merged 1 commit into from
Jan 24, 2025
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
24 changes: 6 additions & 18 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module implements the root command of the CLI tool.

use anyhow::{bail, Result};
use anyhow::Result;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use clap_complete::{generate, Generator, Shell};
use log::{debug, info, set_logger, set_max_level, LevelFilter};
Expand Down Expand Up @@ -130,18 +130,6 @@ fn configure_args(config: &mut Config, matches: &ArgMatches) -> Result<()> {
Ok(())
}

pub fn get_log_level(matches: &ArgMatches) -> Result<Option<LevelFilter>> {
match matches.get_one::<String>("log_level") {
Some(log_level) => match log_level.parse() {
Ok(log_level) => Ok(Some(log_level)),
Err(_) => {
bail!("Unknown log level: {}", log_level);
}
},
None => Ok(None),
}
}

fn app() -> Command {
Command::new("sentry-cli")
.version(VERSION)
Expand Down Expand Up @@ -181,10 +169,10 @@ fn app() -> Command {
Arg::new("log_level")
.value_name("LOG_LEVEL")
.long("log-level")
.value_parser(["trace", "debug", "info", "warn", "error"])
.value_parser(value_parser!(LevelFilter))
.ignore_case(true)
.global(true)
.help("Set the log output verbosity."),
.help("Set the log output verbosity. [possible values: trace, debug, info, warn, error]"),
)
.arg(
Arg::new("quiet")
Expand Down Expand Up @@ -254,15 +242,15 @@ pub fn execute() -> Result<()> {
let mut cmd = app();
cmd = add_commands(cmd);
let matches = cmd.get_matches();
let log_level = get_log_level(&matches)?;
if let Some(log_level) = log_level {
let log_level = matches.get_one::<LevelFilter>("log_level");
if let Some(&log_level) = log_level {
set_max_level(log_level);
}
let mut config = Config::from_cli_config()?;
configure_args(&mut config, &matches)?;
set_quiet_mode(matches.get_flag("quiet"));

if let Some(log_level) = log_level {
if let Some(&log_level) = log_level {
config.set_log_level(log_level);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ Options:
Use the given Sentry auth token.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/_cases/send_event/send_event-help.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ Options:
Set the distribution.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

-E, --env <ENVIRONMENT>
Send with a specific environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ Options:
Metric value, any finite 64 bit float.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ Options:
Metric value, any finite 64 bit float.

--log-level <LOG_LEVEL>
Set the log output verbosity.
[..]
[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/_cases/send_metric/send_metric-help.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ Options:
Use the given Sentry auth token.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ Options:
[default: 1]

--log-level <LOG_LEVEL>
Set the log output verbosity.
[..]
[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ Options:
count will not increase.

--log-level <LOG_LEVEL>
Set the log output verbosity.
[..]
[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ Options:
Don't modify files on disk.

--log-level <LOG_LEVEL>
Set the log output verbosity.

[possible values: trace, debug, info, warn, error]
Set the log output verbosity. [possible values: trace, debug, info, warn, error]

--quiet
Do not print any output while preserving correct exit code. This flag is currently
Expand Down
Loading