author | Simon Sapin <simon.sapin@octobus.net> |
Mon, 08 Feb 2021 21:05:36 +0100 | |
changeset 46553 | 1ecaf09d9964 |
parent 46552 | 184e46550dc8 |
child 46554 | 95d37db31479 |
permissions | -rw-r--r-- |
45536
b1cea0dc9db0
rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
1 |
extern crate log; |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
2 |
use clap::App; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
3 |
use clap::AppSettings; |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
4 |
use clap::ArgMatches; |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
5 |
use format_bytes::format_bytes; |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
6 |
|
45002
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45001
diff
changeset
|
7 |
mod commands; |
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45001
diff
changeset
|
8 |
mod error; |
45001 | 9 |
mod exitcode; |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45002
diff
changeset
|
10 |
mod ui; |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
11 |
use error::CommandError; |
45001 | 12 |
|
13 |
fn main() { |
|
45536
b1cea0dc9db0
rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
14 |
env_logger::init(); |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
15 |
let app = App::new("rhg") |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
16 |
.setting(AppSettings::AllowInvalidUtf8) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
17 |
.setting(AppSettings::SubcommandRequired) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
18 |
.setting(AppSettings::VersionlessSubcommands) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
19 |
.version("0.0.1") |
46553
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
20 |
.subcommand(commands::root::args()) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
21 |
.subcommand(commands::files::args()) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
22 |
.subcommand(commands::cat::args()) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
23 |
.subcommand(commands::debugdata::args()) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
24 |
.subcommand(commands::debugrequirements::args()); |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
25 |
|
45451
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45449
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:
45449
diff
changeset
|
27 |
let _ = ui::Ui::new().writeln_stderr_str(&err.message); |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
28 |
std::process::exit(exitcode::UNIMPLEMENTED) |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
29 |
}); |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
30 |
|
45381
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45051
diff
changeset
|
31 |
let ui = ui::Ui::new(); |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45051
diff
changeset
|
32 |
|
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
33 |
let command_result = match_subcommand(matches, &ui); |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
34 |
|
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
35 |
let exit_code = match command_result { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
36 |
Ok(_) => exitcode::OK, |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
37 |
|
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
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:
45938
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:
45938
diff
changeset
|
40 |
Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED, |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
41 |
|
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
42 |
Err(CommandError::Abort { message }) => { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
43 |
if !message.is_empty() { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
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:
45938
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:
45938
diff
changeset
|
46 |
let _ = |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
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:
45938
diff
changeset
|
48 |
} |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
49 |
exitcode::ABORT |
45381
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45051
diff
changeset
|
50 |
} |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
51 |
}; |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
52 |
std::process::exit(exit_code) |
45001 | 53 |
} |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
54 |
|
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
55 |
fn match_subcommand( |
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
56 |
matches: ArgMatches, |
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
57 |
ui: &ui::Ui, |
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
58 |
) -> Result<(), CommandError> { |
46543
a6e4e4650bac
rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
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:
46513
diff
changeset
|
60 |
|
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
61 |
match matches.subcommand() { |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
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:
46543
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:
46543
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:
46543
diff
changeset
|
65 |
("debugdata", Some(matches)) => { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
66 |
commands::debugdata::run(ui, &config, matches) |
45548
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45543
diff
changeset
|
67 |
} |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
68 |
("debugrequirements", Some(matches)) => { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
69 |
commands::debugrequirements::run(ui, &config, matches) |
45938
ead435aa5294
rhg: add a `debugrequirements` subcommand
Simon Sapin <simon-commits@exyr.org>
parents:
45548
diff
changeset
|
70 |
} |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
71 |
_ => unreachable!(), // Because of AppSettings::SubcommandRequired, |
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
72 |
} |
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
73 |
} |