Better collect output of commands.
This commit is contained in:
parent
6248844714
commit
0ee02d199e
58
src/main.rs
58
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<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!(
|
||||
"{:?}",
|
||||
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()?
|
||||
.arg(&build_path),
|
||||
)
|
||||
.output()?,
|
||||
);
|
||||
}
|
||||
// Make sure buildable clone is up to date
|
||||
output += &format!(
|
||||
"{:?}",
|
||||
output.push(
|
||||
logging_run(
|
||||
Command::new("/run/current-system/sw/bin/git")
|
||||
.current_dir(&build_path)
|
||||
.arg("pull")
|
||||
.output()?
|
||||
.arg("pull"),
|
||||
)
|
||||
.output()?,
|
||||
);
|
||||
// Run `cargo aoc bench`
|
||||
output += &format!(
|
||||
"{:?}",
|
||||
output.push(
|
||||
logging_run(
|
||||
Command::new("cargo")
|
||||
.current_dir(&build_path)
|
||||
.arg("aoc")
|
||||
.arg("bench")
|
||||
.output()?
|
||||
.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>())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user