Skip to content

Commit afe9224

Browse files
committed
imp(cli): initial implementation for executor wrapper
1 parent 97215e3 commit afe9224

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pyroscope_cli/core/src/executor.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use duct::cmd;
2+
use utils::error::{Error, Result};
3+
4+
pub struct Executor<'a> {
5+
cmd: &'a str,
6+
args: &'a str,
7+
handle: Option<duct::Handle>,
8+
}
9+
10+
impl<'a> Executor<'a> {
11+
pub fn new(cmd: &'a str, args: &'a str) -> Executor<'a> {
12+
Executor {
13+
cmd,
14+
args,
15+
handle: None,
16+
}
17+
}
18+
19+
pub fn run(self) -> Result<Self> {
20+
let handle = cmd!(self.cmd, self.args).start()?;
21+
22+
Ok(Self {
23+
cmd: self.cmd,
24+
args: self.args,
25+
handle: Some(handle),
26+
})
27+
}
28+
29+
pub fn stop(self) -> Result<()> {
30+
self.handle
31+
.ok_or_else(|| Error::new("handle is not initialized"))?
32+
.kill()?;
33+
34+
Ok(())
35+
}
36+
37+
pub fn get_pid(&self) -> Result<i32> {
38+
let pid = self
39+
.handle
40+
.as_ref()
41+
.ok_or_else(|| Error::new("handle is not initialized"))?
42+
.pids()
43+
.get(0)
44+
.ok_or_else(|| Error::new("pid not collected"))?
45+
.to_owned() as i32;
46+
47+
Ok(pid)
48+
}
49+
}

0 commit comments

Comments
 (0)