main.rs
73 lines
| 2.4 KiB
| application/rls-services+xml
|
RustLexer
Antoine Cezar
|
r46101 | extern crate log; | ||
Antoine Cezar
|
r45593 | use clap::App; | ||
use clap::AppSettings; | ||||
Antoine Cezar
|
r46100 | use clap::ArgMatches; | ||
Simon Sapin
|
r47174 | use format_bytes::format_bytes; | ||
Antoine Cezar
|
r45593 | |||
Antoine Cezar
|
r45515 | mod commands; | ||
mod error; | ||||
Antoine Cezar
|
r45503 | mod exitcode; | ||
Antoine Cezar
|
r45592 | mod ui; | ||
Antoine Cezar
|
r46100 | use error::CommandError; | ||
Antoine Cezar
|
r45503 | |||
fn main() { | ||||
Antoine Cezar
|
r46101 | env_logger::init(); | ||
Antoine Cezar
|
r46100 | let app = App::new("rhg") | ||
Antoine Cezar
|
r45593 | .setting(AppSettings::AllowInvalidUtf8) | ||
.setting(AppSettings::SubcommandRequired) | ||||
.setting(AppSettings::VersionlessSubcommands) | ||||
.version("0.0.1") | ||||
Simon Sapin
|
r47251 | .subcommand(commands::root::args()) | ||
.subcommand(commands::files::args()) | ||||
.subcommand(commands::cat::args()) | ||||
.subcommand(commands::debugdata::args()) | ||||
.subcommand(commands::debugrequirements::args()); | ||||
Antoine Cezar
|
r45593 | |||
Antoine Cezar
|
r46011 | let matches = app.clone().get_matches_safe().unwrap_or_else(|err| { | ||
let _ = ui::Ui::new().writeln_stderr_str(&err.message); | ||||
Simon Sapin
|
r47174 | std::process::exit(exitcode::UNIMPLEMENTED) | ||
Antoine Cezar
|
r45593 | }); | ||
Antoine Cezar
|
r45920 | let ui = ui::Ui::new(); | ||
Antoine Cezar
|
r46100 | let command_result = match_subcommand(matches, &ui); | ||
Antoine Cezar
|
r45593 | |||
Simon Sapin
|
r47174 | let exit_code = match command_result { | ||
Ok(_) => exitcode::OK, | ||||
// Exit with a specific code and no error message to let a potential | ||||
// wrapper script fallback to Python-based Mercurial. | ||||
Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED, | ||||
Err(CommandError::Abort { message }) => { | ||||
if !message.is_empty() { | ||||
// Ignore errors when writing to stderr, we’re already exiting | ||||
// with failure code so there’s not much more we can do. | ||||
let _ = | ||||
ui.write_stderr(&format_bytes!(b"abort: {}\n", message)); | ||||
} | ||||
exitcode::ABORT | ||||
Antoine Cezar
|
r45920 | } | ||
Simon Sapin
|
r47174 | }; | ||
std::process::exit(exit_code) | ||||
Antoine Cezar
|
r45503 | } | ||
Antoine Cezar
|
r46100 | |||
fn match_subcommand( | ||||
matches: ArgMatches, | ||||
ui: &ui::Ui, | ||||
) -> Result<(), CommandError> { | ||||
Simon Sapin
|
r47213 | let config = hg::config::Config::load()?; | ||
Antoine Cezar
|
r46100 | match matches.subcommand() { | ||
Simon Sapin
|
r47250 | ("root", Some(matches)) => commands::root::run(ui, &config, matches), | ||
("files", Some(matches)) => commands::files::run(ui, &config, matches), | ||||
("cat", Some(matches)) => commands::cat::run(ui, &config, matches), | ||||
("debugdata", Some(matches)) => { | ||||
commands::debugdata::run(ui, &config, matches) | ||||
Antoine Cezar
|
r46113 | } | ||
Simon Sapin
|
r47250 | ("debugrequirements", Some(matches)) => { | ||
commands::debugrequirements::run(ui, &config, matches) | ||||
Simon Sapin
|
r46535 | } | ||
Antoine Cezar
|
r46100 | _ => unreachable!(), // Because of AppSettings::SubcommandRequired, | ||
} | ||||
} | ||||