File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments