fix(config): add option to last category

This commit is contained in:
grimhilt 2024-02-21 16:50:34 +01:00
parent eaacff0e55
commit a5c5f4a713

View File

@ -13,6 +13,7 @@ pub fn config_set(args: ConfigSetArgs) {
// configure possible options and their associated category // configure possible options and their associated category
let mut option_categories: HashMap<&str, &str> = HashMap::new(); let mut option_categories: HashMap<&str, &str> = HashMap::new();
option_categories.insert("force_insecure", "core"); option_categories.insert("force_insecure", "core");
option_categories.insert("token", "core");
let name = args.name.unwrap().next().unwrap(); let name = args.name.unwrap().next().unwrap();
let value = args.value.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<String> { pub fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
let mut root = path::nextsync(); let mut root = path::nextsync();
root.push("config"); root.push("config");
@ -83,8 +84,14 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res
let trimmed_line = line.trim(); let trimmed_line = line.trim();
if trimmed_line.starts_with('[') && trimmed_line.ends_with(']') { if trimmed_line.starts_with('[') && trimmed_line.ends_with(']') {
// 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); in_target_category = trimmed_line == format!("[{}]", category);
} }
}
if in_target_category && !option_found && trimmed_line.starts_with(&format!("{} =", option)) { if in_target_category && !option_found && trimmed_line.starts_with(&format!("{} =", option)) {
// Option already exists, update its value // Option already exists, update its value
@ -96,8 +103,13 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res
} }
} }
if !option_found { // add to last category
// If the option doesn't exist, add it to the 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, "[{}]", category)?;
writeln!(&mut tmp_file, "\t{} = {}", option, value)?; writeln!(&mut tmp_file, "\t{} = {}", option, value)?;
} }