-
Notifications
You must be signed in to change notification settings - Fork 1
/
logs.rs
47 lines (42 loc) · 1.36 KB
/
logs.rs
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
use anyhow::{bail, Result};
use clap::Parser;
use std::process::Command;
use tonic::transport::Channel;
use wacker_api::config::LOGS_DIR;
#[derive(Parser)]
pub struct LogsCommand {
/// Module ID
#[arg(required = true)]
id: String,
/// Follow log output
#[arg(short, long)]
follow: bool,
/// Number of lines to show from the end of the logs
#[arg(short = 'n', long, value_name = "n")]
tail: Option<u32>,
}
impl LogsCommand {
/// Executes the command.
pub async fn execute(self, _: Channel) -> Result<()> {
let home_dir = dirs::home_dir().expect("Can't get home dir");
let path = home_dir.join(LOGS_DIR).join(self.id);
let mut tail_args = vec![];
if self.follow {
tail_args.push("-f".to_string());
}
if self.tail.is_some() {
tail_args.push(format!("-n {}", self.tail.unwrap()));
}
tail_args.push(path.display().to_string());
match Command::new("tail").args(tail_args).spawn() {
Ok(mut child) => {
let status = child.wait().expect("Failed to wait for child process");
if !status.success() {
bail!("tail command failed with: {:?}", status);
}
Ok(())
}
Err(err) => bail!("Error executing tail command: {}", err),
}
}
}