From 7a9f9a14dbbac16d773f4cb4d57d9465cff50565 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 22 Sep 2022 11:13:46 -0700 Subject: [PATCH] cli: extract a function for creating a default `Ui` This will also be reused by binaries that want to add custom commands. --- src/cli_util.rs | 15 +++++++++++++++ src/main.rs | 31 +++++++------------------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index a6676c6a1..b8eaa5346 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -42,6 +42,7 @@ use jujutsu_lib::working_copy::{ use jujutsu_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError}; use jujutsu_lib::{dag_walk, git, revset}; +use crate::config::read_config; use crate::diff_edit::DiffEditError; use crate::ui; use crate::ui::{ColorChoice, FilePathParseError, Ui}; @@ -1128,6 +1129,20 @@ pub struct GlobalArgs { pub color: Option, } +pub fn create_ui() -> Ui<'static> { + // TODO: We need to do some argument parsing here, at least for things like + // --config, and for reading user configs from the repo pointed to by + // -R. + match read_config() { + Ok(user_settings) => Ui::for_terminal(user_settings), + Err(err) => { + let mut ui = Ui::for_terminal(UserSettings::default()); + ui.write_error(&format!("Config error: {}\n", err)).unwrap(); + std::process::exit(1); + } + } +} + fn string_list_from_config(value: config::Value) -> Option> { match value { config::Value { diff --git a/src/main.rs b/src/main.rs index 058aa4655..7e553e800 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,11 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use jujutsu::cli_util::{parse_args, report_command_error, CommandError}; +use jujutsu::cli_util::{create_ui, parse_args, report_command_error, CommandError}; use jujutsu::commands::{default_app, run_command}; -use jujutsu::config::read_config; use jujutsu::ui::Ui; -use jujutsu_lib::settings::UserSettings; fn run(ui: &mut Ui) -> Result<(), CommandError> { let app = default_app(); @@ -25,25 +23,10 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> { } fn main() { - // TODO: We need to do some argument parsing here, at least for things like - // --config, and for reading user configs from the repo pointed to by - // -R. - match read_config() { - Ok(user_settings) => { - let mut ui = Ui::for_terminal(user_settings); - match run(&mut ui) { - Ok(()) => { - std::process::exit(0); - } - Err(err) => { - std::process::exit(report_command_error(&mut ui, err)); - } - } - } - Err(err) => { - let mut ui = Ui::for_terminal(UserSettings::default()); - ui.write_error(&format!("Config error: {}\n", err)).unwrap(); - std::process::exit(1); - } - } + let mut ui = create_ui(); + let exit_code = match run(&mut ui) { + Ok(()) => 0, + Err(err) => report_command_error(&mut ui, err), + }; + std::process::exit(exit_code); }