|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | use crate::RemoteOptions; |
5 | | -use crate::paths::{active_cluster_path, clusters_dir, xdg_config_dir}; |
| 5 | +use crate::paths::{active_cluster_path, clusters_dir, last_sandbox_path, xdg_config_dir}; |
6 | 6 | use miette::{IntoDiagnostic, Result, WrapErr}; |
7 | 7 | use serde::{Deserialize, Serialize}; |
8 | 8 | use std::path::PathBuf; |
@@ -240,6 +240,30 @@ pub fn load_active_cluster() -> Option<String> { |
240 | 240 | if name.is_empty() { None } else { Some(name) } |
241 | 241 | } |
242 | 242 |
|
| 243 | +/// Save the last-used sandbox name for a cluster to persistent storage. |
| 244 | +pub fn save_last_sandbox(cluster: &str, sandbox: &str) -> Result<()> { |
| 245 | + let path = last_sandbox_path(cluster)?; |
| 246 | + if let Some(parent) = path.parent() { |
| 247 | + std::fs::create_dir_all(parent) |
| 248 | + .into_diagnostic() |
| 249 | + .wrap_err_with(|| format!("failed to create {}", parent.display()))?; |
| 250 | + } |
| 251 | + std::fs::write(&path, sandbox) |
| 252 | + .into_diagnostic() |
| 253 | + .wrap_err_with(|| format!("failed to write last sandbox to {}", path.display()))?; |
| 254 | + Ok(()) |
| 255 | +} |
| 256 | + |
| 257 | +/// Load the last-used sandbox name for a cluster from persistent storage. |
| 258 | +/// |
| 259 | +/// Returns `None` if no last sandbox has been set. |
| 260 | +pub fn load_last_sandbox(cluster: &str) -> Option<String> { |
| 261 | + let path = last_sandbox_path(cluster).ok()?; |
| 262 | + let contents = std::fs::read_to_string(&path).ok()?; |
| 263 | + let name = contents.trim().to_string(); |
| 264 | + if name.is_empty() { None } else { Some(name) } |
| 265 | +} |
| 266 | + |
243 | 267 | /// List all clusters that have stored metadata. |
244 | 268 | /// |
245 | 269 | /// Scans `$XDG_CONFIG_HOME/navigator/clusters/` for `*_metadata.json` files |
@@ -470,4 +494,106 @@ mod tests { |
470 | 494 | let meta = create_cluster_metadata_with_host("test", None, 8080, None, None); |
471 | 495 | assert_eq!(meta.gateway_endpoint, "https://127.0.0.1:8080"); |
472 | 496 | } |
| 497 | + |
| 498 | + // ── last-sandbox persistence ────────────────────────────────────── |
| 499 | + |
| 500 | + /// Helper: hold the shared XDG test lock, set `XDG_CONFIG_HOME` to a |
| 501 | + /// tempdir, run `f`, then restore the original value. |
| 502 | + #[allow(unsafe_code)] |
| 503 | + fn with_tmp_xdg<F: FnOnce()>(tmp: &std::path::Path, f: F) { |
| 504 | + let _guard = crate::XDG_TEST_LOCK |
| 505 | + .lock() |
| 506 | + .unwrap_or_else(std::sync::PoisonError::into_inner); |
| 507 | + let orig = std::env::var("XDG_CONFIG_HOME").ok(); |
| 508 | + unsafe { |
| 509 | + std::env::set_var("XDG_CONFIG_HOME", tmp); |
| 510 | + } |
| 511 | + f(); |
| 512 | + unsafe { |
| 513 | + match orig { |
| 514 | + Some(v) => std::env::set_var("XDG_CONFIG_HOME", v), |
| 515 | + None => std::env::remove_var("XDG_CONFIG_HOME"), |
| 516 | + } |
| 517 | + } |
| 518 | + } |
| 519 | + |
| 520 | + #[test] |
| 521 | + fn save_and_load_last_sandbox_roundtrip() { |
| 522 | + let tmp = tempfile::tempdir().unwrap(); |
| 523 | + with_tmp_xdg(tmp.path(), || { |
| 524 | + save_last_sandbox("mycluster", "dev-box").unwrap(); |
| 525 | + assert_eq!(load_last_sandbox("mycluster"), Some("dev-box".to_string())); |
| 526 | + }); |
| 527 | + } |
| 528 | + |
| 529 | + #[test] |
| 530 | + fn load_last_sandbox_returns_none_when_not_set() { |
| 531 | + let tmp = tempfile::tempdir().unwrap(); |
| 532 | + with_tmp_xdg(tmp.path(), || { |
| 533 | + assert_eq!(load_last_sandbox("no-such-cluster"), None); |
| 534 | + }); |
| 535 | + } |
| 536 | + |
| 537 | + #[test] |
| 538 | + fn save_last_sandbox_overwrites_previous() { |
| 539 | + let tmp = tempfile::tempdir().unwrap(); |
| 540 | + with_tmp_xdg(tmp.path(), || { |
| 541 | + save_last_sandbox("c1", "first").unwrap(); |
| 542 | + save_last_sandbox("c1", "second").unwrap(); |
| 543 | + assert_eq!(load_last_sandbox("c1"), Some("second".to_string())); |
| 544 | + }); |
| 545 | + } |
| 546 | + |
| 547 | + #[test] |
| 548 | + fn save_last_sandbox_creates_parent_dirs() { |
| 549 | + let tmp = tempfile::tempdir().unwrap(); |
| 550 | + with_tmp_xdg(tmp.path(), || { |
| 551 | + // The cluster subdir doesn't exist yet — save should create it. |
| 552 | + save_last_sandbox("brand-new-cluster", "sb1").unwrap(); |
| 553 | + assert_eq!( |
| 554 | + load_last_sandbox("brand-new-cluster"), |
| 555 | + Some("sb1".to_string()) |
| 556 | + ); |
| 557 | + }); |
| 558 | + } |
| 559 | + |
| 560 | + #[test] |
| 561 | + fn load_last_sandbox_ignores_whitespace() { |
| 562 | + let tmp = tempfile::tempdir().unwrap(); |
| 563 | + with_tmp_xdg(tmp.path(), || { |
| 564 | + // Write the file manually with surrounding whitespace. |
| 565 | + let path = last_sandbox_path("ws-cluster").unwrap(); |
| 566 | + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 567 | + std::fs::write(&path, " my-sb \n").unwrap(); |
| 568 | + assert_eq!(load_last_sandbox("ws-cluster"), Some("my-sb".to_string())); |
| 569 | + }); |
| 570 | + } |
| 571 | + |
| 572 | + #[test] |
| 573 | + fn load_last_sandbox_returns_none_for_empty_file() { |
| 574 | + let tmp = tempfile::tempdir().unwrap(); |
| 575 | + with_tmp_xdg(tmp.path(), || { |
| 576 | + let path = last_sandbox_path("empty-cluster").unwrap(); |
| 577 | + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 578 | + std::fs::write(&path, " \n").unwrap(); |
| 579 | + assert_eq!(load_last_sandbox("empty-cluster"), None); |
| 580 | + }); |
| 581 | + } |
| 582 | + |
| 583 | + #[test] |
| 584 | + fn last_sandbox_is_per_cluster() { |
| 585 | + let tmp = tempfile::tempdir().unwrap(); |
| 586 | + with_tmp_xdg(tmp.path(), || { |
| 587 | + save_last_sandbox("cluster-a", "sandbox-a").unwrap(); |
| 588 | + save_last_sandbox("cluster-b", "sandbox-b").unwrap(); |
| 589 | + assert_eq!( |
| 590 | + load_last_sandbox("cluster-a"), |
| 591 | + Some("sandbox-a".to_string()) |
| 592 | + ); |
| 593 | + assert_eq!( |
| 594 | + load_last_sandbox("cluster-b"), |
| 595 | + Some("sandbox-b".to_string()) |
| 596 | + ); |
| 597 | + }); |
| 598 | + } |
473 | 599 | } |
0 commit comments