rush_sync_server/commands/list/
command.rs

1use crate::commands::command::Command;
2use crate::core::prelude::*;
3use crate::server::types::{ServerContext, ServerStatus};
4use std::future::Future;
5use std::pin::Pin;
6
7#[derive(Debug, Default)]
8pub struct ListCommand;
9
10impl ListCommand {
11    pub fn new() -> Self {
12        Self
13    }
14}
15
16impl Command for ListCommand {
17    fn name(&self) -> &'static str {
18        "list"
19    }
20    fn description(&self) -> &'static str {
21        "List all web servers (persistent)"
22    }
23    fn matches(&self, command: &str) -> bool {
24        let cmd = command.trim().to_lowercase();
25        cmd == "list" || cmd == "list servers" || cmd == "list server"
26    }
27
28    fn execute_sync(&self, _args: &[&str]) -> Result<String> {
29        let ctx = crate::server::shared::get_shared_context();
30        Ok(self.list_servers(ctx))
31    }
32
33    fn execute_async<'a>(
34        &'a self,
35        _args: &'a [&'a str],
36    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>> {
37        Box::pin(async move { self.execute_sync(_args) })
38    }
39
40    fn supports_async(&self) -> bool {
41        true
42    }
43    fn priority(&self) -> u8 {
44        60
45    }
46}
47
48impl ListCommand {
49    fn list_servers(&self, ctx: &ServerContext) -> String {
50        let registry = crate::server::shared::get_persistent_registry();
51        let servers = ctx.servers.read().unwrap();
52
53        if servers.is_empty() {
54            return format!(
55                "Keine Server erstellt. Verwende 'create'\nRegistry: {}",
56                registry.get_file_path().display() // FIX: get_file_path() verwenden
57            );
58        }
59
60        let mut result = String::from("Server Liste (Persistent):\n");
61        let mut server_list: Vec<_> = servers.values().collect();
62        server_list.sort_by(|a, b| a.created_timestamp.cmp(&b.created_timestamp));
63
64        for (i, server) in server_list.iter().enumerate() {
65            let status_icon = match server.status {
66                ServerStatus::Running => "Running",
67                ServerStatus::Stopped => "Stopped",
68                ServerStatus::Failed => "Failed",
69            };
70
71            result.push_str(&format!(
72                "  {}. {} - {} (Port: {}) [{}]\n",
73                i + 1,
74                server.name,
75                &server.id[0..8],
76                server.port,
77                status_icon
78            ));
79        }
80
81        result.push_str(&format!(
82            "\nRegistry: {}",
83            registry.get_file_path().display() // FIX: get_file_path() verwenden
84        ));
85
86        result
87    }
88}