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 glog::Flags;
use log::info; use log::{error, info};
use rocket::{fairing::AdHoc, response::Responder, State}; use rocket::{fairing::AdHoc, response::Responder, State};
use serde::Deserialize; use serde::Deserialize;
use thiserror::Error; use thiserror::Error;
@ -29,46 +29,65 @@ fn post_reload(config: &State<Config>, repo: &str) -> Result<String, SyncError>
reload(config, repo) 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> { fn reload(config: &State<Config>, repo: &str) -> Result<String, SyncError> {
info!("Need to reload '{}': {:?}", repo, config); info!("Need to reload '{}': {:?}", repo, config);
let source_path = config.source_root.join(repo); let source_path = config.source_root.join(repo);
let build_path = config.build_root.join(repo); let build_path = config.build_root.join(repo);
dbg!(&build_path); dbg!(&build_path);
dbg!(&source_path); dbg!(&source_path);
let needs_clone = false; let mut output = Vec::new();
let mut output = String::new(); let needs_clone = !build_path.exists();
if needs_clone { if needs_clone {
output += &format!( output.push(
"{:?}", logging_run(
Command::new("/run/current-system/sw/bin/git") Command::new("/run/current-system/sw/bin/git")
.current_dir(&config.build_root) .current_dir(&config.build_root)
.arg("clone") .arg("clone")
.arg(&source_path) .arg(&source_path)
.arg(&build_path) .arg(&build_path),
.output()? )
.output()?,
); );
} }
// Make sure buildable clone is up to date // Make sure buildable clone is up to date
output += &format!( output.push(
"{:?}", logging_run(
Command::new("/run/current-system/sw/bin/git") Command::new("/run/current-system/sw/bin/git")
.current_dir(&build_path) .current_dir(&build_path)
.arg("pull") .arg("pull"),
.output()? )
.output()?,
); );
// Run `cargo aoc bench` // Run `cargo aoc bench`
output += &format!( output.push(
"{:?}", logging_run(
Command::new("cargo") Command::new("cargo")
.current_dir(&build_path) .current_dir(&build_path)
.arg("aoc") .arg("aoc")
.arg("bench") .arg("bench"),
.output()? )
.output()?,
); );
// Copy files from `target/` to serving directory // Copy files from `target/` to serving directory
let bench_path = build_path.join("target/aoc/aoc-autobench/target/criterion"); let bench_path = build_path.join("target/aoc/aoc-autobench/target/criterion");
copy_dir_all(bench_path, config.www_root.join(repo))?; 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); info!("{}", response);
Ok(response) Ok(response)
} }
@ -94,6 +113,12 @@ fn index() -> &'static str {
"Hello, world!" "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)] #[derive(Debug, Deserialize)]
struct Config { struct Config {
source_root: PathBuf, source_root: PathBuf,
@ -113,5 +138,6 @@ fn rocket() -> _ {
.unwrap(); .unwrap();
rocket::build() rocket::build()
.mount("/", routes![index, get_reload, post_reload]) .mount("/", routes![index, get_reload, post_reload])
.register("/", catchers![http500])
.attach(AdHoc::config::<Config>()) .attach(AdHoc::config::<Config>())
} }