rust/rhg/src/commands/config.rs
author Simon Sapin <simon.sapin@octobus.net>
Thu, 04 Mar 2021 13:30:20 +0100
changeset 46735 12d59eec7f1d
parent 46726 92e3cfd63096
child 46747 b1e6265e8336
permissions -rw-r--r--
rhg: Align with Python on some more error messages Differences can cause some tests to fail Differential Revision: https://phab.mercurial-scm.org/D10133

use crate::error::CommandError;
use clap::Arg;
use format_bytes::format_bytes;
use hg::errors::HgError;
use hg::utils::SliceExt;

pub const HELP_TEXT: &str = "
With one argument of the form section.name, print just the value of that config item.
";

pub fn args() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("config")
        .arg(
            Arg::with_name("name")
                .help("the section.name to print")
                .value_name("NAME")
                .required(true)
                .takes_value(true),
        )
        .about(HELP_TEXT)
}

pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    let (section, name) = invocation
        .subcommand_args
        .value_of("name")
        .expect("missing required CLI argument")
        .as_bytes()
        .split_2(b'.')
        .ok_or_else(|| HgError::unsupported("hg config <section>"))?;

    let value = invocation.config.get(section, name).unwrap_or(b"");

    invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
    Ok(())
}