annotate rust/rhg/src/commands/config.rs @ 49640:37bc3edef76f

rhg: upgrade `clap` dependency This one is the worst one to upgrade since v2 -> v4 broke a ton of API, which thankfully seems saner now. Contrary to what was done in the `hg-core/src/examples/nodemap` rewrite, we're not switching from the "builder" pattern to the "derive" pattern, since that would imply a much larger diff. It can be done incrementally.
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 15 Nov 2022 00:02:43 +0100
parents b1e6265e8336
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45363
5dbf875b3275 rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45361
diff changeset
1 use crate::error::CommandError;
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
2 use clap::Arg;
45984
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45438
diff changeset
3 use format_bytes::format_bytes;
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
4 use hg::errors::HgError;
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
5 use hg::utils::SliceExt;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
6
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
7 pub const HELP_TEXT: &str = "
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
8 With one argument of the form section.name, print just the value of that config item.
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
9 ";
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
10
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 46747
diff changeset
11 pub fn args() -> clap::Command {
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 46747
diff changeset
12 clap::command!("config")
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
13 .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 46747
diff changeset
14 Arg::new("name")
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
15 .help("the section.name to print")
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
16 .value_name("NAME")
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 46747
diff changeset
17 .required(true),
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
18 )
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
19 .about(HELP_TEXT)
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
20 }
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
21
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
22 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
23 let (section, name) = invocation
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
24 .subcommand_args
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 46747
diff changeset
25 .get_one::<String>("name")
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
26 .expect("missing required CLI argument")
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
27 .as_bytes()
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
28 .split_2(b'.')
46726
92e3cfd63096 rhg: Fall back to Python on unsupported `rhg config <section>`
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
29 .ok_or_else(|| HgError::unsupported("hg config <section>"))?;
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
30
46747
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
31 if let Some(value) = invocation.config.get(section, name) {
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
32 invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
33 Ok(())
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
34 } else {
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
35 Err(CommandError::Unsuccessful)
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
36 }
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
37 }