From a5c5f4a71326d52cfedaa44bff80fe865561ba96 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Wed, 21 Feb 2024 16:50:34 +0100 Subject: [PATCH] fix(config): add option to last category --- src/commands/config.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index a2a4c67..54f4aea 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -13,6 +13,7 @@ pub fn config_set(args: ConfigSetArgs) { // configure possible options and their associated category let mut option_categories: HashMap<&str, &str> = HashMap::new(); option_categories.insert("force_insecure", "core"); + option_categories.insert("token", "core"); let name = args.name.unwrap().next().unwrap(); let value = args.value.unwrap().next().unwrap(); @@ -28,7 +29,7 @@ pub fn config_set(args: ConfigSetArgs) { } -fn find_option_in_cat(category: &str, option: &str) -> Option { +pub fn find_option_in_cat(category: &str, option: &str) -> Option { let mut root = path::nextsync(); root.push("config"); @@ -83,7 +84,13 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res let trimmed_line = line.trim(); if trimmed_line.starts_with('[') && trimmed_line.ends_with(']') { - in_target_category = trimmed_line == format!("[{}]", category); + // if we were already in target category we are now leaving it + // add option only if not found before + if in_target_category && !option_found { + writeln!(&mut tmp_file, "\t{} = {}", option, value)?; + } else if !in_target_category { + in_target_category = trimmed_line == format!("[{}]", category); + } } if in_target_category && !option_found && trimmed_line.starts_with(&format!("{} =", option)) { @@ -96,8 +103,13 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res } } - if !option_found { - // If the option doesn't exist, add it to the category + // add to last category + if in_target_category && !option_found { + writeln!(&mut tmp_file, "\t{} = {}", option, value)?; + } + + // if the category didn't exist create it and add the option + if !in_target_category { writeln!(&mut tmp_file, "[{}]", category)?; writeln!(&mut tmp_file, "\t{} = {}", option, value)?; }