Debian Codegraph @

cli/src/commands/config/unset.rs

raw
1 // Copyright 2024 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 clap_complete::ArgValueCandidates;
16 use jj_lib::config::ConfigNamePathBuf;
17 use tracing::instrument;
18
19 use super::ConfigTargetArgs;
20 use crate::cli_util::CommandHelper;
21 use crate::command_error::CommandError;
22 use crate::command_error::user_error;
23 use crate::command_error::user_error_with_message;
24 use crate::complete;
25 use crate::ui::Ui;
26
27 /// Update a config file to unset the given option.
28 #[derive(clap::Args, Clone, Debug)]
29 pub struct ConfigUnsetArgs {
30 #[arg(required = true)]
31 #[arg(add = ArgValueCandidates::new(complete::config_keys_to_unset))]
32 name: ConfigNamePathBuf,
33
34 #[command(flatten)]
35 target: ConfigTargetArgs,
36 }
37
38 #[instrument(skip_all)]
39 pub async fn cmd_config_unset(
40 ui: &mut Ui,
41 command: &CommandHelper,
42 args: &ConfigUnsetArgs,
43 ) -> Result<(), CommandError> {
44 let mut file = args.target.edit_config_file(ui, command)?;
45 let old_value = file
46 .delete_value(&args.name)
47 .map_err(|err| user_error_with_message(format!("Failed to unset {}", args.name), err))?;
48 if old_value.is_none() {
49 return Err(user_error(format!(r#""{}" doesn't exist"#, args.name)));
50 }
51 file.save()?;
52 Ok(())
53 }