| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::io::Write as _; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use jj_lib::config::ConfigNamePathBuf; |
| 19 |
use jj_lib::config::ConfigValue; |
| 20 |
use tracing::instrument; |
| 21 |
|
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::command_error::CommandError; |
| 24 |
use crate::complete; |
| 25 |
use crate::ui::Ui; |
| 26 |
|
| 27 |
/// Get the value of a given config option. |
| 28 |
/// |
| 29 |
/// Unlike `jj config list`, the result of `jj config get` is printed without |
| 30 |
/// extra formatting and therefore is usable in scripting. For example: |
| 31 |
/// |
| 32 |
/// $ jj config list user.name |
| 33 |
/// user.name="Martin von Zweigbergk" |
| 34 |
/// $ jj config get user.name |
| 35 |
/// Martin von Zweigbergk |
| 36 |
#[derive(clap::Args, Clone, Debug)] |
| 37 |
#[command(verbatim_doc_comment)] |
| 38 |
pub struct ConfigGetArgs { |
| 39 |
#[arg(required = true)] |
| 40 |
#[arg(add = ArgValueCandidates::new(complete::leaf_config_keys))] |
| 41 |
name: ConfigNamePathBuf, |
| 42 |
} |
| 43 |
|
| 44 |
#[instrument(skip_all)] |
| 45 |
pub async fn cmd_config_get( |
| 46 |
ui: &mut Ui, |
| 47 |
command: &CommandHelper, |
| 48 |
args: &ConfigGetArgs, |
| 49 |
) -> Result<(), CommandError> { |
| 50 |
let value = command.settings().get_value(&args.name)?; |
| 51 |
let stringified = match value { |
| 52 |
// Remove extra formatting from a string value |
| 53 |
ConfigValue::String(v) => v.into_value(), |
| 54 |
// Print other values in TOML syntax (but whitespace trimmed) |
| 55 |
ConfigValue::Integer(_) |
| 56 |
| ConfigValue::Float(_) |
| 57 |
| ConfigValue::Boolean(_) |
| 58 |
| ConfigValue::Datetime(_) |
| 59 |
| ConfigValue::Array(_) |
| 60 |
| ConfigValue::InlineTable(_) => value.decorated("", "").to_string(), |
| 61 |
}; |
| 62 |
writeln!(ui.stdout(), "{stringified}")?; |
| 63 |
Ok(()) |
| 64 |
} |