audb_core/features/device/
remove.rs

1use crate::features::config::{device_store::DeviceStore, state::DeviceState};
2use crate::tools::types::DeviceIdentifier;
3use anyhow::Result;
4use dialoguer::Confirm;
5
6pub async fn execute(identifier: &str) -> Result<()> {
7    let device_id = DeviceIdentifier::parse(identifier);
8    let device = DeviceStore::find(&device_id)?;
9
10    println!("\x1b[1mDevice to remove:\x1b[0m");
11    println!("  Name: {}", device.display_name());
12    println!("  Host: {}", device.host);
13    println!("  Port: {}", device.port);
14    println!("  Platform: {}", device.platform);
15
16    let confirmed = Confirm::new()
17        .with_prompt("Are you sure you want to remove this device?")
18        .default(false)
19        .interact()?;
20
21    if !confirmed {
22        println!("Cancelled.");
23        return Ok(());
24    }
25
26    // Check if this is the currently selected device
27    let current_host = DeviceState::get_current().ok();
28    let is_current = current_host.as_ref() == Some(&device.host);
29
30    // Remove device
31    DeviceStore::remove(&device_id)?;
32
33    // Clear current device if needed
34    if is_current {
35        DeviceState::clear_current()?;
36        println!("\x1b[1m\x1b[94minfo\x1b[0m: Current device selection cleared");
37    }
38
39    println!("\x1b[1m\x1b[32msuccess\x1b[0m: Device removed successfully");
40    Ok(())
41}