sloppy_core/
execute.rs

1use std::io::Write;
2use crate::command::Command;
3use crate::context::Context;
4use crate::error::Result;
5
6#[derive(Debug)]
7pub struct Options {
8    pub args: Vec<String>,
9    pub verbose: bool,
10}
11
12#[doc = "Execute a list of commands!"]
13pub fn execute(ctx: &mut Context, commands: &[Command], options: &Options) -> Result<()> {
14    let len = commands.len();
15    for (i, command) in commands.iter().enumerate() {
16        // print command information prior to execution
17        if options.verbose {
18            println!("Execute command: {}", command.name);
19            println!("Execute backend: {}", command.backend);
20            println!("Command imports: {:?}", command.imports);
21            println!("Command exports: {:?}", command.exports);
22            if !command.comment.is_empty() {
23                println!("Command comment: {}", command.comment);
24            }
25            std::io::stdout().flush()?;
26            std::io::stderr().flush()?;
27        }
28
29        match ctx.execute(command, options) {
30            Ok(_) => {}
31            Err(e) => {
32                if command.errexit {
33                    // don't execute following steps
34                    return Err(e);
35                } else {
36                    // continue executing following steps
37                    eprintln!("Failed to execute command: {}", command.name);
38                    eprintln!("{:?}", e);
39                }
40            }
41        }
42
43        // print new line after command execution
44        if options.verbose && i < len - 1 {
45            println!();
46            std::io::stdout().flush()?;
47            std::io::stderr().flush()?;
48        }
49    }
50    Ok(())
51}