Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use toml_edit::{DocumentMut, Item};
use crate::harness::{self, Harness, ModelAndEffort};

mod console;
pub use console::ConsoleSettings;
pub use console::{ConsoleAccess, ConsoleSettings};

/// The settings a User config can hold. Each is what a Run does when its
/// command says nothing about it.
Expand Down Expand Up @@ -289,6 +289,8 @@ pub struct UserConfigDocument {
/// The Setup settings to change. Other settings keep their current values.
/// Credentials and the decision to send a test email stay with Setup.
pub struct UserConfigChanges {
/// None retains all Console settings.
pub console: Option<ConsoleAccess>,
pub merge_always: bool,
pub base_fix: bool,
pub launch_pull: bool,
Expand Down Expand Up @@ -340,6 +342,18 @@ impl UserConfigDocument {
pub fn render(self, changes: Option<UserConfigChanges>) -> String {
let mut document = self.document;
if let Some(changes) = changes {
if let Some(console) = changes.console {
set(
&mut document,
"console",
"address",
console.address.as_str(),
);
if console.allowed_emails != self.settings.console.allowed_emails {
let emails: toml_edit::Array = console.allowed_emails.iter().collect();
set(&mut document, "console", "allowed_emails", emails);
}
}
set(&mut document, "merge", "always", changes.merge_always);
set(&mut document, "base", "fix", changes.base_fix);
set(&mut document, "launch", "pull", changes.launch_pull);
Expand Down Expand Up @@ -464,6 +478,10 @@ fn set(document: &mut DocumentMut, section: &str, key: &str, value: impl Into<to
.and_then(|settings| settings.get_mut(key))
.and_then(Item::as_value_mut)
.unwrap_or_else(|| panic!("a completed User config has {section}.{key}"));
if let (Some(existing), Some(replacement)) = (old.as_array_mut(), value.as_array()) {
edit_array(existing, replacement);
return;
}
let same = match (old.as_bool(), old.as_str()) {
(Some(old), _) => value.as_bool() == Some(old),
(_, Some(old)) => value.as_str() == Some(old),
Expand All @@ -488,6 +506,45 @@ fn set(document: &mut DocumentMut, section: &str, key: &str, value: impl Into<to
*old.decor_mut() = decor;
}

/// Retain element decoration and trailing comments when an allowlist changes.
/// Comments on removed entries stay before the closing bracket.
fn edit_array(existing: &mut toml_edit::Array, replacement: &toml_edit::Array) {
for (index, value) in replacement.iter().enumerate() {
if let Some(old) = existing.get(index) {
if old.as_str() != value.as_str() {
existing.replace(index, value.clone());
}
} else {
let mut value = value.clone();
let trailing = existing.trailing().as_str().unwrap_or("").to_string();
if trailing.contains('\n') || trailing.contains('#') {
// The old last entry's comment must precede the new entry.
let closing_indent = trailing.rsplit('\n').next().unwrap_or("");
let indent = existing
.iter()
.last()
.map(|last| decor_prefix(last.decor()))
.unwrap_or_default();
let indent = indent.rsplit('\n').next().unwrap_or("");
value.decor_mut().set_prefix(format!("{trailing}{indent}"));
existing.set_trailing(format!("\n{closing_indent}"));
}
existing.push_formatted(value);
}
}
let mut removed_decor = String::new();
while existing.len() > replacement.len() {
let removed = existing.remove(replacement.len());
removed_decor.push_str(&decor_prefix(removed.decor()));
removed_decor.push_str(&decor_suffix(removed.decor()));
}
if removed_decor.contains('#') {
removed_decor.push_str(existing.trailing().as_str().unwrap_or(""));
existing.set_trailing(removed_decor);
existing.set_trailing_comma(!existing.is_empty());
}
}

/// Set `email.to` in `document` to `to`. With no `email.to` there yet, it
/// takes the place of the commented-out one, if there is one, written as
/// the defaults would write it, with its comment.
Expand Down Expand Up @@ -1365,6 +1422,7 @@ mod tests {
assert_eq!(current.logs_dir, home.join(".thirdshift/logs"));

let text = document.render(Some(UserConfigChanges {
console: None,
merge_always: true,
base_fix: true,
launch_pull: true,
Expand Down Expand Up @@ -1519,6 +1577,7 @@ mod tests {

fn notifications_to(to: &str) -> UserConfigChanges {
UserConfigChanges {
console: None,
harness: None,
merge_always: false,
base_fix: false,
Expand Down Expand Up @@ -1614,6 +1673,7 @@ mod tests {
let text = "# top\n[merge]\n# before\nalways = false # merge\n# after\n\n\
[harness.codex]\nmodel = 'old' # model\neffort = 'high' # effort\n";
let changes = UserConfigChanges {
console: None,
merge_always: true,
base_fix: false,
launch_pull: false,
Expand Down Expand Up @@ -1659,6 +1719,7 @@ mod tests {
] {
let before = parse(text).unwrap();
let changes = UserConfigChanges {
console: None,
merge_always: true,
base_fix: true,
launch_pull: true,
Expand Down Expand Up @@ -1745,6 +1806,7 @@ mod tests {
}
let before = parse(&text).unwrap();
let changes = UserConfigChanges {
console: None,
merge_always: true,
base_fix: false,
launch_pull: true,
Expand Down
9 changes: 8 additions & 1 deletion src/config/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
use anyhow::{Result, ensure};
use serde::Deserialize;

/// The Console settings Setup asks about; other Console settings stay put.
#[derive(Clone)]
pub struct ConsoleAccess {
pub address: String,
pub allowed_emails: Vec<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ConsoleSettings {
Expand Down Expand Up @@ -49,7 +56,7 @@ impl ConsoleSettings {
Ok(host)
}

pub(super) fn validate(&self) -> Result<()> {
pub(crate) fn validate(&self) -> Result<()> {
if !self.address.is_empty() {
self.hostname()?;
}
Expand Down
Loading
Loading