From 0a5d300e64ca36a5d15358bc3f76a777f97f454f Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Sat, 4 Jun 2022 21:47:31 +0100 Subject: [PATCH] Prepublish command --- src/bin/prepublish/main.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/lib/post/header.rs | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/bin/prepublish/main.rs b/src/bin/prepublish/main.rs index 3033f62..33158da 100644 --- a/src/bin/prepublish/main.rs +++ b/src/bin/prepublish/main.rs @@ -1,4 +1,44 @@ +use std::{env, fs, process}; + +use chrono::Utc; + +use blog::post::PostSource; fn main() { + let mut failed = false; + + let paths = env::args_os() + .skip(1); + + for path in paths { + let contents = match fs::read_to_string(&path) { + Ok(contents) => contents, + Err(err) => { + eprintln!("failed to read {}: {}", path.to_string_lossy(), err); + failed = true; + continue + } + }; + + let mut source = match contents.parse::() { + Ok(source) => source, + Err(err) => { + eprintln!("failed to parse {}: {}", path.to_string_lossy(), err); + failed = true; + continue + }, + }; + + *source.header_mut().published_mut() = Utc::now(); + + if let Err(err) = fs::write(&path, source.to_string()) { + eprintln!("failed to write {}: {}", path.to_string_lossy(), err); + failed = true; + continue + } + } + if failed { + process::exit(1); + } } diff --git a/src/lib/post/header.rs b/src/lib/post/header.rs index 0863f7b..09e846a 100644 --- a/src/lib/post/header.rs +++ b/src/lib/post/header.rs @@ -92,6 +92,6 @@ impl fmt::Display for Header { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { toml::to_string_pretty(self) .map_err(|_| fmt::Error) - .and_then(|s| f.write_str(&s)) + .and_then(|s| f.write_str(s.trim())) } }