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 getopts;
use std::env;
use std::path::PathBuf;
use mdbook::MDBook;
@ -112,7 +113,12 @@ fn init(args: Vec<String>) {
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();
if let Err(e) = book.init(&dir) {
@ -140,6 +146,13 @@ fn build(args: Vec<String>) {
if matches.opt_present("help") {
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>) {

View File

@ -1,6 +1,8 @@
use std::path::PathBuf;
use std::fs::{self, File, metadata};
use std::io::Write;
use std::io::{Error, ErrorKind};
//use std::error::Error;
pub struct MDBook {
dest: PathBuf,
@ -8,6 +10,7 @@ pub struct MDBook {
}
impl MDBook {
pub fn new() -> Self {
MDBook {
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
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 {
writeln!(f, "# Summary");
writeln!(f, "");
writeln!(f, "[Chapter 1](./chapter_1.md)");
try!(writeln!(f, "# Summary"));
try!(writeln!(f, ""));
try!(writeln!(f, "[Chapter 1](./chapter_1.md)"));
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(());
}
pub fn build(&self, dir: &PathBuf) -> Result<(), Error> {
Ok(())
}
pub fn set_dest(mut self, dest: PathBuf) -> Self {
self.dest = dest;
self
@ -89,4 +97,5 @@ impl MDBook {
self.src = src;
self
}
}