konfigkoll_script 0.1.16

Scripting language for Konfigkoll (not for direct public use)
Documentation
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright:
//
// This is based on the process module from rune-rs which is dual licensed under
// the MIT and Apache 2.0 licenses. See
// https://github.com/rune-rs/rune/blob/0.13.x/crates/rune-modules/src/process.rs
// for the original source code.
//
// Parts of the documentation is also copied from tokio and the Rust standard
// library. These are *also* dual licensed under the MIT and Apache 2.0
// licenses. See:
// * https://docs.rs/tokio/1.39.1/tokio/process/index.html
// * https://doc.rust-lang.org/stable/std/process/index.html
// (The docs were not copied from the source but from the generated
// documentation)

//! The native `process` module for the [Rune Language].
//!
//! [Rune Language]: https://rune-rs.github.io
//!
//! ## Usage
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! rune-modules = { version = "0.13.3", features = ["process"] }
//! ```
//!
//! Install it into your context:
//!
//! ```rust
//! let mut context = rune::Context::with_default_modules()?;
//! context.install(rune_modules::process::module(true)?)?;
//! # Ok::<_, rune::support::Error>(())
//! ```
//!
//! Use it in Rune:
//!
//! ```rust,ignore
//! use process::Command;
//!
//! fn main() {
//!     let command = Command::new("ls");
//!     command.run().await;
//! }
//! ```

use rune::Any;
use rune::ContextError;
use rune::Module;
use rune::alloc::Vec;
use rune::alloc::fmt::TryWrite;
use rune::runtime::Bytes;
use rune::runtime::Formatter;
use rune::runtime::Mut;
use rune::runtime::Shared;
use rune::runtime::Value;
use rune::runtime::VmResult;
use rune::vm_try;
use rune::vm_write;
use std::io;
use tokio::process;
use tracing::instrument;

/// A module for working with processes.
///
/// This allows spawning child processes, capturing their output, and creating
/// pipelines.
#[rune::module(::process)]
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
    let mut module = Module::from_meta(module_meta)?;
    module.ty::<Command>()?;
    module.ty::<Child>()?;
    module.ty::<ExitStatus>()?;
    module.ty::<Output>()?;
    module.ty::<Stdio>()?;
    module.ty::<ChildStdin>()?;
    module.ty::<ChildStdout>()?;
    module.ty::<ChildStderr>()?;

    module.function_meta(Command::string_debug)?;
    module.function_meta(Command::new)?;
    module.function_meta(Command::spawn)?;
    module.function_meta(Command::arg)?;
    module.function_meta(Command::args)?;
    #[cfg(unix)]
    module.function_meta(Command::arg0)?;
    module.function_meta(Command::stdin)?;
    module.function_meta(Command::stdout)?;
    module.function_meta(Command::stderr)?;

    module.function_meta(Child::string_debug)?;
    module.function_meta(Child::stdin)?;
    module.function_meta(Child::stdout)?;
    module.function_meta(Child::stderr)?;
    module.function_meta(Child::id)?;
    module.function_meta(Child::start_kill)?;
    module.function_meta(Child::kill)?;
    module.function_meta(Child::wait)?;
    module.function_meta(Child::wait_with_output)?;

    module.function_meta(ExitStatus::string_debug)?;
    module.function_meta(ExitStatus::string_display)?;
    module.function_meta(ExitStatus::code)?;
    module.function_meta(ExitStatus::success)?;

    module.function_meta(Output::string_debug)?;
    module.function_meta(Stdio::null)?;
    module.function_meta(Stdio::inherit)?;
    module.function_meta(Stdio::piped)?;

    module.function_meta(ChildStdin::string_debug)?;
    module.function_meta(ChildStdin::try_into_stdio)?;

    module.function_meta(ChildStdout::string_debug)?;
    module.function_meta(ChildStdout::try_into_stdio)?;

    module.function_meta(ChildStderr::string_debug)?;
    module.function_meta(ChildStderr::try_into_stdio)?;

    Ok(module)
}

/// A builder for a child command to execute
#[derive(Debug, Any)]
#[rune(item = ::process)]
struct Command {
    inner: process::Command,
}

impl Command {
    #[rune::function(vm_result, protocol = STRING_DEBUG)]
    fn string_debug(&self, f: &mut Formatter) {
        vm_write!(f, "{:?}", self);
    }

    /// Construct a new command.
    #[rune::function(path = Self::new)]
    fn new(command: &str) -> Self {
        Self {
            inner: process::Command::new(command),
        }
    }

    /// Add arguments.
    #[rune::function(instance)]
    fn args(&mut self, args: &[Value]) -> VmResult<()> {
        for arg in args {
            match arg {
                Value::String(s) => {
                    self.inner.arg(&*vm_try!(s.borrow_ref()));
                }
                actual => {
                    return VmResult::expected::<String>(vm_try!(actual.type_info()));
                }
            }
        }

        VmResult::Ok(())
    }

    /// Add an argument.
    #[rune::function(instance)]
    fn arg(&mut self, arg: &str) {
        self.inner.arg(arg);
    }

    #[cfg(unix)]
    #[rune::function(instance)]
    /// Set the first process argument, argv[0], to something other than the
    /// default executable path. (Unix only)
    fn arg0(&mut self, arg: &str) {
        self.inner.arg0(arg);
    }

    /// Sets configuration for the child process’s standard input (stdin)
    /// handle.
    #[rune::function(instance)]
    fn stdin(&mut self, stdio: Stdio) {
        self.inner.stdin(stdio.inner);
    }

    /// Sets configuration for the child process’s standard output (stdout)
    /// handle.
    #[rune::function(instance)]
    fn stdout(&mut self, stdio: Stdio) {
        self.inner.stdout(stdio.inner);
    }

    /// Sets configuration for the child process’s standard error (stderr)
    /// handle.
    #[rune::function(instance)]
    fn stderr(&mut self, stdio: Stdio) {
        self.inner.stderr(stdio.inner);
    }

    /// Spawn the command.
    #[rune::function(instance)]
    fn spawn(mut self) -> io::Result<Child> {
        Ok(Child {
            inner: Some(self.inner.spawn()?),
        })
    }
}

/// A running child process
#[derive(Debug, Any)]
#[rune(item = ::process)]
struct Child {
    // we use an option to avoid a panic if we try to complete the child process
    // multiple times.
    //
    // TODO: encapsulate this pattern in some better way.
    inner: Option<process::Child>,
}

impl Child {
    #[rune::function(vm_result, protocol = STRING_DEBUG)]
    fn string_debug(&self, f: &mut Formatter) {
        vm_write!(f, "{:?}", self);
    }

    /// Attempt to take the stdin of the child process.
    ///
    /// Once taken this can not be taken again.
    #[rune::function(instance)]
    fn stdin(&mut self) -> Option<ChildStdin> {
        let Some(inner) = &mut self.inner else {
            return None;
        };
        let stdin = inner.stdin.take()?;
        Some(ChildStdin { inner: stdin })
    }

    /// Attempt to take the stdout of the child process.
    ///
    /// Once taken this can not be taken again.
    #[rune::function(instance)]
    fn stdout(&mut self) -> Option<ChildStdout> {
        let Some(inner) = &mut self.inner else {
            return None;
        };
        let stdout = inner.stdout.take()?;
        Some(ChildStdout { inner: stdout })
    }

    /// Attempt to take the stderr of the child process.
    ///
    /// Once taken this can not be taken again.
    #[rune::function(instance)]
    fn stderr(&mut self) -> Option<ChildStderr> {
        let Some(inner) = &mut self.inner else {
            return None;
        };
        let stderr = inner.stderr.take()?;
        Some(ChildStderr { inner: stderr })
    }

    /// Attempt to get the OS process id of the child process.
    ///
    /// This will return None after the child process has completed.
    #[rune::function(instance)]
    fn id(&self) -> Option<u32> {
        match &self.inner {
            Some(inner) => inner.id(),
            None => None,
        }
    }

    #[rune::function(vm_result, instance)]
    fn start_kill(&mut self) -> io::Result<()> {
        let Some(inner) = &mut self.inner else {
            rune::vm_panic!("already completed");
        };

        inner.start_kill()
    }

    /// Sends a signal to the child process.
    #[rune::function(vm_result, instance, path = Self::kill)]
    #[instrument(level = "info", skip_all)]
    async fn kill(mut this: Mut<Self>) -> io::Result<()> {
        let Some(inner) = &mut this.inner else {
            rune::vm_panic!("already completed");
        };

        inner.kill().await
    }

    /// Attempt to wait for the child process to exit.
    ///
    /// This will not capture output, use [`wait_with_output`] for that.
    #[rune::function(vm_result, instance)]
    #[instrument(level = "info", skip_all)]
    async fn wait(self) -> io::Result<ExitStatus> {
        let Some(mut inner) = self.inner else {
            rune::vm_panic!("already completed");
        };

        let status = inner.wait().await?;

        Ok(ExitStatus { status })
    }

    // Returns a future that will resolve to an Output, containing the exit
    // status, stdout, and stderr of the child process.
    #[rune::function(vm_result, instance)]
    #[instrument(level = "info", skip_all)]
    async fn wait_with_output(self) -> io::Result<Output> {
        let Some(inner) = self.inner else {
            rune::vm_panic!("already completed");
        };

        let output = inner.wait_with_output().await?;

        Ok(Output {
            status: ExitStatus {
                status: output.status,
            },
            stdout: Shared::new(Bytes::from_vec(Vec::try_from(output.stdout).vm?)).vm?,
            stderr: Shared::new(Bytes::from_vec(Vec::try_from(output.stderr).vm?)).vm?,
        })
    }
}

/// The output and exit status, returned by [`Child::wait_with_output`].
#[derive(Debug, Any)]
#[rune(item = ::process)]
struct Output {
    #[rune(get)]
    status: ExitStatus,
    #[rune(get)]
    stdout: Shared<Bytes>,
    #[rune(get)]
    stderr: Shared<Bytes>,
}

impl Output {
    #[rune::function(vm_result, protocol = STRING_DEBUG)]
    fn string_debug(&self, f: &mut Formatter) {
        vm_write!(f, "{:?}", self);
    }
}

/// The exit status from a completed child process
#[derive(Debug, Clone, Copy, Any)]
#[rune(item = ::process)]
struct ExitStatus {
    status: std::process::ExitStatus,
}

impl ExitStatus {
    #[rune::function(vm_result, protocol = STRING_DISPLAY)]
    fn string_display(&self, f: &mut Formatter) {
        vm_write!(f, "{}", self.status);
    }

    #[rune::function(vm_result, protocol = STRING_DEBUG)]
    fn string_debug(&self, f: &mut Formatter) {
        vm_write!(f, "{:?}", self);
    }

    #[rune::function]
    fn success(&self) -> bool {
        self.status.success()
    }

    #[rune::function]
    fn code(&self) -> Option<i32> {
        self.status.code()
    }
}

/// Describes what to do with a standard I/O stream for a child process when
/// passed to the stdin, stdout, and stderr methods of Command.
#[derive(Debug, Any)]
#[rune(item = ::process)]
struct Stdio {
    inner: std::process::Stdio,
}

impl Stdio {
    #[rune::function(vm_result, protocol = STRING_DEBUG)]
    fn string_debug(&self, f: &mut Formatter) {
        vm_write!(f, "{:?}", self);
    }

    /// This stream will be ignored. This is the equivalent of attaching the
    /// stream to /dev/null.
    #[rune::function(path = Self::null)]
    fn null() -> Self {
        Self {
            inner: std::process::Stdio::null(),
        }
    }

    /// The child inherits from the corresponding parent descriptor. This is the
    /// default.
    #[rune::function(path = Self::inherit)]
    fn inherit() -> Self {
        Self {
            inner: std::process::Stdio::inherit(),
        }
    }

    /// A new pipe should be arranged to connect the parent and child processes.
    #[rune::function(path = Self::piped)]
    fn piped() -> Self {
        Self {
            inner: std::process::Stdio::piped(),
        }
    }
}

macro_rules! stdio_stream {
    ($name:ident, $stream:tt) => {
        #[derive(Debug, Any)]
        #[rune(item = ::process)]
        #[doc = concat!("The ", $stream, " stream for spawned children.")]
        struct $name {
            inner: process::$name,
        }

        impl $name {
            #[rune::function(vm_result, protocol = STRING_DEBUG)]
            fn string_debug(&self, f: &mut Formatter) {
                vm_write!(f, "{:?}", self);
            }

            /// Try to convert into a `Stdio`, which allows creating a pipeline between
            /// processes.
            ///
            /// This consumes the stream, as it can only be used once.
            ///
            /// Returns a Result<Stdio>
            #[rune::function(instance)]
            fn try_into_stdio(self) -> Result<Stdio, std::io::Error> {
                Ok(Stdio {
                    inner: self.inner.try_into()?,
                })
            }
        }
    };
}
stdio_stream!(ChildStdin, "stdin");
stdio_stream!(ChildStdout, "stdout");
stdio_stream!(ChildStderr, "stderr");