|
1 | 1 | use chrono::NaiveDate;
|
2 | 2 | pub use database::{Commit, PatchName, QueryLabel};
|
3 | 3 | use serde::Deserialize;
|
4 |
| -use std::cmp::PartialOrd; |
| 4 | +use std::{cmp::PartialOrd, ffi::OsStr, path::Path}; |
5 | 5 | use std::fmt;
|
6 | 6 | use std::process::{self, Command};
|
7 | 7 |
|
@@ -144,7 +144,31 @@ pub fn run_command(cmd: &mut Command) -> anyhow::Result<()> {
|
144 | 144 | Ok(())
|
145 | 145 | }
|
146 | 146 |
|
147 |
| -pub fn command_output(cmd: &mut Command) -> anyhow::Result<process::Output> { |
| 147 | +#[cfg(windows)] |
| 148 | +pub fn robocopy(from: &Path, to: &Path, extra_args: &[&dyn AsRef<OsStr>]) -> anyhow::Result<()> { |
| 149 | + let mut cmd = Command::new("robocopy"); |
| 150 | + cmd.arg(from).arg(to).arg("/s").arg("/e"); |
| 151 | + |
| 152 | + for arg in extra_args { |
| 153 | + cmd.arg(arg.as_ref()); |
| 154 | + } |
| 155 | + |
| 156 | + let output = run_command_with_output(&mut cmd)?; |
| 157 | + |
| 158 | + if output.status.code() >= Some(8) { |
| 159 | + // robocopy returns 0-7 on success |
| 160 | + return Err(anyhow::anyhow!( |
| 161 | + "expected success, got {}\n\nstderr={}\n\n stdout={}", |
| 162 | + output.status, |
| 163 | + String::from_utf8_lossy(&output.stderr), |
| 164 | + String::from_utf8_lossy(&output.stdout) |
| 165 | + )); |
| 166 | + } |
| 167 | + |
| 168 | + Ok(()) |
| 169 | +} |
| 170 | + |
| 171 | +fn run_command_with_output(cmd: &mut Command) -> anyhow::Result<process::Output> { |
148 | 172 | log::trace!("running: {:?}", cmd);
|
149 | 173 | let mut child = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
|
150 | 174 |
|
@@ -174,19 +198,25 @@ pub fn command_output(cmd: &mut Command) -> anyhow::Result<process::Output> {
|
174 | 198 | )?;
|
175 | 199 |
|
176 | 200 | let status = child.wait()?;
|
177 |
| - if !status.success() { |
178 |
| - return Err(anyhow::anyhow!( |
179 |
| - "expected success, got {}\n\nstderr={}\n\n stdout={}", |
180 |
| - status, |
181 |
| - String::from_utf8_lossy(&stderr), |
182 |
| - String::from_utf8_lossy(&stdout) |
183 |
| - )); |
184 |
| - } |
185 | 201 |
|
186 |
| - let output = process::Output { |
| 202 | + Ok(process::Output { |
187 | 203 | status,
|
188 | 204 | stdout: stdout,
|
189 | 205 | stderr: stderr,
|
190 |
| - }; |
| 206 | + }) |
| 207 | +} |
| 208 | + |
| 209 | +pub fn command_output(cmd: &mut Command) -> anyhow::Result<process::Output> { |
| 210 | + let output = run_command_with_output(cmd)?; |
| 211 | + |
| 212 | + if !output.status.success() { |
| 213 | + return Err(anyhow::anyhow!( |
| 214 | + "expected success, got {}\n\nstderr={}\n\n stdout={}", |
| 215 | + output.status, |
| 216 | + String::from_utf8_lossy(&output.stderr), |
| 217 | + String::from_utf8_lossy(&output.stdout) |
| 218 | + )); |
| 219 | + } |
| 220 | + |
191 | 221 | Ok(output)
|
192 | 222 | }
|
0 commit comments