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
use crate::config;
use anyhow::bail;
use std::{ffi::OsString, io::Write as _};
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
pub struct OptXtask {
    /// Name
    pub subcommand: String,

    /// Arguments for the subcommand
    #[structopt(parse(from_os_str))]
    pub args: Vec<OsString>,
}

pub(crate) fn run(
    opt: OptXtask,
    ctx: crate::Context<impl Sized, impl Sized, impl Sized>,
) -> anyhow::Result<()> {
    let OptXtask { subcommand, args } = opt;

    let crate::Context {
        cwd,
        shell:
            crate::shell::Shell {
                stdin_process_redirection,
                stdout_process_redirection,
                stderr_process_redirection,
                ..
            },
    } = ctx;

    let config::Script {
        program,
        extension,
        content,
    } = config::xtask(&cwd, None, &subcommand)?;

    let mut tempfile = tempfile::Builder::new()
        .prefix(&format!("snowchains-xtask-{}", subcommand))
        .suffix(&format!(".{}", extension))
        .tempfile()?;

    tempfile.write_all(content.as_ref())?;
    tempfile.flush()?;

    let status = std::process::Command::new(program)
        .arg(tempfile.path())
        .args(args)
        .stdin(stdin_process_redirection())
        .stdout(stdout_process_redirection())
        .stderr(stderr_process_redirection())
        .status()?;

    if !status.success() {
        match status.code() {
            Some(code) => bail!("The custom subcommand exited with {}", code),
            None => bail!("The custom subcommand was terminated by signal"),
        }
    }

    tempfile.close()?;
    Ok(())
}