Skip to content

Commit

Permalink
cli: move --verbose handling to parse_args(), enable it in custom exa…
Browse files Browse the repository at this point in the history
…mples
  • Loading branch information
yuja committed Jan 4, 2023
1 parent dbcfb00 commit 9763a4d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
10 changes: 6 additions & 4 deletions examples/custom-backend/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::path::Path;

use clap::{FromArgMatches, Subcommand};
use git2::Repository;
use jujutsu::cli_util::{handle_command_result, parse_args, CommandError};
use jujutsu::cli_util::{handle_command_result, parse_args, CommandError, TracingSubscription};
use jujutsu::commands::{default_app, run_command};
use jujutsu::config::read_config;
use jujutsu::ui::Ui;
Expand All @@ -35,10 +35,11 @@ enum CustomCommands {
InitJit,
}

fn run(ui: &mut Ui) -> Result<(), CommandError> {
fn run(ui: &mut Ui, tracing_subscription: &TracingSubscription) -> Result<(), CommandError> {
ui.reset(read_config()?);
let app = CustomCommands::augment_subcommands(default_app());
let (mut command_helper, matches) = parse_args(ui, app, std::env::args_os())?;
let (mut command_helper, matches) =
parse_args(ui, app, tracing_subscription, std::env::args_os())?;
let mut store_factories = StoreFactories::default();
// Register the backend so it can be loaded when the repo is loaded. The name
// must match `Backend::name()`.
Expand All @@ -63,9 +64,10 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
}

fn main() {
let tracing_subscription = TracingSubscription::init();
jujutsu::cleanup_guard::init();
let mut ui = Ui::new();
let result = run(&mut ui);
let result = run(&mut ui, &tracing_subscription);
let exit_code = handle_command_result(&mut ui, result);
ui.finalize_writes();
std::process::exit(exit_code);
Expand Down
9 changes: 5 additions & 4 deletions examples/custom-command/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use clap::{FromArgMatches, Subcommand};
use jujutsu::cli_util::{
handle_command_result, parse_args, short_commit_description, CommandError,
handle_command_result, parse_args, short_commit_description, CommandError, TracingSubscription,
};
use jujutsu::commands::{default_app, run_command};
use jujutsu::config::read_config;
Expand All @@ -33,10 +33,10 @@ struct FrobnicateArgs {
revision: String,
}

fn run(ui: &mut Ui) -> Result<(), CommandError> {
fn run(ui: &mut Ui, tracing_subscription: &TracingSubscription) -> Result<(), CommandError> {
ui.reset(read_config()?);
let app = CustomCommands::augment_subcommands(default_app());
let (command_helper, matches) = parse_args(ui, app, std::env::args_os())?;
let (command_helper, matches) = parse_args(ui, app, tracing_subscription, std::env::args_os())?;
match CustomCommands::from_arg_matches(&matches) {
// Handle our custom command
Ok(CustomCommands::Frobnicate(args)) => {
Expand All @@ -62,9 +62,10 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
}

fn main() {
let tracing_subscription = TracingSubscription::init();
jujutsu::cleanup_guard::init();
let mut ui = Ui::new();
let result = run(&mut ui);
let result = run(&mut ui, &tracing_subscription);
let exit_code = handle_command_result(&mut ui, result);
ui.finalize_writes();
std::process::exit(exit_code);
Expand Down
5 changes: 5 additions & 0 deletions src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ fn handle_early_args(
pub fn parse_args(
ui: &mut Ui,
app: clap::Command,
tracing_subscription: &TracingSubscription,
args_os: ArgsOs,
) -> Result<(CommandHelper, ArgMatches), CommandError> {
let mut string_args: Vec<String> = vec![];
Expand All @@ -1617,6 +1618,10 @@ pub fn parse_args(
let matches = app.clone().try_get_matches_from(&string_args)?;

let args: Args = Args::from_arg_matches(&matches).unwrap();
if args.global_args.verbose {
// TODO: set up verbose logging as early as possible
tracing_subscription.enable_verbose_logging()?;
}
let command_helper = CommandHelper::new(app, string_args, args.global_args);
Ok((command_helper, matches))
}
Expand Down
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ use jujutsu::ui::Ui;
fn run(ui: &mut Ui, tracing_subscription: &TracingSubscription) -> Result<(), CommandError> {
ui.reset(read_config()?);
let app = default_app();
let (command_helper, matches) = parse_args(ui, app, std::env::args_os())?;
if command_helper.global_args().verbose {
tracing_subscription.enable_verbose_logging()?;
}
let (command_helper, matches) = parse_args(ui, app, tracing_subscription, std::env::args_os())?;
run_command(ui, &command_helper, &matches)
}

fn main() {
// TODO(@rslabbert): restructure logging filter setup to better handle
// having verbose logging set up as early as possible, and to support
// custom commands. See discussion on:
// https://github.com/martinvonz/jj/pull/771
let tracing_subscription = TracingSubscription::init();
jujutsu::cleanup_guard::init();
let mut ui = Ui::new();
Expand Down

0 comments on commit 9763a4d

Please sign in to comment.