use chrono::Utc;
use std::{env, fs, path};
use error::{Result, ResultExt};
use std::fs::OpenOptions;
use std::process::Command;
pub fn post(title: &str, edit: bool) -> Result<()> {
let mut path = env::current_dir()
.chain_err(|| "couldn't get current directory")?;
path.push("posts");
if title.chars().any(path::is_separator) {
return Err("the title contains a path separator".into());
}
if !path.is_dir() {
fs::create_dir(&path)
.chain_err(|| "couldn't create posts directory")?;
}
let date = Utc::today().format("%Y-%m-%d");
path.push(format!("{} {}.md", date, title));
if path.exists() {
return Err("post already exists".into());
}
OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.chain_err(|| "couldn't create post")?;
info!("Created post at '{}'.", path.display());
if edit {
if let Some(ed) = env::var_os("EDITOR") {
match Command::new(ed).arg(path).spawn() {
Ok(mut child) => {
let _ = child.wait();
},
Err(e) => {
warn!("The EDITOR could not be started: {}.", e);
}
}
} else {
warn!("Could not find EDITOR environment variable.");
}
}
Ok(())
}