memtui 0.3.0

An interactive TUI (Terminal User Interface) for browsing and inspecting key-value stores (Redis, Memcached, etc.)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use super::{
    Backend, BackendCapabilities, BackendError, ConnectionInfo, RawCommandResult, ServerInfo,
};
use crate::types::{
    Auth, BackendType, ConnectionConfig, KeyMetadata, KeyScanResult, Value, ValueType,
};
use etcd_client::{Client, GetOptions, KeyValue};
use std::time::{Duration, SystemTime};

/// etcd backend implementation
pub struct EtcdBackend {
    config: ConnectionConfig,
    client: Option<Client>,
}

impl EtcdBackend {
    pub fn new(config: ConnectionConfig) -> Self {
        Self {
            config,
            client: None,
        }
    }

    /// Build etcd endpoint URLs from config
    fn build_endpoints(&self) -> Vec<String> {
        let scheme = if self.config.tls.as_ref().map(|t| t.enabled).unwrap_or(false) {
            "https"
        } else {
            "http"
        };

        vec![format!(
            "{}://{}:{}",
            scheme, self.config.host, self.config.port
        )]
    }

    /// Check if we have a client (without needing mutable access)
    fn has_client(&self) -> bool {
        self.client.is_some()
    }

    /// Detect the value type from raw bytes
    fn detect_value_type(data: &[u8]) -> ValueType {
        if super::utils::is_json(data) {
            ValueType::Json
        } else if data.iter().all(|&b| b.is_ascii()) {
            ValueType::String
        } else {
            ValueType::Binary
        }
    }

    /// Convert etcd KeyValue to KeyMetadata
    fn kv_to_metadata(kv: &KeyValue) -> KeyMetadata {
        let name = String::from_utf8_lossy(kv.key()).to_string();
        let data = kv.value();
        let value_type = Self::detect_value_type(data);

        KeyMetadata {
            name,
            value_type,
            size_bytes: data.len() as u64,
            ttl: None, // etcd uses leases for TTL, would need extra call
            last_accessed: Some(SystemTime::now()),
            encoding: Some("utf8".to_string()),
        }
    }
}

#[async_trait::async_trait]
impl Backend for EtcdBackend {
    fn backend_type(&self) -> BackendType {
        BackendType::Etcd
    }

    fn capabilities(&self) -> BackendCapabilities {
        BackendCapabilities {
            supports_ttl: true,                      // Via leases
            supports_scan: true,                     // Via prefix range queries
            supports_raw_commands: false,            // No raw command support
            supports_batch_get: true,                // Can get ranges
            supports_efficient_pattern_search: true, // Prefix-based
        }
    }

    async fn connect(&mut self) -> Result<ConnectionInfo, BackendError> {
        let endpoints = self.build_endpoints();

        // Build connection options
        let mut options = etcd_client::ConnectOptions::new();

        // Set timeout
        options = options.with_connect_timeout(self.config.timeout);

        // Handle authentication
        if let Some(auth) = &self.config.auth {
            match auth {
                Auth::UserPassword { username, password } => {
                    options = options.with_user(username, password);
                }
                Auth::Token(_token) => {
                    // etcd-client doesn't directly support token auth in connect options
                    // Token would typically be used for gRPC metadata
                    return Err(BackendError::AuthenticationError(
                        "Token auth not directly supported; use username/password".to_string(),
                    ));
                }
                Auth::Certificate { .. } => {
                    // TLS client certificates would require additional TLS config
                    return Err(BackendError::AuthenticationError(
                        "Certificate auth requires TLS configuration".to_string(),
                    ));
                }
            }
        }

        // Connect with timeout
        let client = tokio::time::timeout(
            self.config.timeout,
            Client::connect(endpoints.clone(), Some(options)),
        )
        .await
        .map_err(|_| BackendError::Timeout)?
        .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        self.client = Some(client);

        // Get cluster version via status
        let version = match self.client.as_mut() {
            Some(client) => match client.status().await {
                Ok(resp) => resp.version().to_string(),
                Err(_) => "unknown".to_string(),
            },
            None => "unknown".to_string(),
        };

        Ok(ConnectionInfo {
            connected: true,
            server_version: version,
            address: format!("{}:{}", self.config.host, self.config.port),
        })
    }

    async fn disconnect(&mut self) -> Result<(), BackendError> {
        self.client = None;
        Ok(())
    }

    async fn ping(&self) -> Result<Duration, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        // For ping, we need to clone the client since status() requires &mut
        // This is a limitation - we'll measure a simple key get instead
        let start = std::time::Instant::now();

        // We can't actually ping without mutable access, so just return quickly
        // The actual connection health is verified during operations
        Ok(start.elapsed())
    }

    async fn info(&self) -> Result<ServerInfo, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        // etcd doesn't provide as rich server info as Redis
        // We'll return what we can gather
        Ok(ServerInfo {
            version: "etcd v3".to_string(),
            uptime: Duration::from_secs(0), // Not easily available
            memory_used: 0,                 // Not easily available
            memory_max: None,
            clients_connected: 1,
            keys_total: 0, // Would need a full scan to count
        })
    }

    async fn scan_keys(
        &self,
        pattern: Option<&str>,
        cursor: Option<String>,
        limit: usize,
    ) -> Result<KeyScanResult, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        // For etcd, pattern is treated as a key prefix
        let prefix = pattern.unwrap_or("");

        // Use cursor as the start key for pagination
        let start_key = cursor.unwrap_or_else(|| prefix.to_string());

        // We need mutable access, so we'll get a clone of endpoints and reconnect
        // This is inefficient but necessary given the trait design
        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        // Get keys with prefix and limit
        let get_options = GetOptions::new()
            .with_prefix()
            .with_limit((limit + 1) as i64) // +1 to check if there are more
            .with_keys_only();

        let resp = client
            .get(start_key.as_bytes(), Some(get_options))
            .await
            .map_err(|e| BackendError::Internal(e.to_string()))?;

        let kvs = resp.kvs();
        let has_more = kvs.len() > limit;
        let take_count = if has_more { limit } else { kvs.len() };

        let mut keys: Vec<KeyMetadata> = Vec::new();
        let mut last_key: Option<String> = None;

        for kv in kvs.iter().take(take_count) {
            let key_str = String::from_utf8_lossy(kv.key()).to_string();
            last_key = Some(key_str.clone());

            // For keys_only query, we don't have value data
            keys.push(KeyMetadata {
                name: key_str,
                value_type: ValueType::Unknown,
                size_bytes: 0,
                ttl: None,
                last_accessed: Some(SystemTime::now()),
                encoding: None,
            });
        }

        // For next cursor, use the last key + null byte to start after it
        let next_cursor = if has_more {
            last_key.map(|k| format!("{}\0", k))
        } else {
            None
        };

        Ok(KeyScanResult {
            keys,
            cursor: next_cursor,
            has_more,
        })
    }

    async fn search_keys(
        &self,
        pattern: &str,
        limit: usize,
    ) -> Result<KeyScanResult, BackendError> {
        // For etcd, search is essentially a prefix query
        // Strip wildcards and use as prefix
        let prefix = pattern.trim_matches('*');
        self.scan_keys(Some(prefix), None, limit).await
    }

    async fn key_count(&self, pattern: Option<&str>) -> Result<u64, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let prefix = pattern.unwrap_or("");

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        let get_options = GetOptions::new().with_prefix().with_count_only();

        let resp = client
            .get(prefix.as_bytes(), Some(get_options))
            .await
            .map_err(|e| BackendError::Internal(e.to_string()))?;

        Ok(resp.count() as u64)
    }

    async fn key_info(&self, key: &str) -> Result<KeyMetadata, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        let resp = client
            .get(key, None)
            .await
            .map_err(|e| BackendError::Internal(e.to_string()))?;

        let kv = resp
            .kvs()
            .first()
            .ok_or_else(|| BackendError::KeyNotFound(key.to_string()))?;

        Ok(Self::kv_to_metadata(kv))
    }

    async fn get(&self, key: &str) -> Result<Value, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        let resp = client
            .get(key, None)
            .await
            .map_err(|e| BackendError::Internal(e.to_string()))?;

        let kv = resp
            .kvs()
            .first()
            .ok_or_else(|| BackendError::KeyNotFound(key.to_string()))?;

        let data = kv.value().to_vec();
        let value_type = Self::detect_value_type(&data);

        Ok(Value {
            data,
            value_type,
            encoding: Some("utf8".to_string()),
        })
    }

    async fn get_many(&self, keys: &[String]) -> Result<Vec<(String, Value)>, BackendError> {
        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        let mut results = Vec::new();

        for key in keys {
            match client.get(key.as_str(), None).await {
                Ok(resp) => {
                    if let Some(kv) = resp.kvs().first() {
                        let data = kv.value().to_vec();
                        let value_type = Self::detect_value_type(&data);

                        results.push((
                            key.clone(),
                            Value {
                                data,
                                value_type,
                                encoding: Some("utf8".to_string()),
                            },
                        ));
                    }
                }
                Err(_) => continue, // Skip keys that don't exist
            }
        }

        Ok(results)
    }

    async fn set(
        &self,
        key: &str,
        value: &[u8],
        ttl: Option<Duration>,
    ) -> Result<(), BackendError> {
        if self.config.read_only {
            return Err(BackendError::ReadOnly);
        }

        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        if let Some(ttl_duration) = ttl {
            // Create a lease for TTL
            let lease_resp = client
                .lease_grant(ttl_duration.as_secs() as i64, None)
                .await
                .map_err(|e| BackendError::Internal(e.to_string()))?;

            let lease_id = lease_resp.id();

            // Put with lease
            let put_options = etcd_client::PutOptions::new().with_lease(lease_id);
            client
                .put(key, value, Some(put_options))
                .await
                .map_err(|e| BackendError::Internal(e.to_string()))?;
        } else {
            client
                .put(key, value, None)
                .await
                .map_err(|e| BackendError::Internal(e.to_string()))?;
        }

        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<bool, BackendError> {
        if self.config.read_only {
            return Err(BackendError::ReadOnly);
        }

        if !self.has_client() {
            return Err(BackendError::ConnectionError("Not connected".to_string()));
        }

        let endpoints = self.build_endpoints();
        let mut options = etcd_client::ConnectOptions::new();
        options = options.with_connect_timeout(self.config.timeout);

        if let Some(Auth::UserPassword { username, password }) = &self.config.auth {
            options = options.with_user(username, password);
        }

        let mut client = Client::connect(endpoints, Some(options))
            .await
            .map_err(|e| BackendError::ConnectionError(e.to_string()))?;

        let resp = client
            .delete(key, None)
            .await
            .map_err(|e| BackendError::Internal(e.to_string()))?;

        Ok(resp.deleted() > 0)
    }

    async fn execute_raw(&self, _command: &str) -> Result<RawCommandResult, BackendError> {
        // etcd doesn't support raw commands in the same way as Redis
        Err(BackendError::NotSupported)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_config() -> ConnectionConfig {
        ConnectionConfig {
            id: "test".to_string(),
            name: "Test etcd".to_string(),
            backend_type: BackendType::Etcd,
            host: "localhost".to_string(),
            port: 2379,
            auth: None,
            database: None,
            tls: None,
            timeout: Duration::from_secs(5),
            read_only: false,
        }
    }

    #[test]
    fn test_build_endpoints_basic() {
        let backend = EtcdBackend::new(create_test_config());
        let endpoints = backend.build_endpoints();
        assert_eq!(endpoints, vec!["http://localhost:2379"]);
    }

    #[test]
    fn test_build_endpoints_with_tls() {
        let mut config = create_test_config();
        config.tls = Some(crate::types::TlsConfig {
            enabled: true,
            ca_cert_path: None,
            verify_hostname: true,
        });
        let backend = EtcdBackend::new(config);
        let endpoints = backend.build_endpoints();
        assert_eq!(endpoints, vec!["https://localhost:2379"]);
    }

    #[test]
    fn test_build_endpoints_custom_port() {
        let mut config = create_test_config();
        config.port = 2380;
        let backend = EtcdBackend::new(config);
        let endpoints = backend.build_endpoints();
        assert_eq!(endpoints, vec!["http://localhost:2380"]);
    }

    #[test]
    fn test_backend_type() {
        let backend = EtcdBackend::new(create_test_config());
        assert_eq!(backend.backend_type(), BackendType::Etcd);
    }

    #[test]
    fn test_capabilities() {
        let backend = EtcdBackend::new(create_test_config());
        let caps = backend.capabilities();
        assert!(caps.supports_ttl);
        assert!(caps.supports_scan);
        assert!(!caps.supports_raw_commands);
        assert!(caps.supports_batch_get);
        assert!(caps.supports_efficient_pattern_search);
    }

    #[test]
    fn test_detect_value_type_json() {
        let json_data = b"{\"key\": \"value\"}";
        assert_eq!(EtcdBackend::detect_value_type(json_data), ValueType::Json);
    }

    #[test]
    fn test_detect_value_type_string() {
        let string_data = b"hello world";
        assert_eq!(
            EtcdBackend::detect_value_type(string_data),
            ValueType::String
        );
    }

    #[test]
    fn test_detect_value_type_binary() {
        let binary_data = &[0x00, 0xFF, 0x80, 0x90];
        assert_eq!(
            EtcdBackend::detect_value_type(binary_data),
            ValueType::Binary
        );
    }

    #[test]
    fn test_has_client_not_connected() {
        let backend = EtcdBackend::new(create_test_config());
        assert!(!backend.has_client());
    }
}