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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use duct::{cmd, Expression, ReaderHandle};
use std::ffi::OsString;
use std::io::{BufRead, BufReader};

use std::io;

/// Various events associated with process's life-cycle
///
#[derive(Debug)]
pub enum ProcessEvent {
    /// Default value placeholder
    _Unknown,
    /// Process is starting but not yet started!
    Starting,
    /// Process is started
    Started,
    /// Error occured while starting the process itself
    StartError,
    /// Process started but error occured during reading the output data
    IOError,
    /// Process started and output data reader reached to the EOF, means process's output data is unavailable
    IOEof,
    /// Process started and a line from the output data is available now
    IOData,
    /// Process started and during IOData reading based on the API consumer's decision the callback returned [`Some(false)`] ,
    /// which means process's exit request is submitted
    ExitRequested,
    /// Kill API was used to kill the process
    KillRequested,
    /// Process which was started earlier now exited
    Exited,
    /// A error occured while killing/stopping the process
    KillError,
}

/// Various fields related to the process
///
pub struct ProcessData<'a> {
    /// A unique request-id / session-id
    pub request_id: u32,
    /// List of child pids
    pub pids: Vec<u32>,
    /// Process's STDOUT & STDERR output's line number
    pub line_number: i64,
    /// Process's STDOUT & STDERR output's line
    pub line: String,
    /// Internal reader handle for managing the process
    reader: Option<&'a ReaderHandle>,
    /// Callback to register for the process events
    callback: Option<&'a dyn Fn(&ProcessEvent, &ProcessData) -> Option<bool>>,
}

impl ProcessData<'_> {
    /// Create a new instance of the [`ProcessData`]
    ///
    pub fn new() -> Self {
        Self {
            request_id: 0,
            pids: vec![],
            line_number: 0,
            line: String::new(),
            reader: None,
            callback: None,
        }
    }
    /// Kill the running process
    ///
    pub fn kill(&self) -> io::Result<()> {
        Ok(if self.reader.is_some() {
            if self.callback.is_some() {
                self.callback.unwrap()(&ProcessEvent::KillRequested, &self);
            }
            return self.reader.as_ref().unwrap().kill();
        })
    }

    /// Get the list of child pids
    pub fn child_pids(&mut self) -> &Vec<u32> {
        if self.reader.is_some() {
            self.pids = self.reader.as_ref().unwrap().pids();
        }
        self.pids.as_ref()
    }
}

/// check if the callback is registed and if yes then trigger it wi the supplied data
fn check_and_trigger_callback(
    callback: &Option<&dyn Fn(&ProcessEvent, &ProcessData) -> Option<bool>>,
    event: &ProcessEvent,
    data: &ProcessData,
) -> Option<bool> {
    if callback.is_some() {
        return callback.unwrap()(event, data);
    };
    Some(true)
}

/**
Run a process based on the provided arguments.
Generates various events [`ProcessEvent`] according to the process's life-cycle, process's information and data [`ProcessData`] associated with that event

# Arguments
request_id : [`u32`] // custom unique numeric id to relate the various callbacks for a particular process execution session

use_shell : [`bool`] // use shell mode or direct executable path based execution

cmd_line : [`Vec<Vec<String>>`] // Vector of commandline alog with arguments. For a single command line one vector element is enough,
for the pipe lines use case output of one to provide to the next use Vector of Commandlines.

callback : [`Option<&dyn Fn(&ProcessEvent, &ProcessData) -> Option<bool>>`] // register callback to get various events and process output, for no callbacks use None


# Examples

```
// setup callback for the process events and data streaming

let callback = |status: &ProcessEvent, data: &ProcessData| -> Option<bool> {
            match status {
                ProcessEvent::Started => {
                    println!(
                        "Event {:?} | req-id {}  | Pids: {:?}",
                        status, data.request_id, data.pids
                    );
                }
                ProcessEvent::IOData => {
                    println!(
                        "Event {:?} | req-id {} | # {} : {}",
                        status, data.request_id, data.line_number, data.line
                    );
                    //demo how to kill/stop
                    //_ = data.kill();
                    // //or return false if line_number check, to exit the process
                    // if data.line_number == 1 {
                    //     return Some(false);
                    // }
                    //
                    // // or return false if a condition is true based on the output, to exit the process
                    // if data.line.contains("Sandy") {
                    //     return Some(false);
                    // }
                }

                other => {
                    if !data.line.is_empty() {
                        println!(
                            "Event {:?} | req-id {} | addational detail(s): {}",
                            other, data.request_id, data.line
                        );
                    } else {
                        println!("Event {:?} | req-id {}", other, data.request_id);
                    }
                }
            }
            Some(true)
 };
 ```
 ```
 //using shell mode, prints hi and waits for 3 seconds. Events are sent to provided callback

 run_process(
     102,
     true,
     vec![vec![
         String::from("echo"),
         String::from("hi"),
         String::from("&"),
         String::from("timeout"),
         String::from("/t"),
         String::from("3"),
     ]],
     Some(&callback)
 );

 //using cmd mode, starts calculator application in windows
 run_process(101, false, vec![vec![String::from("calc")]], None);

 //running process using a thread
 _ = thread::spawn(move || {
            run_process(101, false, vec![vec![String::from("calc")]], None);
    });


```
*/
pub fn run_process(
    request_id: u32,
    use_shell: bool,
    cmd_line: Vec<Vec<String>>,
    callback: Option<&dyn Fn(&ProcessEvent, &ProcessData) -> Option<bool>>,
) {
    let mut process_data = ProcessData::new();
    process_data.line.clear();
    process_data.request_id = request_id;
    process_data.callback = callback;
    if cmd_line.len() == 0 || cmd_line[0].len() == 0 {
        process_data
            .line
            .push_str(format!("{:?}", "Commandline - arguments are unavailable!").as_str());
        check_and_trigger_callback(&callback, &ProcessEvent::StartError, &process_data);
        return;
    }

    check_and_trigger_callback(&callback, &ProcessEvent::Starting, &process_data);

    let handle = handle_pipeline(cmd_line, use_shell);
    let reader = &handle.stderr_to_stdout().reader();

    match reader {
        Ok(reader) => {
            process_data.reader = Some(reader);
            process_data.pids = reader.pids();
            check_and_trigger_callback(&callback, &ProcessEvent::Started, &process_data);
            let mut buffer_reader = BufReader::new(reader);
            loop {
                process_data.line.clear();
                let result = buffer_reader.read_line(&mut process_data.line);
                match result {
                    Ok(result) if result == 0 => {
                        check_and_trigger_callback(&callback, &ProcessEvent::IOEof, &process_data);
                        break;
                    }
                    Ok(_result) => {
                        process_data.line_number += 1;
                        match check_and_trigger_callback(
                            &callback,
                            &ProcessEvent::IOData,
                            &process_data,
                        ) {
                            Some(value) if value == false => {
                                check_and_trigger_callback(
                                    &callback,
                                    &ProcessEvent::ExitRequested,
                                    &process_data,
                                );
                                break;
                            }
                            _other => {}
                        }
                    }
                    Err(error) => {
                        process_data.line.push_str(format!("{:?}", error).as_str());
                        check_and_trigger_callback(
                            &callback,
                            &ProcessEvent::IOError,
                            &process_data,
                        );
                        break;
                    }
                }
            }
            process_data.line.clear();
            let exit_result = reader.kill();

            match exit_result {
                Ok(_) => {
                    check_and_trigger_callback(&callback, &ProcessEvent::Exited, &process_data);
                }
                Err(_) => {
                    check_and_trigger_callback(&callback, &ProcessEvent::KillError, &process_data);
                }
            }
        }
        _error => {
            let reader = reader.as_ref();
            if reader.err().is_some() {
                process_data
                    .line
                    .push_str(format!("{:?}", reader.err().unwrap()).as_str());
            }
            check_and_trigger_callback(&callback, &ProcessEvent::StartError, &process_data);
        }
    }
    process_data.callback = None;
    process_data.reader = None;
}

/// handle pipeline based multiple commandlines
fn handle_pipeline(cmd_line: Vec<Vec<String>>, use_shell: bool) -> Expression {
    let mut cmd_pipeline;
    if use_shell {
        cmd_pipeline = sh_vector(&cmd_line[0]);
    } else {
        let cli = vec_string_to_osstring(&cmd_line[0]);
        cmd_pipeline = cmd(&cli[0], &cli[1..]);
    }
    if cmd_line.len() > 1 {
        let mut cmd_itr = cmd_line.iter();
        cmd_itr.next();
        for command in cmd_itr {
            if use_shell {
                cmd_pipeline = cmd_pipeline.pipe(sh_vector(&command));
            } else {
                let cli = vec_string_to_osstring(&command);
                cmd_pipeline = cmd_pipeline.pipe(cmd(&cli[0], &cli[1..]));
            }
        }
    }
    cmd_pipeline
}

/// create and run a shell based command, using vector of cmd and arguments
fn sh_vector(command: &Vec<String>) -> Expression {
    let argv = shell_command_argv_vector(command.into());
    cmd(&argv[0], &argv[1..])
}

/// create a shell based command
#[cfg(unix)]
fn shell_command_argv_vector(command: &Vec<String>) -> Vec<OsString> {
    let mut cli: Vec<OsString> = vec_string_to_osstring(command);
    let mut full_args = vec!["/bin/sh".into(), "-c".into()];
    full_args.append(&mut cli);
    full_args
}

/// Prepare shell based command
#[cfg(windows)]
fn shell_command_argv_vector(command: &Vec<String>) -> Vec<OsString> {
    let comspec = std::env::var_os("COMSPEC").unwrap_or_else(|| "cmd.exe".into());
    let mut cli: Vec<OsString> = vec_string_to_osstring(command);
    let mut full_args = vec![comspec, "/C".into()];
    full_args.append(&mut cli);
    full_args
}

/// convert vector of [`String`] to vector of [`OsString`]
fn vec_string_to_osstring(input: &Vec<String>) -> Vec<OsString> {
    input.iter().map(|x| x.as_str().into()).collect()
}

#[cfg(test)]
mod tests {
    use crate::{run_process, ProcessData, ProcessEvent};
    use std::{thread, time::Duration};

    #[test]
    pub fn test_using_sh_output_streaming() {
        let callback = |status: &ProcessEvent, data: &ProcessData| -> Option<bool> {
            match status {
                ProcessEvent::Started => {
                    println!(
                        "Event {:?} | req-id {}  | Pids: {:?}",
                        status, data.request_id, data.pids
                    );
                }
                ProcessEvent::IOData => {
                    println!(
                        "Event {:?} | req-id {} | # {} : {}",
                        status, data.request_id, data.line_number, data.line
                    );
                    //demo how to kill/stop
                    //_ = data.kill();
                    // //or return false if line_number check, to exit the process
                    // if data.line_number == 1 {
                    //     return Some(false);
                    // }
                    // // or return false if a condition is true based on the output, to exit the process
                    // if data.line.contains("Sandy") {
                    //     return Some(false);
                    // }
                }

                other => {
                    if !data.line.is_empty() {
                        println!(
                            "Event {:?} | req-id {} | addational detail(s): {}",
                            other, data.request_id, data.line
                        );
                    } else {
                        println!("Event {:?} | req-id {}", other, data.request_id);
                    }
                }
            }
            Some(true)
        };

        _ = thread::spawn(move || {
            thread::sleep(Duration::from_millis(500));
            println!(
                "test_using_sh_output_streaming in a thread, no arguments means error {:?}",
                run_process(104, true, vec![vec![]], Some(&callback))
            );
        });

        println!(
            "test_using_sh_output_streaming pipe-line {:?}",
            //current dir listing and sort the piped output
            run_process(
                105,
                true,
                vec![vec![String::from("dir")], vec![String::from("sort"),]],
                Some(&callback)
            )
        );

        println!(
            "test_using_sh_output_streaming double quotes {:?}",
            //current dir listing and sort the piped output
            run_process(
                106,
                true,
                vec![vec![String::from(r#"echo "Sandy" "#),]],
                Some(&callback)
            )
        );

        println!(
            "test_using_sh_output_streaming no arguments means error {:?}",
            run_process(107, true, vec![vec![]], Some(&callback))
        );
    }

    #[test]
    pub fn test_using_cmd_output_streaming() {
        println!(
            "test_using_cmd_output_streaming {:?}",
            run_process(108, false, vec![vec![String::from("calc")]], None)
        );
    }
}