Skip to main content

command_stream/execa/
options.rs

1use std::collections::HashMap;
2use std::ffi::OsString;
3use std::path::PathBuf;
4use std::time::Duration;
5use tokio::sync::watch;
6
7/// Portable counterparts of Execa process options.
8#[derive(Debug, Clone)]
9pub struct Options {
10    pub cwd: Option<PathBuf>,
11    pub env: HashMap<String, String>,
12    pub extend_env: bool,
13    pub input: Option<Vec<u8>>,
14    pub reject: bool,
15    pub strip_final_newline: bool,
16    pub all: bool,
17    pub buffer: bool,
18    pub max_buffer: usize,
19    pub timeout: Option<Duration>,
20    pub cancel_signal: Option<CancelSignal>,
21    pub kill_signal: String,
22    pub prefer_local: crate::PreferLocal,
23    pub node_exec_path: OsString,
24    pub node_options: Vec<OsString>,
25}
26
27impl Default for Options {
28    fn default() -> Self {
29        Self {
30            cwd: None,
31            env: HashMap::new(),
32            extend_env: true,
33            input: None,
34            reject: true,
35            strip_final_newline: true,
36            all: false,
37            buffer: true,
38            max_buffer: 100_000_000,
39            timeout: None,
40            cancel_signal: None,
41            kill_signal: "SIGTERM".into(),
42            prefer_local: crate::PreferLocal::Off,
43            node_exec_path: "node".into(),
44            node_options: Vec::new(),
45        }
46    }
47}
48
49/// A clonable cancellation controller. Dropping it does not cancel the child.
50#[derive(Debug, Clone)]
51pub struct Cancellation(watch::Sender<bool>);
52
53impl Default for Cancellation {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl Cancellation {
60    pub fn new() -> Self {
61        Self(watch::channel(false).0)
62    }
63
64    pub fn signal(&self) -> CancelSignal {
65        CancelSignal(self.0.subscribe())
66    }
67
68    pub fn cancel(&self) {
69        self.0.send_replace(true);
70    }
71}
72
73/// Cancellation state supplied to a command.
74#[derive(Debug, Clone)]
75pub struct CancelSignal(pub(crate) watch::Receiver<bool>);