Skip to content

Commit

Permalink
Add more logging, color commamnd STDERR and STDOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Feb 19, 2025
1 parent b85ee7e commit 7b97957
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ exclude = [ ".github", ".vscode", ".gitignore", ".ci", ".pre-*.yaml"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ansi_term = "0.12.1"
cargo_toml = "0.21.0"
chrono = "0.4.39"
hex = "0.4.3"
iri-string = "0.7.7"
log = { version = "0.4.25", features = ["kv"] }
pgxn_meta = "0.5.2"
regex = "1.11.1"
scopeguard = "1.2.0"
semver = "1.0.25"
serde = "1.0.217"
serde_json = "1.0.138"
Expand Down
43 changes: 26 additions & 17 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use dist::{Dist, Release, Releases};
use crate::error::BuildError;
use iri_string::spec;
use iri_string::template::{simple_context::SimpleContext, UriTemplateStr, UriTemplateString};
use log::{debug, info, trace};
use log::{debug, info};
use semver::Version;
use serde_json::{json, Value};
use std::{
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Api {
ctx.insert("version", version.to_string());
let url = self.url_for("meta", ctx)?;
let mut val = fetch_json(&self.agent, &url)?;
debug!(url:display; "parsing");
debug!(url:?; "parsing");
if val.get("meta-spec").is_none() {
// PGXN v1 stripped meta-spec out of this API :-/.
let val_type = type_of!(val);
Expand All @@ -117,7 +117,7 @@ impl Api {
/// Unpack download `file` in directory `into` and return the path to the
/// unpacked directory.
pub fn unpack<P: AsRef<Path>>(&self, into: P, file: P) -> Result<PathBuf, BuildError> {
info!(file:display = crate::filename(&file); "unpacking");
info!(archive:? = crate::filename(&file); "Unpacking");
let zip = File::open(file)?;
let mut archive = zip::ZipArchive::new(zip)?;
archive.extract(&into)?;
Expand All @@ -137,7 +137,7 @@ impl Api {
.ok_or_else(|| BuildError::UnknownTemplate(name.to_string()))?;
let path = template.expand::<spec::UriSpec, _>(&ctx)?;
let url = self.url.join(&path.to_string())?;
trace!(url:display; "resolved URL");
debug!(url:?; "Resolved");
Ok(url)
}

Expand All @@ -152,10 +152,9 @@ impl Api {
ctx.insert("dist", meta.name());
ctx.insert("version", meta.version().to_string());
let url = self.url_for("download", ctx)?;
info!(url:display; "downloading");
let file = self.download_url_to(dir, url)?;
info!(file:display = file.display(); "validating");
meta.release().digests().validate(&file)?;

Ok(file)
}

Expand All @@ -166,7 +165,7 @@ impl Api {
dir: P,
url: url::Url,
) -> Result<PathBuf, BuildError> {
trace!( url:display, dir:display = dir.as_ref().display(); "downloading");
info!(from:% = url, to:?=dir.as_ref(); "Downloading");
// Extract the file name from the URL.
match url.path_segments() {
None => Err(BuildError::NoUrlFile(url))?,
Expand All @@ -183,31 +182,40 @@ impl Api {
// Copy the file. Eschew std::fs::copy for better
// error messages.
let mut input = get_file(&url)?;
debug!(destination:?=dst; "Create");
return match File::create(&dst) {
Err(e) => Err(BuildError::File(
"creating",
dst.display().to_string(),
e.kind(),
)),
Ok(mut out) => match io::copy(&mut input, &mut out) {
Ok(_) => Ok(dst),
Err(e) => copy_err!(url.to_file_path().unwrap().display(), dst, e),
},
Ok(mut out) => {
debug!(to:?=dst; "Copy");
match io::copy(&mut input, &mut out) {
Ok(_) => Ok(dst),
Err(e) => copy_err!(url.to_file_path().unwrap().display(), dst, e),
}
}
};
}

// Download the file over HTTP.
debug!(url:%; "GET");
let res = self.agent.get(url.as_str()).call()?;
debug!(destination:?=dst; "Create");
match File::create(&dst) {
Err(e) => Err(BuildError::File(
"creating",
dst.display().to_string(),
e.kind(),
)),
Ok(mut out) => match io::copy(&mut res.into_body().as_reader(), &mut out) {
Ok(_) => Ok(dst),
Err(e) => copy_err!(url, dst, e),
},
Ok(mut out) => {
debug!(to:?=dst; "Copy");
match io::copy(&mut res.into_body().as_reader(), &mut out) {
Ok(_) => Ok(dst),
Err(e) => copy_err!(url, dst, e),
}
}
}
}
}
Expand All @@ -227,7 +235,7 @@ fn parse_base_url(url: &str) -> Result<url::Url, url::ParseError> {

/// Fetches the JSON at URL and converts it to a serde_json::Value.
fn fetch_json(agent: &ureq::Agent, url: &url::Url) -> Result<Value, BuildError> {
debug!(url:display; "fetching");
debug!(url:?; "fetching");
match url.scheme() {
"file" => Ok(serde_json::from_reader(get_file(url)?)?),
// Avoid .into_json(); it returns IO errors.
Expand All @@ -243,7 +251,7 @@ fn fetch_reader(
agent: &ureq::Agent,
url: &url::Url,
) -> Result<Box<dyn io::Read + Send + Sync + 'static>, BuildError> {
debug!(url:display; "fetching");
debug!(url:?; "fetching");
match url.scheme() {
"file" => Ok(Box::new(get_file(url)?)),
"http" | "https" => Ok(Box::new(
Expand All @@ -260,6 +268,7 @@ fn get_file(url: &url::Url) -> Result<File, BuildError> {
Err(_) => Err(BuildError::NoUrlFile(url.clone()))?,
Ok(s) => s,
};
debug!(file:? = src; "Source");
// if src.is_dir() {
// return Err(BuildError::File(
// "opening",
Expand Down
3 changes: 2 additions & 1 deletion src/pgxs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::pipeline::Pipeline;
use crate::{error::BuildError, pg_config::PgConfig};
use log::info;
use log::{debug, info};
use regex::Regex;
use std::{
fs::{self, File},
Expand Down Expand Up @@ -114,6 +114,7 @@ fn makefile(dir: &Path) -> Option<PathBuf> {
for makefile in ["GNUmakefile", "makefile", "Makefile"] {
let file = dir.join(makefile);
if file.exists() {
debug!("Found {:?}", file);
return Some(file);
}
}
Expand Down
33 changes: 29 additions & 4 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
use crate::{error::BuildError, pg_config::PgConfig};
use log::debug;
use std::{io::Write, path::Path, process::Command};
use scopeguard::defer;
use std::{
io::{self, IsTerminal, Write},
path::Path,
process::Command,
};

/// Defines the interface for build pipelines to configure, compile, and test
/// PGXN distributions.
Expand Down Expand Up @@ -53,7 +58,7 @@ pub(crate) trait Pipeline<P: AsRef<Path>> {
/// Attempts to write a temporary file to `dir` and returns `true` on
/// success and `false` on failure. The temporary file will be deleted.
fn is_writeable<D: AsRef<Path>>(&self, dir: D) -> bool {
debug!(dir:display = crate::filename(&dir); "testing write access");
debug!(dir:? = crate::filename(&dir); "testing write access");
match tempfile::Builder::new()
.prefix("pgxn-")
.suffix(".test")
Expand All @@ -71,10 +76,30 @@ pub(crate) trait Pipeline<P: AsRef<Path>> {
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
// Set up STDOUT to be dimmed grey.
let grey = ansi_term::Color::Fixed(244).dimmed();
let mut stdout = io::stdout();
if stdout.is_terminal() {
write!(stdout, "{}", grey.prefix())?;
}

// Set up STDERR to be red.
let mut stderr = io::stderr();
let red = ansi_term::Color::Red;
if stderr.is_terminal() {
write!(stderr, "{}", red.prefix())?;
}

// Reset colors when this function exits.
defer! {
if stderr.is_terminal() { _= write!(stderr, "{}", red.suffix()) }
if stdout.is_terminal() { _= write!(stdout, "{}", grey.suffix()) }
};

// Use `sudo` if the param is set.
let mut cmd = self.maybe_sudo(program, sudo);
cmd.args(args);
cmd.current_dir(self.dir());
cmd.args(args).current_dir(self.dir());
debug!(command:? = cmd; "Executing");
match cmd.status() {
Ok(status) => {
if !status.success() {
Expand Down

0 comments on commit 7b97957

Please sign in to comment.