rust/rhg/src/commands/config.rs
changeset 46505 a25033eb43b5
parent 46503 d8730ff51d5a
child 46592 80840b651721
equal deleted inserted replaced
46504:2e5dd18d6dc3 46505:a25033eb43b5
       
     1 use crate::error::CommandError;
       
     2 use crate::ui::Ui;
       
     3 use clap::Arg;
       
     4 use clap::ArgMatches;
       
     5 use format_bytes::format_bytes;
       
     6 use hg::config::Config;
       
     7 use hg::errors::HgError;
       
     8 use hg::repo::Repo;
       
     9 use hg::utils::SliceExt;
       
    10 use std::path::Path;
       
    11 
       
    12 pub const HELP_TEXT: &str = "
       
    13 With one argument of the form section.name, print just the value of that config item.
       
    14 ";
       
    15 
       
    16 pub fn args() -> clap::App<'static, 'static> {
       
    17     clap::SubCommand::with_name("config")
       
    18         .arg(
       
    19             Arg::with_name("name")
       
    20                 .help("the section.name to print")
       
    21                 .value_name("NAME")
       
    22                 .required(true)
       
    23                 .takes_value(true),
       
    24         )
       
    25         .about(HELP_TEXT)
       
    26 }
       
    27 
       
    28 pub fn run(
       
    29     ui: &Ui,
       
    30     config: &Config,
       
    31     repo_path: Option<&Path>,
       
    32     args: &ArgMatches,
       
    33 ) -> Result<(), CommandError> {
       
    34     let opt_repo = Repo::find_optional(config, repo_path)?;
       
    35     let config = if let Some(repo) = &opt_repo {
       
    36         repo.config()
       
    37     } else {
       
    38         config
       
    39     };
       
    40 
       
    41     let (section, name) = args
       
    42         .value_of("name")
       
    43         .expect("missing required CLI argument")
       
    44         .as_bytes()
       
    45         .split_2(b'.')
       
    46         .ok_or_else(|| HgError::abort(""))?;
       
    47 
       
    48     let value = config.get(section, name).unwrap_or(b"");
       
    49 
       
    50     ui.write_stdout(&format_bytes!(b"{}\n", value))?;
       
    51     Ok(())
       
    52 }