Prepublish command
This commit is contained in:
1 parent
d397d50cdb
commit
0a5d300e64
2 files changed
+41
-1
No files matched your search
@@ -1,4 +1,44 @@
|
|||||||
|
use std::{env, fs, process};
|
||||||
|
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
|
use blog::post::PostSource;
|
||||||
|
|
||||||
fn main() {
|
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::<PostSource>() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,6 @@ impl fmt::Display for Header {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
toml::to_string_pretty(self)
|
toml::to_string_pretty(self)
|
||||||
.map_err(|_| fmt::Error)
|
.map_err(|_| fmt::Error)
|
||||||
.and_then(|s| f.write_str(&s))
|
.and_then(|s| f.write_str(s.trim()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user