1use std::path::{Path, PathBuf};
4
5use anyhow::{Result, bail};
6
7use super::{TEAM_CONFIG_FILE, team_config_dir};
8
9const TOPOLOGY_RELOAD_MARKER: &str = "reload-topology";
10
11fn topology_reload_marker_path(project_root: &Path) -> PathBuf {
12 team_config_dir(project_root).join(TOPOLOGY_RELOAD_MARKER)
13}
14
15pub(crate) fn consume_topology_reload_request(project_root: &Path) -> Result<bool> {
16 let path = topology_reload_marker_path(project_root);
17 if !path.exists() {
18 return Ok(false);
19 }
20 std::fs::remove_file(&path)?;
21 Ok(true)
22}
23
24pub fn request_topology_reload(project_root: &Path) -> Result<()> {
25 let team_dir = team_config_dir(project_root);
26 let config_path = team_dir.join(TEAM_CONFIG_FILE);
27 if !config_path.exists() {
28 bail!(
29 "no team config found at {}; run `batty init` first",
30 config_path.display()
31 );
32 }
33
34 std::fs::create_dir_all(&team_dir)?;
35 std::fs::write(topology_reload_marker_path(project_root), b"")?;
36 Ok(())
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn consume_reload_request_clears_marker() {
45 let tmp = tempfile::tempdir().unwrap();
46 let team_dir = team_config_dir(tmp.path());
47 std::fs::create_dir_all(&team_dir).unwrap();
48 let marker = topology_reload_marker_path(tmp.path());
49 std::fs::write(&marker, b"").unwrap();
50
51 assert!(marker.exists());
52 assert!(consume_topology_reload_request(tmp.path()).unwrap());
53 assert!(!marker.exists());
54 assert!(!consume_topology_reload_request(tmp.path()).unwrap());
55 }
56}