|
| 1 | +use crate::error::Diagnostic; |
| 2 | +use std::process::{Command, Stdio}; |
| 3 | + |
| 4 | +pub enum CmdError { |
| 5 | + /// The binary failed to spawn, probably because it's not installed |
| 6 | + /// or not in PATH |
| 7 | + BinaryNotFound(std::io::Error), |
| 8 | + /// An I/O error occurred accessing the process' pipes |
| 9 | + Io(std::io::Error), |
| 10 | + /// The binary ran, but returned a non-zero exit code and (hopefully) |
| 11 | + /// diagnostics |
| 12 | + ToolErrors { |
| 13 | + exit_code: i32, |
| 14 | + /// Diagnostics that were parsed from the output |
| 15 | + diagnostics: Vec<Diagnostic>, |
| 16 | + }, |
| 17 | +} |
| 18 | + |
| 19 | +impl From<CmdError> for crate::error::Error { |
| 20 | + fn from(ce: CmdError) -> Self {} |
| 21 | +} |
| 22 | + |
| 23 | +pub struct CmdOutput { |
| 24 | + /// The output the command is actually supposed to give back |
| 25 | + pub binary: Vec<u8>, |
| 26 | + /// Warning or Info level diagnostics that were gathered during execution |
| 27 | + pub diagnostics: Vec<Diagnostic>, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(PartialEq, Copy, Clone)] |
| 31 | +pub enum Output { |
| 32 | + /// Doesn't try to read stdout for tool output (other than diagnostics) |
| 33 | + Ignore, |
| 34 | + /// Attempts to retrieve the tool's output from stdout |
| 35 | + Retrieve, |
| 36 | +} |
| 37 | + |
| 38 | +pub fn exec( |
| 39 | + cmd: Command, |
| 40 | + input: Option<&[u8]>, |
| 41 | + retrieve_output: Output, |
| 42 | +) -> Result<CmdOutput, CmdError> { |
| 43 | + if input.is_some() { |
| 44 | + cmd.stdin(Stdio::piped()); |
| 45 | + } |
| 46 | + |
| 47 | + cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); |
| 48 | + |
| 49 | + let mut child = cmd.spawn().map_err(|e| CmdError::BinaryNotFound(e))?; |
| 50 | + |
| 51 | + if let Some(input) = input { |
| 52 | + use std::io::Write; |
| 53 | + |
| 54 | + child |
| 55 | + .stdin |
| 56 | + .take() |
| 57 | + .unwrap() |
| 58 | + .write_all(input) |
| 59 | + .map_err(|e| CmdError::Io(e))?; |
| 60 | + } |
| 61 | + |
| 62 | + let output = child.wait_with_output().map_err(|e| CmdError::Io(e))?; |
| 63 | + |
| 64 | + let code = match output.status.code() { |
| 65 | + Some(code) => code, |
| 66 | + None => { |
| 67 | + #[cfg(unix)] |
| 68 | + let message = { |
| 69 | + use std::os::unix::process::ExitStatusExt; |
| 70 | + format!( |
| 71 | + "process terminated by signal: {}", |
| 72 | + output.status.signal().unwrap_or(666) |
| 73 | + ) |
| 74 | + }; |
| 75 | + #[cfg(not(unix))] |
| 76 | + let message = "process ended in an unknown state".to_owned(); |
| 77 | + |
| 78 | + return Err(CmdError::ToolErrors { |
| 79 | + exit_code: -1, |
| 80 | + diagnostics: vec![Diagnostic { |
| 81 | + line: 0, |
| 82 | + column: 0, |
| 83 | + index: 0, |
| 84 | + message, |
| 85 | + is_text: false, |
| 86 | + }], |
| 87 | + }); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // stderr should only ever contain error+ level diagnostics |
| 92 | + if code != 0 { |
| 93 | + let diagnostics: Vec<_> = match String::from_utf8(output.stderr) { |
| 94 | + Ok(errors) => errors |
| 95 | + .lines() |
| 96 | + .filter_map(|line| crate::error::Message::parse(line).map(Diagnostic::from)) |
| 97 | + .collect(), |
| 98 | + Err(e) => vec![Diagnostic { |
| 99 | + line: 0, |
| 100 | + column: 0, |
| 101 | + index: 0, |
| 102 | + message: format!( |
| 103 | + "unable to read stderr ({}) but process exited with code {}", |
| 104 | + e, code |
| 105 | + ), |
| 106 | + is_text: false, |
| 107 | + }], |
| 108 | + }; |
| 109 | + |
| 110 | + return Err(CmdError::ToolErrors { |
| 111 | + exit_code: code, |
| 112 | + diagnostics, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + fn split<'a>(haystack: &'a [u8], needle: u8) -> impl Iterator<Item = &'a [u8]> + 'a { |
| 117 | + struct Split<'a> { |
| 118 | + haystack: &'a [u8], |
| 119 | + needle: u8, |
| 120 | + } |
| 121 | + |
| 122 | + impl<'a> Iterator for Split<'a> { |
| 123 | + type Item = &'a [u8]; |
| 124 | + |
| 125 | + fn next(&mut self) -> Option<&'a [u8]> { |
| 126 | + if self.haystack.is_empty() { |
| 127 | + return None; |
| 128 | + } |
| 129 | + let (ret, remaining) = match memchr::memchr(self.needle, self.haystack) { |
| 130 | + Some(pos) => (&self.haystack[..pos], &self.haystack[pos + 1..]), |
| 131 | + None => (self.haystack, &[][..]), |
| 132 | + }; |
| 133 | + self.haystack = remaining; |
| 134 | + Some(ret) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + Split { haystack, needle } |
| 139 | + } |
| 140 | + |
| 141 | + let retrieve_output = retrieve_output == Output::Retrieve; |
| 142 | + |
| 143 | + // Since we are retrieving the results via stdout, but it can also contain |
| 144 | + // diagnostic messages, we need to be careful |
| 145 | + let mut diagnostics = Vec::new(); |
| 146 | + let mut binary = Vec::with_capacity(if retrieve_output { 1024 } else { 0 }); |
| 147 | + |
| 148 | + let mut iter = split(&output.stdout, b'\n'); |
| 149 | + let mut maybe_diagnostic = true; |
| 150 | + for line in iter { |
| 151 | + if maybe_diagnostic { |
| 152 | + if let Some(s) = std::str::from_utf8(line).ok() { |
| 153 | + if let Some(msg) = crate::error::Message::parse(s) { |
| 154 | + diagnostics.push(Diagnostic::from(msg)); |
| 155 | + continue; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if retrieve_output { |
| 161 | + binary.extend_from_slice(line); |
| 162 | + } |
| 163 | + maybe_diagnostic = false; |
| 164 | + } |
| 165 | + |
| 166 | + Ok(CmdOutput { |
| 167 | + binary, |
| 168 | + diagnostics, |
| 169 | + }) |
| 170 | +} |
0 commit comments