Skip to content

Commit dceb539

Browse files
authored
feat(cli): fall back to last-used sandbox when name is omitted (#70)
1 parent ca1812d commit dceb539

11 files changed

Lines changed: 798 additions & 57 deletions

File tree

‎Cargo.lock‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/navigator-bootstrap/src/lib.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ mod pki;
1414
pub(crate) mod push;
1515
mod runtime;
1616

17+
/// Shared lock for tests that mutate the process-global `XDG_CONFIG_HOME`
18+
/// env var. All such tests in any module must hold this lock to avoid
19+
/// concurrent clobbering.
20+
#[cfg(test)]
21+
pub(crate) static XDG_TEST_LOCK: Mutex<()> = Mutex::new(());
22+
1723
use bollard::Docker;
1824
use miette::{IntoDiagnostic, Result};
1925
use std::path::{Path, PathBuf};
@@ -46,8 +52,8 @@ pub use crate::kubeconfig::{
4652
};
4753
pub use crate::metadata::{
4854
ClusterMetadata, clear_active_cluster, get_cluster_metadata, list_clusters,
49-
load_active_cluster, load_cluster_metadata, remove_cluster_metadata, save_active_cluster,
50-
store_cluster_metadata,
55+
load_active_cluster, load_cluster_metadata, load_last_sandbox, remove_cluster_metadata,
56+
save_active_cluster, save_last_sandbox, store_cluster_metadata,
5157
};
5258

5359
/// Options for remote SSH deployment.

‎crates/navigator-bootstrap/src/metadata.rs‎

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
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};
66
use miette::{IntoDiagnostic, Result, WrapErr};
77
use serde::{Deserialize, Serialize};
88
use std::path::PathBuf;
@@ -240,6 +240,30 @@ pub fn load_active_cluster() -> Option<String> {
240240
if name.is_empty() { None } else { Some(name) }
241241
}
242242

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+
243267
/// List all clusters that have stored metadata.
244268
///
245269
/// Scans `$XDG_CONFIG_HOME/navigator/clusters/` for `*_metadata.json` files
@@ -470,4 +494,106 @@ mod tests {
470494
let meta = create_cluster_metadata_with_host("test", None, 8080, None, None);
471495
assert_eq!(meta.gateway_endpoint, "https://127.0.0.1:8080");
472496
}
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+
}
473599
}

‎crates/navigator-bootstrap/src/paths.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,39 @@ pub fn active_cluster_path() -> Result<PathBuf> {
2727
pub fn clusters_dir() -> Result<PathBuf> {
2828
Ok(xdg_config_dir()?.join("nemoclaw").join("clusters"))
2929
}
30+
31+
/// Path to the file that stores the last-used sandbox name for a cluster.
32+
///
33+
/// Location: `$XDG_CONFIG_HOME/nemoclaw/clusters/<cluster>/last_sandbox`
34+
pub fn last_sandbox_path(cluster: &str) -> Result<PathBuf> {
35+
Ok(clusters_dir()?.join(cluster).join("last_sandbox"))
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
#[allow(unsafe_code)]
44+
fn last_sandbox_path_layout() {
45+
let _guard = crate::XDG_TEST_LOCK
46+
.lock()
47+
.unwrap_or_else(std::sync::PoisonError::into_inner);
48+
let tmp = tempfile::tempdir().unwrap();
49+
let orig = std::env::var("XDG_CONFIG_HOME").ok();
50+
unsafe {
51+
std::env::set_var("XDG_CONFIG_HOME", tmp.path());
52+
}
53+
let path = last_sandbox_path("my-cluster").unwrap();
54+
assert!(
55+
path.ends_with("nemoclaw/clusters/my-cluster/last_sandbox"),
56+
"unexpected path: {path:?}"
57+
);
58+
unsafe {
59+
match orig {
60+
Some(v) => std::env::set_var("XDG_CONFIG_HOME", v),
61+
None => std::env::remove_var("XDG_CONFIG_HOME"),
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)