|
1 | 1 | //! Cache management commands |
2 | 2 | //! |
3 | | -//! Commands for managing the session cache: |
4 | | -//! - `clear-cache`: Clear all cached sessions |
5 | | -//! - `list-cache`: List all cached sessions with fingerprints |
| 3 | +//! Commands for managing the session cache and identity keys: |
| 4 | +//! - `cache list`: List cached sessions and identity fingerprints |
| 5 | +//! - `cache clear [sessions|all]`: Clear cached sessions and/or identity keys |
6 | 6 |
|
7 | 7 | use bw_rat_client::SessionStore; |
8 | | -use clap::Args; |
| 8 | +use clap::{Args, Subcommand, ValueEnum}; |
9 | 9 | use color_eyre::eyre::Result; |
10 | 10 |
|
11 | 11 | use super::util::format_relative_time; |
12 | | -use crate::storage::FileSessionCache; |
| 12 | +use crate::storage::{FileIdentityStorage, FileSessionCache}; |
13 | 13 |
|
14 | | -/// Arguments for the clear-cache command |
| 14 | +/// Which client type to operate on |
| 15 | +#[derive(Clone, Copy, Debug, ValueEnum)] |
| 16 | +pub enum ClientType { |
| 17 | + /// Only the remote (connect) side |
| 18 | + Remote, |
| 19 | + /// Only the user (listen) side |
| 20 | + User, |
| 21 | +} |
| 22 | + |
| 23 | +/// What to clear |
| 24 | +#[derive(Clone, Copy, Debug, Default, ValueEnum)] |
| 25 | +pub enum ClearScope { |
| 26 | + /// Clear sessions only (keep identity key) |
| 27 | + Sessions, |
| 28 | + /// Clear sessions and delete identity key |
| 29 | + #[default] |
| 30 | + All, |
| 31 | +} |
| 32 | + |
| 33 | +/// Manage the session cache |
15 | 34 | #[derive(Args)] |
16 | | -pub struct ClearCacheArgs; |
| 35 | +pub struct CacheArgs { |
| 36 | + #[command(subcommand)] |
| 37 | + command: CacheCommands, |
17 | 38 |
|
18 | | -impl ClearCacheArgs { |
19 | | - /// Execute the clear-cache command |
| 39 | + /// Limit to a specific client type (omit for both) |
| 40 | + #[arg(long, global = true)] |
| 41 | + client_type: Option<ClientType>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Subcommand)] |
| 45 | +enum CacheCommands { |
| 46 | + /// Clear cached sessions and/or identity keys |
| 47 | + Clear { |
| 48 | + /// What to clear: "sessions" (keep identity key) or "all" (sessions + identity key) |
| 49 | + #[arg(default_value = "all")] |
| 50 | + scope: ClearScope, |
| 51 | + }, |
| 52 | + /// List cached sessions and identity info |
| 53 | + List, |
| 54 | +} |
| 55 | + |
| 56 | +impl CacheArgs { |
20 | 57 | pub fn run(self) -> Result<()> { |
21 | | - let mut cache = FileSessionCache::load_or_create("remote_client")?; |
22 | | - cache.clear()?; |
23 | | - println!("Session cache cleared."); |
24 | | - Ok(()) |
| 58 | + match self.command { |
| 59 | + CacheCommands::Clear { scope } => clear_cache(self.client_type, scope), |
| 60 | + CacheCommands::List => list_cache(self.client_type), |
| 61 | + } |
25 | 62 | } |
26 | 63 | } |
27 | 64 |
|
28 | | -/// Arguments for the list-cache command |
29 | | -#[derive(Args)] |
30 | | -pub struct ListCacheArgs; |
| 65 | +/// Describes one client side for display and storage lookup |
| 66 | +struct CacheSide { |
| 67 | + label: &'static str, |
| 68 | + description: &'static str, |
| 69 | + storage_name: &'static str, |
| 70 | +} |
31 | 71 |
|
32 | | -impl ListCacheArgs { |
33 | | - /// Execute the list-cache command |
34 | | - pub fn run(self) -> Result<()> { |
35 | | - let cache = FileSessionCache::load_or_create("remote_client")?; |
36 | | - let mut sessions = cache.list_sessions(); |
| 72 | +const REMOTE_SIDE: CacheSide = CacheSide { |
| 73 | + label: "Remote", |
| 74 | + description: "connect", |
| 75 | + storage_name: "remote_client", |
| 76 | +}; |
37 | 77 |
|
38 | | - if sessions.is_empty() { |
39 | | - println!("No cached sessions."); |
40 | | - return Ok(()); |
| 78 | +const USER_SIDE: CacheSide = CacheSide { |
| 79 | + label: "User", |
| 80 | + description: "listen", |
| 81 | + storage_name: "user_client", |
| 82 | +}; |
| 83 | + |
| 84 | +fn sides_for(client_type: Option<ClientType>) -> Vec<&'static CacheSide> { |
| 85 | + match client_type { |
| 86 | + Some(ClientType::Remote) => vec![&REMOTE_SIDE], |
| 87 | + Some(ClientType::User) => vec![&USER_SIDE], |
| 88 | + None => vec![&REMOTE_SIDE, &USER_SIDE], |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn clear_cache(client_type: Option<ClientType>, scope: ClearScope) -> Result<()> { |
| 93 | + let sides = sides_for(client_type); |
| 94 | + |
| 95 | + for side in &sides { |
| 96 | + // Always clear sessions |
| 97 | + let mut cache = FileSessionCache::load_or_create(side.storage_name)?; |
| 98 | + cache.clear()?; |
| 99 | + println!( |
| 100 | + "{} ({}) session cache cleared.", |
| 101 | + side.label, side.description |
| 102 | + ); |
| 103 | + |
| 104 | + // Clear identity key if scope is All |
| 105 | + if matches!(scope, ClearScope::All) { |
| 106 | + FileIdentityStorage::delete(side.storage_name)?; |
| 107 | + println!( |
| 108 | + "{} ({}) identity key deleted.", |
| 109 | + side.label, side.description |
| 110 | + ); |
41 | 111 | } |
| 112 | + } |
42 | 113 |
|
43 | | - // Sort by last_connected descending (most recent first) |
44 | | - sessions.sort_by(|a, b| b.3.cmp(&a.3)); |
| 114 | + Ok(()) |
| 115 | +} |
45 | 116 |
|
46 | | - for (fingerprint, name, _cached_at, last_connected) in &sessions { |
47 | | - let hex = hex::encode(fingerprint.0); |
48 | | - let relative = format_relative_time(*last_connected); |
49 | | - if let Some(name) = name { |
50 | | - println!("{name} {hex} (last used: {relative})"); |
51 | | - } else { |
52 | | - println!("{hex} (last used: {relative})"); |
53 | | - } |
| 117 | +fn list_cache(client_type: Option<ClientType>) -> Result<()> { |
| 118 | + let sides = sides_for(client_type); |
| 119 | + for (i, side) in sides.iter().enumerate() { |
| 120 | + if i > 0 { |
| 121 | + println!(); |
54 | 122 | } |
55 | 123 |
|
56 | | - Ok(()) |
| 124 | + // Load identity fingerprint (if key exists) |
| 125 | + let fingerprint = FileIdentityStorage::load_fingerprint(side.storage_name)?; |
| 126 | + let fp_display = match &fingerprint { |
| 127 | + Some(fp) => hex::encode(fp.0), |
| 128 | + None => "no identity key".to_string(), |
| 129 | + }; |
| 130 | + |
| 131 | + println!( |
| 132 | + "{} ({}) \u{2014} identity: {}", |
| 133 | + side.label, side.description, fp_display |
| 134 | + ); |
| 135 | + |
| 136 | + // Load and display sessions |
| 137 | + let cache = FileSessionCache::load_or_create(side.storage_name)?; |
| 138 | + let mut sessions = cache.list_sessions(); |
| 139 | + |
| 140 | + if sessions.is_empty() { |
| 141 | + println!(" No cached sessions."); |
| 142 | + } else { |
| 143 | + sessions.sort_by(|a, b| b.3.cmp(&a.3)); |
| 144 | + for (session_fp, name, _cached_at, last_connected) in &sessions { |
| 145 | + let hex = hex::encode(session_fp.0); |
| 146 | + let relative = format_relative_time(*last_connected); |
| 147 | + if let Some(name) = name { |
| 148 | + println!(" {name} {hex} (last used: {relative})"); |
| 149 | + } else { |
| 150 | + println!(" {hex} (last used: {relative})"); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
57 | 154 | } |
| 155 | + |
| 156 | + Ok(()) |
58 | 157 | } |
0 commit comments