rust/rhg/src/main.rs
author Simon Sapin <simon.sapin@octobus.net>
Mon, 08 Feb 2021 21:05:36 +0100
changeset 46501 1ecaf09d9964
parent 46500 184e46550dc8
child 46502 95d37db31479
permissions -rw-r--r--
rhg: Move subcommand CLI arguments definitions to respective modules Differential Revision: https://phab.mercurial-scm.org/D9968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45530
b1cea0dc9db0 rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45529
diff changeset
     1
extern crate log;
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
     2
use clap::App;
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
     3
use clap::AppSettings;
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
     4
use clap::ArgMatches;
46445
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
     5
use format_bytes::format_bytes;
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
     6
44982
bacf6c7ef01b rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44981
diff changeset
     7
mod commands;
bacf6c7ef01b rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44981
diff changeset
     8
mod error;
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     9
mod exitcode;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44982
diff changeset
    10
mod ui;
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    11
use error::CommandError;
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    12
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    13
fn main() {
45530
b1cea0dc9db0 rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45529
diff changeset
    14
    env_logger::init();
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    15
    let app = App::new("rhg")
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    16
        .setting(AppSettings::AllowInvalidUtf8)
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    17
        .setting(AppSettings::SubcommandRequired)
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    18
        .setting(AppSettings::VersionlessSubcommands)
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    19
        .version("0.0.1")
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    20
        .subcommand(commands::root::args())
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    21
        .subcommand(commands::files::args())
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    22
        .subcommand(commands::cat::args())
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    23
        .subcommand(commands::debugdata::args())
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    24
        .subcommand(commands::debugrequirements::args());
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    25
45440
a6a000ab135b rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45438
diff changeset
    26
    let matches = app.clone().get_matches_safe().unwrap_or_else(|err| {
a6a000ab135b rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45438
diff changeset
    27
        let _ = ui::Ui::new().writeln_stderr_str(&err.message);
46445
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    28
        std::process::exit(exitcode::UNIMPLEMENTED)
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    29
    });
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    30
45361
47997afadf08 rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
    31
    let ui = ui::Ui::new();
47997afadf08 rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
    32
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    33
    let command_result = match_subcommand(matches, &ui);
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    34
46445
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    35
    let exit_code = match command_result {
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    36
        Ok(_) => exitcode::OK,
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    37
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    38
        // Exit with a specific code and no error message to let a potential
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    39
        // wrapper script fallback to Python-based Mercurial.
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    40
        Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED,
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    41
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    42
        Err(CommandError::Abort { message }) => {
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    43
            if !message.is_empty() {
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    44
                // Ignore errors when writing to stderr, we’re already exiting
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    45
                // with failure code so there’s not much more we can do.
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    46
                let _ =
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    47
                    ui.write_stderr(&format_bytes!(b"abort: {}\n", message));
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    48
            }
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    49
            exitcode::ABORT
45361
47997afadf08 rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
    50
        }
46445
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    51
    };
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
    52
    std::process::exit(exit_code)
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    53
}
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    54
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    55
fn match_subcommand(
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    56
    matches: ArgMatches,
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    57
    ui: &ui::Ui,
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    58
) -> Result<(), CommandError> {
46484
a6e4e4650bac rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents: 46445
diff changeset
    59
    let config = hg::config::Config::load()?;
a6e4e4650bac rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents: 46445
diff changeset
    60
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    61
    match matches.subcommand() {
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    62
        ("root", Some(matches)) => commands::root::run(ui, &config, matches),
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    63
        ("files", Some(matches)) => commands::files::run(ui, &config, matches),
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    64
        ("cat", Some(matches)) => commands::cat::run(ui, &config, matches),
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    65
        ("debugdata", Some(matches)) => {
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    66
            commands::debugdata::run(ui, &config, matches)
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45537
diff changeset
    67
        }
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    68
        ("debugrequirements", Some(matches)) => {
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
    69
            commands::debugrequirements::run(ui, &config, matches)
45923
ead435aa5294 rhg: add a `debugrequirements` subcommand
Simon Sapin <simon-commits@exyr.org>
parents: 45542
diff changeset
    70
        }
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    71
        _ => unreachable!(), // Because of AppSettings::SubcommandRequired,
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    72
    }
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    73
}