Mercurial > hg-stable
annotate rust/rhg/src/main.rs @ 46635:d2e61f00ee9d
rust: Introduce a get_bytes_from_os_str utility function
It does the same as get_bytes_from_path but takes an `OsStr`
instead of a `Path`. The implementation is the same so using either
ends up correct but the function name suggests it’s not.
Differential Revision: https://phab.mercurial-scm.org/D10007
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Wed, 17 Feb 2021 11:21:34 +0100 |
parents | 5ce2aa7c2ad5 |
children | 755c31a1caf9 |
rev | line source |
---|---|
45536
b1cea0dc9db0
rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
1 extern crate log; |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
2 use crate::ui::Ui; |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
3 use clap::App; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
4 use clap::AppSettings; |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
5 use clap::Arg; |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
6 use clap::ArgMatches; |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
7 use format_bytes::format_bytes; |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
8 use hg::config::Config; |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
9 use hg::repo::{Repo, RepoError}; |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
10 use std::path::{Path, PathBuf}; |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
11 |
45002
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45001
diff
changeset
|
12 mod error; |
45001 | 13 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
|
14 mod ui; |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
15 use error::CommandError; |
45001 | 16 |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
17 fn add_global_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
18 app.arg( |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
19 Arg::with_name("repository") |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
20 .help("repository root directory") |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
21 .short("-R") |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
22 .long("--repository") |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
23 .value_name("REPO") |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
24 .takes_value(true), |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
25 ) |
46556
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
26 .arg( |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
27 Arg::with_name("config") |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
28 .help("set/override config option (use 'section.name=value')") |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
29 .long("--config") |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
30 .value_name("CONFIG") |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
31 .takes_value(true) |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
32 // Ok: `--config section.key1=val --config section.key2=val2` |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
33 .multiple(true) |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
34 // Not ok: `--config section.key1=val section.key2=val2` |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
35 .number_of_values(1), |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
36 ) |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
37 } |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
38 |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
39 fn main_with_result(ui: &ui::Ui) -> Result<(), CommandError> { |
45536
b1cea0dc9db0
rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
40 env_logger::init(); |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
41 let app = App::new("rhg") |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
42 .setting(AppSettings::AllowInvalidUtf8) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
43 .setting(AppSettings::SubcommandRequired) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
44 .setting(AppSettings::VersionlessSubcommands) |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
45 .version("0.0.1"); |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
46 let app = add_global_args(app); |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
47 let app = add_subcommand_args(app); |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
48 |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
49 let matches = app.clone().get_matches_safe()?; |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
50 |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
51 let (subcommand_name, subcommand_matches) = matches.subcommand(); |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
52 let run = subcommand_run_fn(subcommand_name) |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
53 .expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired"); |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
54 let subcommand_args = subcommand_matches |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
55 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired"); |
45051
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
56 |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
57 // Global arguments can be in either based on e.g. `hg -R ./foo log` v.s. |
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
58 // `hg log -R ./foo` |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
59 let value_of_global_arg = |name| { |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
60 subcommand_args |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
61 .value_of_os(name) |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
62 .or_else(|| matches.value_of_os(name)) |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
63 }; |
46556
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
64 // For arguments where multiple occurences are allowed, return a |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
65 // possibly-iterator of all values. |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
66 let values_of_global_arg = |name: &str| { |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
67 let a = matches.values_of_os(name).into_iter().flatten(); |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
68 let b = subcommand_args.values_of_os(name).into_iter().flatten(); |
46556
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
69 a.chain(b) |
2e5dd18d6dc3
rhg: Add support for --config CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
70 }; |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
71 |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
72 let config_args = values_of_global_arg("config") |
46635
d2e61f00ee9d
rust: Introduce a get_bytes_from_os_str utility function
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
73 .map(hg::utils::files::get_bytes_from_os_str); |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
74 let non_repo_config = &hg::config::Config::load(config_args)?; |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
75 |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
76 let repo_path = value_of_global_arg("repository").map(Path::new); |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
77 let repo = match Repo::find(non_repo_config, repo_path) { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
78 Ok(repo) => Ok(repo), |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
79 Err(RepoError::NotFound { at }) if repo_path.is_none() => { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
80 // Not finding a repo is not fatal yet, if `-R` was not given |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
81 Err(NoRepoInCwdError { cwd: at }) |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
82 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
83 Err(error) => return Err(error.into()), |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
84 }; |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
85 |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
86 run(&CliInvocation { |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
87 ui, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
88 subcommand_args, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
89 non_repo_config, |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
90 repo: repo.as_ref(), |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
91 }) |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
92 } |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
93 |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
94 fn main() { |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
95 let ui = ui::Ui::new(); |
46630
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
96 |
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
97 let exit_code = match main_with_result(&ui) { |
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46557
diff
changeset
|
98 Ok(()) => exitcode::OK, |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
99 |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
100 // 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
|
101 // 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
|
102 Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED, |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
103 |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
104 Err(CommandError::Abort { message }) => { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
105 if !message.is_empty() { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
106 // 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
|
107 // 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
|
108 let _ = |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
109 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
|
110 } |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
111 exitcode::ABORT |
45381
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45051
diff
changeset
|
112 } |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
113 }; |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45938
diff
changeset
|
114 std::process::exit(exit_code) |
45001 | 115 } |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
116 |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
117 macro_rules! subcommands { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
118 ($( $command: ident )+) => { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
119 mod commands { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
120 $( |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
121 pub mod $command; |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
122 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
123 } |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
124 |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
125 fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
126 app |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
127 $( |
46555
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46554
diff
changeset
|
128 .subcommand(add_global_args(commands::$command::args())) |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
129 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
130 } |
46543
a6e4e4650bac
rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
131 |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
132 pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>; |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
133 |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
134 fn subcommand_run_fn(name: &str) -> Option<RunFn> { |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
135 match name { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
136 $( |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
137 stringify!($command) => Some(commands::$command::run), |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
138 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
139 _ => None, |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
140 } |
45548
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45543
diff
changeset
|
141 } |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
142 }; |
45535
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45451
diff
changeset
|
143 } |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
144 |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
145 subcommands! { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
146 cat |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
147 debugdata |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
148 debugrequirements |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
149 files |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
150 root |
46557
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46556
diff
changeset
|
151 config |
46554
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46553
diff
changeset
|
152 } |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
153 pub struct CliInvocation<'a> { |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
154 ui: &'a Ui, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
155 subcommand_args: &'a ArgMatches<'a>, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
156 non_repo_config: &'a Config, |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
157 /// References inside `Result` is a bit peculiar but allow |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
158 /// `invocation.repo?` to work out with `&CliInvocation` since this |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
159 /// `Result` type is `Copy`. |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
160 repo: Result<&'a Repo, &'a NoRepoInCwdError>, |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
161 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
162 |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
163 struct NoRepoInCwdError { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
164 cwd: PathBuf, |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46630
diff
changeset
|
165 } |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
166 |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
167 impl CliInvocation<'_> { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
168 fn config(&self) -> &Config { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
169 if let Ok(repo) = self.repo { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
170 repo.config() |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
171 } else { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
172 self.non_repo_config |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
173 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
174 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
175 } |