just playing with tangled
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
15use std::io::Write as _;
16
17use clap_complete::ArgValueCandidates;
18use jj_lib::config::ConfigNamePathBuf;
19use jj_lib::config::ConfigValue;
20use tracing::instrument;
21
22use crate::cli_util::CommandHelper;
23use crate::command_error::CommandError;
24use crate::complete;
25use 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)]
38pub struct ConfigGetArgs {
39 #[arg(required = true, add = ArgValueCandidates::new(complete::leaf_config_keys))]
40 name: ConfigNamePathBuf,
41}
42
43#[instrument(skip_all)]
44pub fn cmd_config_get(
45 ui: &mut Ui,
46 command: &CommandHelper,
47 args: &ConfigGetArgs,
48) -> Result<(), CommandError> {
49 let stringified = command
50 .settings()
51 .get_value_with(&args.name, |value| match value {
52 // Remove extra formatting from a string value
53 ConfigValue::String(v) => Ok(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(_) => Ok(value.decorated("", "").to_string()),
59 // TODO: maybe okay to just print array or table in TOML syntax?
60 ConfigValue::Array(_) => {
61 Err("Expected a value convertible to a string, but is an array")
62 }
63 ConfigValue::InlineTable(_) => {
64 Err("Expected a value convertible to a string, but is a table")
65 }
66 })?;
67 writeln!(ui.stdout(), "{stringified}")?;
68 Ok(())
69}