Added arg for init to specify directory

This commit is contained in:
Mathieu David 2015-07-07 12:36:11 +02:00
parent a6b3f99c53
commit 508f3068f0
2 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,7 @@
extern crate mdbook; extern crate mdbook;
extern crate getopts; extern crate getopts;
use std::env; use std::env;
use std::path::PathBuf;
use mdbook::MDBook; use mdbook::MDBook;
@ -112,7 +113,12 @@ fn init(args: Vec<String>) {
return; return;
} }
let dir = std::env::current_dir().unwrap(); let dir = if args.len() <= 2 {
std::env::current_dir().unwrap()
} else {
std::env::current_dir().unwrap().join(&args[2])
};
let book = MDBook::new(); let book = MDBook::new();
if let Err(e) = book.init(&dir) { if let Err(e) = book.init(&dir) {
@ -140,6 +146,13 @@ fn build(args: Vec<String>) {
if matches.opt_present("help") { if matches.opt_present("help") {
println!("{}", usage); println!("{}", usage);
} }
let dir = std::env::current_dir().unwrap();
let book = MDBook::new();
if let Err(e) = book.build(&dir) {
println!("Error: {}", e);
}
} }
fn watch(args: Vec<String>) { fn watch(args: Vec<String>) {

View File

@ -1,6 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::{self, File, metadata}; use std::fs::{self, File, metadata};
use std::io::Write; use std::io::Write;
use std::io::{Error, ErrorKind};
//use std::error::Error;
pub struct MDBook { pub struct MDBook {
dest: PathBuf, dest: PathBuf,
@ -8,6 +10,7 @@ pub struct MDBook {
} }
impl MDBook { impl MDBook {
pub fn new() -> Self { pub fn new() -> Self {
MDBook { MDBook {
dest: PathBuf::from("book"), dest: PathBuf::from("book"),
@ -15,11 +18,11 @@ impl MDBook {
} }
} }
pub fn init(&self, dir: &PathBuf) -> Result<(),&str> { pub fn init(&self, dir: &PathBuf) -> Result<(), Error> {
// Hacky way to check if the directory exists... Until PathExt moves to stable // Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(dir) { match metadata(dir) {
Err(_) => return Err("Destination path does not exist"), Err(_) => return Err(Error::new(ErrorKind::Other, "Directory does not exist")),
_ => {} _ => {}
} }
@ -69,17 +72,22 @@ impl MDBook {
}; };
if let Ok(mut f) = summary { if let Ok(mut f) = summary {
writeln!(f, "# Summary"); try!(writeln!(f, "# Summary"));
writeln!(f, ""); try!(writeln!(f, ""));
writeln!(f, "[Chapter 1](./chapter_1.md)"); try!(writeln!(f, "[Chapter 1](./chapter_1.md)"));
let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap(); let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap();
writeln!(chapter_1, "# Chapter 1"); try!(writeln!(chapter_1, "# Chapter 1"));
} }
return Ok(()); return Ok(());
} }
pub fn build(&self, dir: &PathBuf) -> Result<(), Error> {
Ok(())
}
pub fn set_dest(mut self, dest: PathBuf) -> Self { pub fn set_dest(mut self, dest: PathBuf) -> Self {
self.dest = dest; self.dest = dest;
self self
@ -89,4 +97,5 @@ impl MDBook {
self.src = src; self.src = src;
self self
} }
} }