annotate rust/rhg/src/commands/config.rs @ 52774:94e2547e6f3d

rust: move code from utils to utils::strings This moves string-related functions in hg::utils into the recently added hg::utils::strings module.
author Mitchell Kember <mkember@janestreet.com>
date Thu, 16 Jan 2025 13:15:02 -0500
parents 37bc3edef76f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45383
5dbf875b3275 rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45381
diff changeset
1 use crate::error::CommandError;
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
2 use clap::Arg;
45999
fada33872b5b rhg: use `format_bytes!` for error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 45449
diff changeset
3 use format_bytes::format_bytes;
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
4 use hg::errors::HgError;
52774
94e2547e6f3d rust: move code from utils to utils::strings
Mitchell Kember <mkember@janestreet.com>
parents: 49758
diff changeset
5 use hg::utils::strings::SliceExt;
45050
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 = "
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
8 With one argument of the form section.name, print just the value of that config item.
45050
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
49758
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46760
diff changeset
11 pub fn args() -> clap::Command {
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46760
diff changeset
12 clap::command!("config")
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
13 .arg(
49758
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46760
diff changeset
14 Arg::new("name")
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
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: 46555
diff changeset
16 .value_name("NAME")
49758
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46760
diff changeset
17 .required(true),
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
18 )
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
19 .about(HELP_TEXT)
46553
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
20 }
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
21
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
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: 46557
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: 46557
diff changeset
24 .subcommand_args
49758
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 46760
diff changeset
25 .get_one::<String>("name")
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
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: 46555
diff changeset
27 .as_bytes()
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
28 .split_2(b'.')
46739
92e3cfd63096 rhg: Fall back to Python on unsupported `rhg config <section>`
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
29 .ok_or_else(|| HgError::unsupported("hg config <section>"))?;
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
30
46760
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46739
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: 46739
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: 46739
diff changeset
33 Ok(())
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46739
diff changeset
34 } else {
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46739
diff changeset
35 Err(CommandError::Unsuccessful)
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46739
diff changeset
36 }
45050
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
37 }