Skip to content

Commit

Permalink
Merge pull request #223 from whostolemyhat/new-post
Browse files Browse the repository at this point in the history
New post command
  • Loading branch information
epage committed Jun 6, 2017
2 parents 7109912 + b9c3b72 commit 4700f34
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -35,7 +35,7 @@ extern crate lazy_static;
pub use cobalt::build;
pub use error::Error;
pub use config::Config;
pub use new::create_new_project;
pub use new::{create_new_project, create_new_document};

pub mod error;

Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Expand Up @@ -28,7 +28,7 @@ use env_logger::LogBuilder;
use hyper::server::{Server, Request, Response};
use hyper::uri::RequestUri;
use ghp::import_dir;
use cobalt::create_new_project;
use cobalt::{create_new_project, create_new_document};

use notify::{Watcher, RecursiveMode, raw_watcher};
use std::sync::mpsc::channel;
Expand Down Expand Up @@ -108,6 +108,19 @@ fn main() {
.help("Suppress all output")
.default_value("./")
.index(1)))
.subcommand(SubCommand::with_name("new")
.about("Create a new post or page")
.arg(Arg::with_name("FILETYPE")
.help("Type of file to create eg post or page")
.default_value("post")
.takes_value(true))
.arg(Arg::with_name("FILENAME")
.help("File to create")
.default_value_if("FILETYPE",
Some("page"),
"new_page.md")
.default_value("new_post.md")
.takes_value(true)))
.subcommand(SubCommand::with_name("build")
.about("build the cobalt project at the source dir")
.arg(Arg::with_name("import")
Expand Down Expand Up @@ -263,6 +276,20 @@ fn main() {
}
}

"new" => {
let filetype = matches.value_of("FILETYPE").unwrap();
let filename = matches.value_of("FILENAME").unwrap();

match create_new_document(&filetype, &filename, &config) {
Ok(_) => info!("Created new {} {}", filetype, filename),
Err(e) => {
error!("{}", e);
error!("Could not create {}", filetype);
std::process::exit(1);
}
}
}

"build" => {
build(&config);
if matches.is_present("import") {
Expand Down
21 changes: 19 additions & 2 deletions src/new.rs
Expand Up @@ -69,6 +69,7 @@ use std::path::Path;
use std::fs::{DirBuilder, OpenOptions};
use std::io::Write;
use error::Result;
use config::Config;

pub fn create_new_project<P: AsRef<Path>>(dest: P) -> Result<()> {
let dest = dest.as_ref();
Expand All @@ -88,6 +89,19 @@ pub fn create_new_project<P: AsRef<Path>>(dest: P) -> Result<()> {
Ok(())
}

pub fn create_new_document(doc_type: &str, name: &str, config: &Config) -> Result<()> {
let path = Path::new(&config.source);
let full_path = &path.join(&config.posts).join(name);

match doc_type {
"page" => create_file(name, index_liquid)?,
"post" => create_file(full_path, post_1_md)?,
_ => bail!("Unsupported document type {}", doc_type),
}

Ok(())
}

fn create_folder<P: AsRef<Path>>(path: P) -> Result<()> {
trace!("Creating folder {:?}", &path.as_ref());

Expand All @@ -99,9 +113,12 @@ fn create_folder<P: AsRef<Path>>(path: P) -> Result<()> {
fn create_file<P: AsRef<Path>>(name: P, content: &[u8]) -> Result<()> {
trace!("Creating file {:?}", &name.as_ref());

let mut file = try!(OpenOptions::new().write(true).create(true).open(name));
let mut file = try!(OpenOptions::new()
.write(true)
.create_new(true)
.open(name));

try!(file.write_all(content));
file.write_all(content)?;

Ok(())
}

0 comments on commit 4700f34

Please sign in to comment.