use std::io::{self, Write};
use std::path::Path;
use clap::ValueEnum;
use dataferry_dsl::config::{ConfigError, ErrorKind, Location, mask_urls};
use dataferry_registry::Redactor;
use dataferry_registry::secret::url_password;
use serde_json::json;
pub(crate) mod code {
pub const CONFIG_DIR: &str = "F0001";
pub const MISWIRED: &str = "F0002";
#[allow(dead_code)]
pub const NOT_IMPLEMENTED: &str = "F0003";
pub const OUTPUT: &str = "F0004";
pub const LOAD_IO: &str = "F0101";
pub const LOAD_YAML: &str = "F0102";
pub const LOAD_LAYOUT: &str = "F0103";
pub const LOAD_INCLUDE: &str = "F0104";
pub const LOAD_MODEL: &str = "F0105";
pub const LOAD_DUPLICATE: &str = "F0106";
pub const LOAD_INVALID: &str = "F0107";
pub const LOAD_ENV: &str = "F0108";
pub const LOAD_DEPENDS_ON: &str = "F0109";
pub const EXPR: &str = "F0110";
pub const TYPE: &str = "F0111";
pub const PLAN: &str = "F0112";
pub const LOAD_OTHER: &str = "F0100";
pub const OPTIONS: &str = "F0301";
pub const UNKNOWN_TYPE: &str = "F0302";
pub const ROLE: &str = "F0303";
pub const STATE_TYPE: &str = "F0304";
pub const STATE_NOT_CAPABLE: &str = "F0305";
pub const BUILD: &str = "F0306";
pub const SECRET_LITERAL: &str = "W0301";
pub const PLAN_WARNING: &str = "W0401";
pub const CONNECT: &str = "F0501";
pub const RUN: &str = "F0601";
pub const PARTITION_FAILED: &str = "F0602";
pub const INTERRUPTED: &str = "F0603";
pub const LEASE_OVERRIDE: &str = "W0601";
pub const VERIFY_FAILED: &str = "F0701";
pub const UNVERIFIED: &str = "W0701";
pub const RESUME_REFUSED: &str = "F0801";
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Severity {
Error,
Warning,
}
impl Severity {
fn as_str(self) -> &'static str {
match self {
Severity::Error => "error",
Severity::Warning => "warning",
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct Diagnostic {
pub severity: Severity,
pub code: &'static str,
pub message: String,
pub location: Option<Location>,
pub path: String,
pub help: Option<String>,
}
impl Diagnostic {
pub fn error(code: &'static str, message: impl Into<String>) -> Self {
Self {
severity: Severity::Error,
code,
message: message.into(),
location: None,
path: String::new(),
help: None,
}
}
pub fn warning(code: &'static str, message: impl Into<String>) -> Self {
Self {
severity: Severity::Warning,
..Self::error(code, message)
}
}
pub fn at(mut self, location: Option<&Location>) -> Self {
self.location = location.cloned();
self
}
pub fn path(mut self, path: impl Into<String>) -> Self {
self.path = path.into();
self
}
pub fn help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
pub fn is_error(&self) -> bool {
self.severity == Severity::Error
}
}
impl From<ConfigError> for Diagnostic {
fn from(error: ConfigError) -> Self {
let code = match error.kind {
ErrorKind::Io => code::LOAD_IO,
ErrorKind::Yaml => code::LOAD_YAML,
ErrorKind::Layout => code::LOAD_LAYOUT,
ErrorKind::Include => code::LOAD_INCLUDE,
ErrorKind::Model => code::LOAD_MODEL,
ErrorKind::Duplicate => code::LOAD_DUPLICATE,
ErrorKind::Invalid => code::LOAD_INVALID,
ErrorKind::Env => code::LOAD_ENV,
ErrorKind::DependsOn => code::LOAD_DEPENDS_ON,
ErrorKind::Expr => code::EXPR,
ErrorKind::Type => code::TYPE,
ErrorKind::Plan => code::PLAN,
_ => code::LOAD_OTHER,
};
Self {
severity: Severity::Error,
code,
message: error.message,
location: error.location,
path: error.path,
help: error.help,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum Format {
#[default]
Text,
Json,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Redaction {
secrets: Redactor,
}
impl Redaction {
pub fn add(&mut self, value: &str) {
self.secrets.extend(url_password(value));
self.secrets.add(value);
}
pub fn apply(&self, text: &str) -> String {
mask_urls(&self.secrets.redact(text))
}
}
pub(crate) struct Printer<'a> {
pub out: &'a mut dyn Write,
pub err: &'a mut dyn Write,
pub format: Format,
pub color: bool,
pub redaction: Redaction,
}
impl Printer<'_> {
pub fn output(&mut self, text: &str) -> io::Result<()> {
let text = self.redaction.apply(text);
self.out.write_all(text.as_bytes())?;
self.out.flush()
}
pub fn note(&mut self, text: &str) -> io::Result<()> {
let text = self.redaction.apply(text);
self.err.write_all(text.as_bytes())?;
self.err.flush()
}
pub fn diagnostics(&mut self, diagnostics: &[Diagnostic], config_dir: &Path) -> io::Result<()> {
let mut text = String::new();
for d in diagnostics {
match self.format {
Format::Text => render_text(d, config_dir, self.color, &mut text),
Format::Json => {
text.push_str(&render_json(d, config_dir).to_string());
text.push('\n');
}
}
}
let text = self.redaction.apply(&text);
self.err.write_all(text.as_bytes())?;
self.err.flush()
}
}
fn display_file(config_dir: &Path, location: &Location) -> String {
config_dir.join(&*location.file).display().to_string()
}
fn render_json(d: &Diagnostic, config_dir: &Path) -> serde_json::Value {
json!({
"severity": d.severity.as_str(),
"code": d.code,
"message": d.message,
"file": d.location.as_ref().map(|l| display_file(config_dir, l)),
"line": d.location.as_ref().map(|l| l.line),
"column": d.location.as_ref().map(|l| l.column),
"path": (!d.path.is_empty()).then_some(&d.path),
"help": d.help,
})
}
fn render_text(d: &Diagnostic, config_dir: &Path, color: bool, out: &mut String) {
let paint = |style: &str, text: &str| match color {
true => format!("\x1b[{style}m{text}\x1b[0m"),
false => text.to_owned(),
};
let label = match d.severity {
Severity::Error => paint("1;31", &format!("error[{}]", d.code)),
Severity::Warning => paint("1;33", &format!("warning[{}]", d.code)),
};
out.push_str(&label);
out.push_str(": ");
if !d.path.is_empty() {
out.push_str(&d.path);
out.push_str(": ");
}
out.push_str(&d.message);
out.push('\n');
let mut gutter = 2;
if let Some(location) = &d.location {
let line = source_line(config_dir, location);
gutter = line
.as_ref()
.map_or(1, |_| location.line.to_string().len())
.max(1);
let pad = " ".repeat(gutter);
out.push_str(&format!(
"{pad}{} {}:{}:{}\n",
paint("1;34", "-->"),
display_file(config_dir, location),
location.line,
location.column
));
if let Some(text) = line {
let bar = paint("1;34", "|");
let start = (location.column as usize).saturating_sub(1);
let width = text
.chars()
.skip(start)
.take_while(|c| !c.is_whitespace())
.count()
.max(1);
out.push_str(&format!("{pad} {bar}\n"));
out.push_str(&format!(
"{} {bar} {text}\n",
paint("1;34", &location.line.to_string())
));
out.push_str(&format!(
"{pad} {bar} {}{}\n",
" ".repeat(start),
paint("1;31", &"^".repeat(width))
));
}
}
if let Some(help) = &d.help {
out.push_str(&format!(
"{} {} help: {help}\n",
" ".repeat(gutter),
paint("1;34", "=")
));
}
}
fn source_line(config_dir: &Path, location: &Location) -> Option<String> {
let text = std::fs::read_to_string(config_dir.join(&*location.file)).ok()?;
let line = text.lines().nth((location.line as usize).checked_sub(1)?)?;
Some(line.replace('\t', " "))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_points_at_the_line_and_underlines_the_token() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("connections.yaml"),
"connections:\n - name: warehouse\n type: postgresql\n",
)
.unwrap();
let d = Diagnostic::error(code::UNKNOWN_TYPE, "unknown connection type `postgresql`")
.at(Some(&Location::new("connections.yaml", 3, 11)))
.path("connections[warehouse].type")
.help("registered types: `postgres`");
let mut text = String::new();
render_text(&d, dir.path(), false, &mut text);
let file = dir.path().join("connections.yaml").display().to_string();
assert_eq!(
text,
format!(
"error[F0302]: connections[warehouse].type: unknown connection type `postgresql`\n \
--> {file}:3:11\n \
|\n\
3 | type: postgresql\n \
| ^^^^^^^^^^\n \
= help: registered types: `postgres`\n"
)
);
}
#[test]
fn text_without_a_readable_file_still_names_file_and_line() {
let d = Diagnostic::error(code::LOAD_YAML, "bad indentation").at(Some(&Location::new(
"flows/orders.yaml",
4,
2,
)));
let mut text = String::new();
render_text(&d, Path::new("/nonexistent/ferry"), false, &mut text);
assert_eq!(
text,
"error[F0102]: bad indentation\n --> /nonexistent/ferry/flows/orders.yaml:4:2\n"
);
}
#[test]
fn json_has_every_field_and_nulls_for_missing_ones() {
let d = Diagnostic::warning(code::SECRET_LITERAL, "literal secret");
assert_eq!(
render_json(&d, Path::new("ferry")),
json!({
"severity": "warning", "code": "W0301", "message": "literal secret",
"file": null, "line": null, "column": null, "path": null, "help": null,
})
);
}
#[test]
fn redaction_masks_values_their_url_passwords_and_any_other_url_password() {
let mut r = Redaction::default();
r.add("postgres://app:hunter2@db/shop");
assert_eq!(
r.apply("url postgres://app:hunter2@db/shop, password hunter2"),
"url ***, password ***"
);
assert_eq!(
r.apply("other mongodb://u:pw@host/x"),
"other mongodb://u:***@host/x"
);
r.add(r#"pa"ss"#);
assert_eq!(
r.apply(&json!({"m": r#"pa"ss"#}).to_string()),
r#"{"m":"***"}"#
);
}
}