rust/rhg/src/commands/config.rs
changeset 46593 5ce2aa7c2ad5
parent 46592 80840b651721
child 46665 7284b524b441
equal deleted inserted replaced
46592:80840b651721 46593:5ce2aa7c2ad5
     1 use crate::error::CommandError;
     1 use crate::error::CommandError;
     2 use clap::Arg;
     2 use clap::Arg;
     3 use format_bytes::format_bytes;
     3 use format_bytes::format_bytes;
     4 use hg::errors::HgError;
     4 use hg::errors::HgError;
     5 use hg::repo::Repo;
       
     6 use hg::utils::SliceExt;
     5 use hg::utils::SliceExt;
     7 
     6 
     8 pub const HELP_TEXT: &str = "
     7 pub const HELP_TEXT: &str = "
     9 With one argument of the form section.name, print just the value of that config item.
     8 With one argument of the form section.name, print just the value of that config item.
    10 ";
     9 ";
    20         )
    19         )
    21         .about(HELP_TEXT)
    20         .about(HELP_TEXT)
    22 }
    21 }
    23 
    22 
    24 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    23 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    25     let opt_repo =
       
    26         Repo::find_optional(invocation.non_repo_config, invocation.repo_path)?;
       
    27     let config = if let Some(repo) = &opt_repo {
       
    28         repo.config()
       
    29     } else {
       
    30         invocation.non_repo_config
       
    31     };
       
    32     let (section, name) = invocation
    24     let (section, name) = invocation
    33         .subcommand_args
    25         .subcommand_args
    34         .value_of("name")
    26         .value_of("name")
    35         .expect("missing required CLI argument")
    27         .expect("missing required CLI argument")
    36         .as_bytes()
    28         .as_bytes()
    37         .split_2(b'.')
    29         .split_2(b'.')
    38         .ok_or_else(|| HgError::abort(""))?;
    30         .ok_or_else(|| HgError::abort(""))?;
    39 
    31 
    40     let value = config.get(section, name).unwrap_or(b"");
    32     let value = invocation.config().get(section, name).unwrap_or(b"");
    41 
    33 
    42     invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
    34     invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
    43     Ok(())
    35     Ok(())
    44 }
    36 }