rust/rhg/src/commands/config.rs
author Simon Sapin <simon.sapin@octobus.net>
Tue, 09 Mar 2021 09:17:24 +0100
changeset 46747 b1e6265e8336
parent 46726 92e3cfd63096
child 49640 37bc3edef76f
permissions -rw-r--r--
rhg: Return an error code for `rhg config Section.idontexist` This is what Python-based hg does. Differential Revision: https://phab.mercurial-scm.org/D10145
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;
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
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    11
pub fn args() -> clap::App<'static, 'static> {
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    12
    clap::SubCommand::with_name("config")
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    13
        .arg(
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    14
            Arg::with_name("name")
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")
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    17
                .required(true)
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    18
                .takes_value(true),
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    19
        )
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    20
        .about(HELP_TEXT)
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    21
}
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
    22
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
    23
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
    24
    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
    25
        .subcommand_args
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    26
        .value_of("name")
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    27
        .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
    28
        .as_bytes()
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    29
        .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
    30
        .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
    31
46747
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
    32
    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
    33
        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
    34
        Ok(())
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
    35
    } else {
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
    36
        Err(CommandError::Unsuccessful)
b1e6265e8336 rhg: Return an error code for `rhg config Section.idontexist`
Simon Sapin <simon.sapin@octobus.net>
parents: 46726
diff changeset
    37
    }
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    38
}