mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 02:04:19 +00:00
cli: don't call process::exit()
in create_ui()
This patch move the call to `process::exit()` out of `create_ui()` by letting that function return a `(Ui, Result<(), CommandError>)`.
This commit is contained in:
parent
cc30cb6ca8
commit
4f0ccedd31
4 changed files with 31 additions and 32 deletions
|
@ -17,7 +17,7 @@ use std::path::Path;
|
||||||
|
|
||||||
use clap::{FromArgMatches, Subcommand};
|
use clap::{FromArgMatches, Subcommand};
|
||||||
use git2::Repository;
|
use git2::Repository;
|
||||||
use jujutsu::cli_util::{create_ui, parse_args, report_command_error, CommandError};
|
use jujutsu::cli_util::{create_ui, handle_command_result, parse_args, CommandError};
|
||||||
use jujutsu::commands::{default_app, run_command};
|
use jujutsu::commands::{default_app, run_command};
|
||||||
use jujutsu::ui::Ui;
|
use jujutsu::ui::Ui;
|
||||||
use jujutsu_lib::backend::{
|
use jujutsu_lib::backend::{
|
||||||
|
@ -61,11 +61,9 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut ui = create_ui();
|
let (mut ui, result) = create_ui();
|
||||||
let exit_code = match run(&mut ui) {
|
let result = result.and_then(|()| run(&mut ui));
|
||||||
Ok(()) => 0,
|
let exit_code = handle_command_result(&mut ui, result);
|
||||||
Err(err) => report_command_error(&mut ui, err),
|
|
||||||
};
|
|
||||||
std::process::exit(exit_code);
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
use clap::{FromArgMatches, Subcommand};
|
use clap::{FromArgMatches, Subcommand};
|
||||||
use jujutsu::cli_util::{
|
use jujutsu::cli_util::{
|
||||||
create_ui, parse_args, report_command_error, short_commit_description, CommandError,
|
create_ui, handle_command_result, parse_args, short_commit_description, CommandError,
|
||||||
};
|
};
|
||||||
use jujutsu::commands::{default_app, run_command};
|
use jujutsu::commands::{default_app, run_command};
|
||||||
use jujutsu::ui::Ui;
|
use jujutsu::ui::Ui;
|
||||||
|
@ -59,10 +59,8 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut ui = create_ui();
|
let (mut ui, result) = create_ui();
|
||||||
let exit_code = match run(&mut ui) {
|
let result = result.and_then(|()| run(&mut ui));
|
||||||
Ok(()) => 0,
|
let exit_code = handle_command_result(&mut ui, result);
|
||||||
Err(err) => report_command_error(&mut ui, err),
|
|
||||||
};
|
|
||||||
std::process::exit(exit_code);
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@ use crate::ui::{ColorChoice, FilePathParseError, Ui};
|
||||||
|
|
||||||
pub enum CommandError {
|
pub enum CommandError {
|
||||||
UserError(String),
|
UserError(String),
|
||||||
|
ConfigError(String),
|
||||||
/// Invalid command line
|
/// Invalid command line
|
||||||
CliError(String),
|
CliError(String),
|
||||||
BrokenPipe,
|
BrokenPipe,
|
||||||
|
@ -68,7 +69,7 @@ impl From<std::io::Error> for CommandError {
|
||||||
|
|
||||||
impl From<config::ConfigError> for CommandError {
|
impl From<config::ConfigError> for CommandError {
|
||||||
fn from(err: config::ConfigError) -> Self {
|
fn from(err: config::ConfigError) -> Self {
|
||||||
CommandError::UserError(format!("Config error: {err}"))
|
CommandError::ConfigError(err.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1135,16 +1136,14 @@ pub struct GlobalArgs {
|
||||||
pub color: Option<ColorChoice>,
|
pub color: Option<ColorChoice>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_ui() -> Ui<'static> {
|
pub fn create_ui() -> (Ui<'static>, Result<(), CommandError>) {
|
||||||
// TODO: We need to do some argument parsing here, at least for things like
|
// 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
|
// --config, and for reading user configs from the repo pointed to by -R.
|
||||||
// -R.
|
|
||||||
match read_config() {
|
match read_config() {
|
||||||
Ok(user_settings) => Ui::for_terminal(user_settings),
|
Ok(user_settings) => (Ui::for_terminal(user_settings), Ok(())),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let mut ui = Ui::for_terminal(UserSettings::default());
|
let ui = Ui::for_terminal(UserSettings::default());
|
||||||
ui.write_error(&format!("Config error: {}\n", err)).unwrap();
|
(ui, Err(CommandError::ConfigError(err.to_string())))
|
||||||
std::process::exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1264,18 +1263,24 @@ pub fn parse_args<'help>(
|
||||||
|
|
||||||
// TODO: Return std::process::ExitCode instead, once our MSRV is >= 1.61
|
// TODO: Return std::process::ExitCode instead, once our MSRV is >= 1.61
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn report_command_error(ui: &mut Ui, err: CommandError) -> i32 {
|
pub fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> i32 {
|
||||||
match err {
|
match result {
|
||||||
CommandError::UserError(message) => {
|
Ok(()) => 0,
|
||||||
|
Err(CommandError::UserError(message)) => {
|
||||||
ui.write_error(&format!("Error: {}\n", message)).unwrap();
|
ui.write_error(&format!("Error: {}\n", message)).unwrap();
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
CommandError::CliError(message) => {
|
Err(CommandError::ConfigError(message)) => {
|
||||||
|
ui.write_error(&format!("Config error: {}\n", message))
|
||||||
|
.unwrap();
|
||||||
|
1
|
||||||
|
}
|
||||||
|
Err(CommandError::CliError(message)) => {
|
||||||
ui.write_error(&format!("Error: {}\n", message)).unwrap();
|
ui.write_error(&format!("Error: {}\n", message)).unwrap();
|
||||||
2
|
2
|
||||||
}
|
}
|
||||||
CommandError::BrokenPipe => std::process::exit(3),
|
Err(CommandError::BrokenPipe) => std::process::exit(3),
|
||||||
CommandError::InternalError(message) => {
|
Err(CommandError::InternalError(message)) => {
|
||||||
ui.write_error(&format!("Internal error: {}\n", message))
|
ui.write_error(&format!("Internal error: {}\n", message))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
255
|
255
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use jujutsu::cli_util::{create_ui, parse_args, report_command_error, CommandError};
|
use jujutsu::cli_util::{create_ui, handle_command_result, parse_args, CommandError};
|
||||||
use jujutsu::commands::{default_app, run_command};
|
use jujutsu::commands::{default_app, run_command};
|
||||||
use jujutsu::ui::Ui;
|
use jujutsu::ui::Ui;
|
||||||
|
|
||||||
|
@ -23,10 +23,8 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut ui = create_ui();
|
let (mut ui, result) = create_ui();
|
||||||
let exit_code = match run(&mut ui) {
|
let result = result.and_then(|()| run(&mut ui));
|
||||||
Ok(()) => 0,
|
let exit_code = handle_command_result(&mut ui, result);
|
||||||
Err(err) => report_command_error(&mut ui, err),
|
|
||||||
};
|
|
||||||
std::process::exit(exit_code);
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue