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

fix(commands): Require an argument name before the list of tracing filters when used without a subcommand #7056

Merged
merged 5 commits into from
Jul 5, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org).


## [Zebra 1.0.1](https://github.com/ZcashFoundation/zebra/releases/tag/v1.0.1) - 2023-XX-XX

### Breaking Changes

- Zebra now detects subcommand name typos on the command-line. If you want to give Zebra a list of tracing filters, use `zebrad start --filters debug,...` ([#7056](https://github.com/ZcashFoundation/zebra/pull/7056))


## [Zebra 1.0.1](https://github.com/ZcashFoundation/zebra/releases/tag/v1.0.1) - 2023-07-03

Zebra's first patch release fixes multiple peer connection security issues and panics. It also significantly reduces Zebra's CPU usage. We recommend that all users upgrade to Zebra 1.0.1 or later.
Expand Down
31 changes: 12 additions & 19 deletions zebrad/src/commands/entry_point.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Zebrad EntryPoint

use std::cmp::min;

use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
use clap::Parser;
use std::{ffi::OsString, path::PathBuf};
Expand Down Expand Up @@ -42,7 +40,7 @@ pub struct EntryPoint {

/// Filter strings which override the config file and defaults
// This can be applied to the default start command if no subcommand is provided.
#[clap(help = "tracing filters which override the zebrad.toml config")]
#[clap(long, help = "tracing filters which override the zebrad.toml config")]
filters: Vec<String>,
}

Expand All @@ -63,28 +61,23 @@ impl EntryPoint {
"start"
}

/// Checks if the provided arguments include a subcommand
fn should_add_default_subcommand(&self) -> bool {
self.cmd.is_none()
}

/// Process command arguments and insert the default subcommand
/// if no subcommand is provided.
pub fn process_cli_args(mut args: Vec<OsString>) -> clap::error::Result<Vec<OsString>> {
// Check if the provided arguments include a subcommand
let should_add_default_subcommand = EntryPoint::try_parse_from(&args)?.cmd.is_none();
let entry_point = EntryPoint::try_parse_from(&args)?;

// Add the default subcommand to args after the top-level args if cmd is None
if should_add_default_subcommand {
// try_parse_from currently produces an error if the first argument is not the binary name,
let mut num_top_level_args = 1;

// update last_top_level_arg_idx to the number of top-level args
for (idx, arg) in args.iter().enumerate() {
num_top_level_args = match arg.to_str() {
Some("--verbose" | "-v" | "--version" | "-V" | "--help") => idx + 1,
Some("--config" | "-c") => idx + 2,
_ => num_top_level_args,
}
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
if entry_point.should_add_default_subcommand() {
args.push(EntryPoint::default_cmd_as_str().into());
// This duplicates the top-level filters args, but the tracing component only checks `StartCmd.filters`.
for filter in entry_point.filters {
args.push(filter.into())
arya2 marked this conversation as resolved.
Show resolved Hide resolved
}

num_top_level_args = min(num_top_level_args, args.len());
args.insert(num_top_level_args, EntryPoint::default_cmd_as_str().into());
}

Ok(args)
Expand Down
3 changes: 2 additions & 1 deletion zebrad/src/commands/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ fn args_with_subcommand_pass_through() {
(true, false, false, vec!["zebrad", "--help"]),
(false, true, false, vec!["zebrad", "start"]),
(false, true, true, vec!["zebrad", "-v", "start"]),
(false, true, false, vec!["zebrad", "warn"]),
(false, true, false, vec!["zebrad", "--filters", "warn"]),
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
arya2 marked this conversation as resolved.
Show resolved Hide resolved
(true, false, false, vec!["zebrad", "warn"]),
(false, true, false, vec!["zebrad", "start", "warn"]),
(true, false, false, vec!["zebrad", "help", "warn"]),
];
Expand Down