mk_lib/schema/command/
mod.rs

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
use std::io::{
  BufRead as _,
  BufReader,
};
use std::process::Command as ProcessCommand;

use std::thread;

use crate::handle_output;
use crate::schema::get_output_handler;
use anyhow::Context;

use super::TaskContext;
use serde::Deserialize;

mod container_build;
mod container_run;
mod local_run;
mod task_run;

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum CommandRunner {
  ContainerBuild(container_build::ContainerBuild),
  ContainerRun(container_run::ContainerRun),
  LocalRun(local_run::LocalRun),
  TaskRun(task_run::TaskRun),
  CommandRun(String),
}

impl CommandRunner {
  pub fn execute(&self, context: &mut TaskContext) -> anyhow::Result<()> {
    match self {
      CommandRunner::ContainerBuild(container_build) => container_build.execute(context),
      CommandRunner::ContainerRun(container_run) => container_run.execute(context),
      CommandRunner::LocalRun(local_run) => local_run.execute(context),
      CommandRunner::TaskRun(task_run) => task_run.execute(context),
      CommandRunner::CommandRun(command) => self.execute_command(context, command),
    }
  }

  fn execute_command(&self, context: &TaskContext, command: &str) -> anyhow::Result<()> {
    assert!(!command.is_empty());

    let ignore_errors = context.ignore_errors();
    let verbose = context.verbose();
    let shell: &str = &context.shell();

    let stdout = get_output_handler(verbose);
    let stderr = get_output_handler(verbose);

    let mut cmd = ProcessCommand::new(shell);
    cmd.arg("-c").arg(command).stdout(stdout).stderr(stderr);

    // Inject environment variables
    for (key, value) in context.env_vars.iter() {
      cmd.env(key, value);
    }

    let mut cmd = cmd.spawn()?;
    if verbose {
      handle_output!(cmd.stdout, context);
      handle_output!(cmd.stderr, context);
    }

    let status = cmd.wait()?;
    if !status.success() && !ignore_errors {
      anyhow::bail!("Command failed - {}", command);
    }

    Ok(())
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn test_command_1() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        ignore_errors: false
        verbose: false
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;

      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert_eq!(local_run.ignore_errors, Some(false));
        assert_eq!(local_run.verbose, Some(false));
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_2() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;

      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert_eq!(local_run.ignore_errors, None);
        assert_eq!(local_run.verbose, None);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_3() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        ignore_errors: true
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert_eq!(local_run.ignore_errors, Some(true));
        assert_eq!(local_run.verbose, None);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_4() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        verbose: false
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, None);
        assert_eq!(local_run.ignore_errors, None);
        assert_eq!(local_run.verbose, Some(false));
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_5() -> anyhow::Result<()> {
    {
      let yaml = "
        command: 'echo \"Hello, World!\"'
        work_dir: /tmp
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::LocalRun(local_run) = command {
        assert_eq!(local_run.command, "echo \"Hello, World!\"");
        assert_eq!(local_run.shell, "sh");
        assert_eq!(local_run.work_dir, Some("/tmp".into()));
        assert_eq!(local_run.ignore_errors, None);
        assert_eq!(local_run.verbose, None);
      } else {
        panic!("Expected CommandRunner::LocalRun");
      }

      Ok(())
    }
  }

  #[test]
  fn test_command_6() -> anyhow::Result<()> {
    {
      let yaml = "
        echo 'Hello, World!'
      ";
      let command = serde_yaml::from_str::<CommandRunner>(yaml)?;
      if let CommandRunner::CommandRun(command) = command {
        assert_eq!(command, "echo 'Hello, World!'");
      } else {
        panic!("Expected CommandRunner::CommandRun");
      }

      Ok(())
    }
  }
}