Better collect output of commands.

This commit is contained in:
Bill Thiede 2022-12-03 18:11:36 -08:00
parent 6248844714
commit 0ee02d199e

View File

@ -7,7 +7,7 @@ use std::{
};
use glog::Flags;
use log::info;
use log::{error, info};
use rocket::{fairing::AdHoc, response::Responder, State};
use serde::Deserialize;
use thiserror::Error;
@ -29,46 +29,65 @@ fn post_reload(config: &State<Config>, repo: &str) -> Result<String, SyncError>
reload(config, repo)
}
fn logging_run(cmd: &mut Command) -> &mut Command {
info!("Running {cmd:?}");
cmd
}
fn reload(config: &State<Config>, repo: &str) -> Result<String, SyncError> {
info!("Need to reload '{}': {:?}", repo, config);
let source_path = config.source_root.join(repo);
let build_path = config.build_root.join(repo);
dbg!(&build_path);
dbg!(&source_path);
let needs_clone = false;
let mut output = String::new();
let mut output = Vec::new();
let needs_clone = !build_path.exists();
if needs_clone {
output += &format!(
"{:?}",
Command::new("/run/current-system/sw/bin/git")
.current_dir(&config.build_root)
.arg("clone")
.arg(&source_path)
.arg(&build_path)
.output()?
output.push(
logging_run(
Command::new("/run/current-system/sw/bin/git")
.current_dir(&config.build_root)
.arg("clone")
.arg(&source_path)
.arg(&build_path),
)
.output()?,
);
}
// Make sure buildable clone is up to date
output += &format!(
"{:?}",
Command::new("/run/current-system/sw/bin/git")
.current_dir(&build_path)
.arg("pull")
.output()?
output.push(
logging_run(
Command::new("/run/current-system/sw/bin/git")
.current_dir(&build_path)
.arg("pull"),
)
.output()?,
);
// Run `cargo aoc bench`
output += &format!(
"{:?}",
Command::new("cargo")
.current_dir(&build_path)
.arg("aoc")
.arg("bench")
.output()?
output.push(
logging_run(
Command::new("cargo")
.current_dir(&build_path)
.arg("aoc")
.arg("bench"),
)
.output()?,
);
// Copy files from `target/` to serving directory
let bench_path = build_path.join("target/aoc/aoc-autobench/target/criterion");
copy_dir_all(bench_path, config.www_root.join(repo))?;
let response = format!("{:?}", output);
let response = output
.iter()
.map(|o| {
format!(
"Status {}:\nStdout: {}\nStderr: {}",
o.status,
String::from_utf8_lossy(&o.stdout),
String::from_utf8_lossy(&o.stderr)
)
})
.collect::<Vec<_>>()
.join("\n");
info!("{}", response);
Ok(response)
}
@ -94,6 +113,12 @@ fn index() -> &'static str {
"Hello, world!"
}
#[catch(500)]
fn http500(req: &rocket::Request) -> String {
// TODO(wathiede): figure out a way to retrieve the Error that got us here?
format!("{:?}", req)
}
#[derive(Debug, Deserialize)]
struct Config {
source_root: PathBuf,
@ -113,5 +138,6 @@ fn rocket() -> _ {
.unwrap();
rocket::build()
.mount("/", routes![index, get_reload, post_reload])
.register("/", catchers![http500])
.attach(AdHoc::config::<Config>())
}