From 0ee02d199e3df54383495c57a6f15b4cc6fdcb42 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 3 Dec 2022 18:11:36 -0800 Subject: [PATCH] Better collect output of commands. --- src/main.rs | 76 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff82997..ffca413 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, repo: &str) -> Result reload(config, repo) } +fn logging_run(cmd: &mut Command) -> &mut Command { + info!("Running {cmd:?}"); + cmd +} + fn reload(config: &State, repo: &str) -> Result { 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::>() + .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::()) }