-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathrun.rs
More file actions
83 lines (72 loc) · 1.8 KB
/
run.rs
File metadata and controls
83 lines (72 loc) · 1.8 KB
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
use std::{panic::AssertUnwindSafe, time::SystemTime};
use futures::FutureExt;
use ruma::UserId;
use tracing::{Instrument, Level, span};
use tuwunel_core::{Error, debug, error};
use super::{CommandResult, CommandSystem, Service};
impl Service {
pub async fn run_command(
&self,
command_system: &dyn CommandSystem,
line: &str,
input: &str,
sender: Option<&UserId>,
) -> CommandResult {
let timer = SystemTime::now();
let span = span!(Level::INFO, "command");
let span_id = span.id();
// start capture if the span is enabled
if let Some(ref span_id) = span_id {
self.services
.server
.log
.capture
.start_capture(span_id);
}
let args = command_system.parse(line);
let args = args
.iter()
.map(String::as_str)
.collect::<Vec<&str>>();
let result = AssertUnwindSafe(Box::pin(
command_system
.process(&args, input, sender)
.instrument(span),
))
.catch_unwind()
.await
.map(|result| result.map_err(|error| format!("Command failed: \n{error}")))
.unwrap_or_else(|panic| {
let error = Error::from_panic(panic);
error!("Panic while processing command: {error:?}");
Err(format!(
"Panic occurred while processing command:\n\
```\n\
{error:#?}\n\
```\n\
Please submit a [bug report](https://github.com/matrix-construct/tuwunel/issues/new).🥺"
))
});
let (output, err) = match result {
| Ok(output) => (output, false),
| Err(output) => (output, true),
};
// return logs if span is active, otherwise empty
let logs = if let Some(ref span_id) = span_id {
self.services
.server
.log
.capture
.stop_capture(span_id)
} else {
Vec::new()
};
debug!(
ok = !err,
elapsed = ?timer.elapsed(),
command = ?args,
"command processed"
);
CommandResult { output, logs, err }
}
}