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 = ctx.servers.read().unwrap();
40
41        if servers.is_empty() {
42            return format!(
43                "Keine Server erstellt. Verwende 'create'\nRegistry: {}",
44                registry.get_file_path().display() // FIX: get_file_path() verwenden
45            );
46        }
47
48        let mut result = String::from("Server Liste (Persistent):\n");
49        let mut server_list: Vec<_> = servers.values().collect();
50        server_list.sort_by(|a, b| a.created_timestamp.cmp(&b.created_timestamp));
51
52        for (i, server) in server_list.iter().enumerate() {
53            let status_icon = match server.status {
54                ServerStatus::Running => "Running",
55                ServerStatus::Stopped => "Stopped",
56                ServerStatus::Failed => "Failed",
57            };
58
59            result.push_str(&format!(
60                "  {}. {} - {} (Port: {}) [{}]\n",
61                i + 1,
62                server.name,
63                &server.id[0..8],
64                server.port,
65                status_icon
66            ));
67        }
68
69        result.push_str(&format!(
70            "\nRegistry: {}",
71            registry.get_file_path().display() // FIX: get_file_path() verwenden
72        ));
73
74        result
75    }
76}