Skip to content
Open
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
5 changes: 3 additions & 2 deletions crates/openshell-bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ pub use crate::docker::{
};
pub use crate::metadata::{
GatewayMetadata, clear_active_gateway, extract_host_from_ssh_destination, get_gateway_metadata,
list_gateways, load_active_gateway, load_gateway_metadata, load_last_sandbox,
remove_gateway_metadata, resolve_ssh_hostname, save_active_gateway, save_last_sandbox,
clear_last_sandbox_if_matches, list_gateways, load_active_gateway, load_gateway_metadata,
load_last_sandbox, remove_gateway_metadata, resolve_ssh_hostname, save_active_gateway,
save_last_sandbox,
store_gateway_metadata,
};

Expand Down
14 changes: 14 additions & 0 deletions crates/openshell-bootstrap/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ pub fn load_last_sandbox(gateway: &str) -> Option<String> {
if name.is_empty() { None } else { Some(name) }
}

/// Clear the last-used sandbox record for a gateway if it matches the given name.
///
/// This should be called after a sandbox is deleted so that subsequent commands
/// don't try to connect to a sandbox that no longer exists.
pub fn clear_last_sandbox_if_matches(gateway: &str, sandbox: &str) {
if let Some(current) = load_last_sandbox(gateway) {
if current == sandbox {
if let Ok(path) = last_sandbox_path(gateway) {
let _ = std::fs::remove_file(path);
}
}
}
}

/// List all gateways that have stored metadata.
///
/// Scans `$XDG_CONFIG_HOME/openshell/gateways/` for subdirectories containing
Expand Down
3 changes: 2 additions & 1 deletion crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,8 @@ async fn main() -> Result<()> {
run::sandbox_list(endpoint, limit, offset, ids, names, &tls).await?;
}
SandboxCommands::Delete { names, all } => {
run::sandbox_delete(endpoint, &names, all, &tls).await?;
run::sandbox_delete(endpoint, &names, all, &tls, &ctx.name)
.await?;
}
SandboxCommands::Connect { name, editor } => {
let name = resolve_sandbox_name(name, &ctx.name)?;
Expand Down
5 changes: 4 additions & 1 deletion crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use miette::{IntoDiagnostic, Result, WrapErr};
use openshell_bootstrap::{
DeployOptions, GatewayMetadata, RemoteOptions, clear_active_gateway, container_name,
extract_host_from_ssh_destination, get_gateway_metadata, list_gateways, load_active_gateway,
remove_gateway_metadata, resolve_ssh_hostname, save_active_gateway, save_last_sandbox,
clear_last_sandbox_if_matches, remove_gateway_metadata, resolve_ssh_hostname,
save_active_gateway, save_last_sandbox,
store_gateway_metadata,
};
use openshell_core::proto::{
Expand Down Expand Up @@ -2743,6 +2744,7 @@ pub async fn sandbox_delete(
names: &[String],
all: bool,
tls: &TlsOptions,
gateway: &str,
) -> Result<()> {
let mut client = grpc_client(server, tls).await?;

Expand Down Expand Up @@ -2783,6 +2785,7 @@ pub async fn sandbox_delete(

let deleted = response.into_inner().deleted;
if deleted {
clear_last_sandbox_if_matches(gateway, name);
println!("{} Deleted sandbox {name}", "✓".green().bold());
} else {
println!("{} Sandbox {name} not found", "!".yellow());
Expand Down
Loading