Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions crates/gargoyle-cli/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gargoyle_core::{Check, CheckError, CheckSpec, load_checks};
use serde::Serialize;

use crate::exit::Exit;
use crate::output::print_json;
use crate::output::{out, print_json};

pub(crate) fn list(dir: &Path, json: bool) -> anyhow::Result<Exit> {
let checks = match load_checks(dir) {
Expand All @@ -20,18 +20,18 @@ pub(crate) fn list(dir: &Path, json: bool) -> anyhow::Result<Exit> {
} else if checks.is_empty() {
eprintln!("No checks found in {}", dir.display());
} else {
print_table(&checks);
print_table(&checks)?;
}
Ok(Exit::Clean)
}

pub(crate) fn validate(dir: &Path) -> Exit {
pub(crate) fn validate(dir: &Path) -> anyhow::Result<Exit> {
match load_checks(dir) {
Ok(checks) => {
println!("{} checks valid in {}", checks.len(), dir.display());
Exit::Clean
out!("{} checks valid in {}", checks.len(), dir.display())?;
Ok(Exit::Clean)
}
Err(errors) => report(&errors),
Err(errors) => Ok(report(&errors)),
}
}

Expand All @@ -42,22 +42,23 @@ fn report(errors: &[CheckError]) -> Exit {
Exit::Config
}

fn print_table(checks: &[Check]) {
fn print_table(checks: &[Check]) -> anyhow::Result<()> {
let width = checks
.iter()
.map(|c| c.id().as_str().len())
.max()
.unwrap_or(0);
for check in checks {
let spec = &check.spec;
println!(
out!(
"{:<width$} v{:<3} {:<7} {}",
check.id().as_str(),
spec.version,
spec.severity.as_str(),
spec.description
);
)?;
}
Ok(())
}

/// One entry of `checks list --json`: the frontmatter plus where it came from.
Expand Down
3 changes: 2 additions & 1 deletion crates/gargoyle-cli/src/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::Path;
use anyhow::Context;

use crate::exit::{Exit, config_error};
use crate::output::out;
use crate::workspace::Workspace;

/// `file` is the answer, stdin when it's missing or `-`
Expand Down Expand Up @@ -51,7 +52,7 @@ pub(crate) fn run(run: &str, task: &str, file: Option<&Path>) -> anyhow::Result<
} else {
"findings"
};
println!("ok: {} {noun} for {}", output.findings.len(), task.id);
out!("ok: {} {noun} for {}", output.findings.len(), task.id)?;
Ok(Exit::Clean)
}
Err(problems) => {
Expand Down
11 changes: 6 additions & 5 deletions crates/gargoyle-cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use anyhow::Context;
use gargoyle_core::{CONFIG_FILE, ChecksConfig};

use crate::exit::Exit;
use crate::output::out;

const CONFIG_TEMPLATE: &str = include_str!("../templates/gargoyle.toml");

Expand Down Expand Up @@ -42,11 +43,11 @@ pub(crate) fn run(root: &Path, force: bool, skill: bool) -> anyhow::Result<Exit>
// init can run again any time, it only fills in what's missing
let config_path = root.join(CONFIG_FILE);
if config_path.exists() && !force {
println!("kept {CONFIG_FILE}, use --force to overwrite it");
out!("kept {CONFIG_FILE}, use --force to overwrite it")?;
} else {
fs::write(&config_path, CONFIG_TEMPLATE)
.with_context(|| format!("writing {}", config_path.display()))?;
println!("created {CONFIG_FILE}");
out!("created {CONFIG_FILE}")?;
}

// never overwrite a check, you may have edited it
Expand All @@ -56,11 +57,11 @@ pub(crate) fn run(root: &Path, force: bool, skill: bool) -> anyhow::Result<Exit>
for (name, text) in BUILT_IN_CHECKS {
let path = checks_dir.join(name);
if root.join(&path).exists() {
println!("kept {}", path.display());
out!("kept {}", path.display())?;
continue;
}
fs::write(root.join(&path), text).with_context(|| format!("writing {}", path.display()))?;
println!("created {}", path.display());
out!("created {}", path.display())?;
}

if skill {
Expand All @@ -74,7 +75,7 @@ pub(crate) fn run(root: &Path, force: bool, skill: bool) -> anyhow::Result<Exit>
fn add_skill(root: &Path) -> anyhow::Result<()> {
// windows only finds npx through its .cmd shim
let npx = if cfg!(windows) { "npx.cmd" } else { "npx" };
println!("\nadding the /gargoyle skill with npx {SKILLS_CLI} add {SKILL_SOURCE}");
out!("\nadding the /gargoyle skill with npx {SKILLS_CLI} add {SKILL_SOURCE}")?;
let status = Command::new(npx)
.args(["--yes", SKILLS_CLI, "add", SKILL_SOURCE])
.current_dir(root)
Expand Down
2 changes: 1 addition & 1 deletion crates/gargoyle-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn run(cli: &Cli) -> anyhow::Result<Exit> {
Err(exit) => Ok(exit),
},
Command::Checks(ChecksCommand::Validate { source }) => match checks_dir(source) {
Ok(dir) => Ok(checks::validate(&dir)),
Ok(dir) => checks::validate(&dir),
Err(exit) => Ok(exit),
},
Command::Diff { base } => diff::run(base.as_deref()),
Expand Down
12 changes: 11 additions & 1 deletion crates/gargoyle-cli/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
//! helpers for writting results to stdout
//! helpers for writing results to stdout

use std::io::Write;

use serde::Serialize;

/// like `println!`, but a closed pipe comes back as an error instead of a panic,
/// so `gargoyle checks list | head -1` exits cleanly
macro_rules! out {
($($arg:tt)*) => {{
use std::io::Write as _;
writeln!(std::io::stdout().lock(), $($arg)*)
}};
}
pub(crate) use out;

/// writes pretty json plus a trailing newline to stdout
pub(crate) fn print_json(value: &impl Serialize) -> anyhow::Result<()> {
let mut stdout = std::io::stdout().lock();
Expand Down
8 changes: 4 additions & 4 deletions crates/gargoyle-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! gargoyle is an deterministic review engine
//! gargoyle is a deterministic review engine
//!
//! this crate owns evey step of a review that doesnt need a model
//! loading checks, building the review pla, validating findings,
//! fingerprinting and dedupe, it does no network i/o and never prints
//! this crate owns every step of a review that doesn't need a model:
//! loading checks, building the review plan, validating findings,
//! fingerprinting and dedup, it does no network i/o and never prints

mod check;
mod confidence;
Expand Down
Loading