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