rust/rhg/src/commands/config.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 11 Mar 2025 02:29:42 +0100
branchstable
changeset 53042 cdd7bf612c7b
parent 52760 94e2547e6f3d
permissions -rw-r--r--
bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
52760
94e2547e6f3d rust: move code from utils to utils::strings
Mitchell Kember <mkember@janestreet.com>
parents: 49640
diff changeset
     5
use hg::utils::strings::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
}