Skip to main content

rush_sync_server/commands/list/
command.rs

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