Extracted `mdbook test` and `mdbook init` to separate files/modules

This commit is contained in:
Michal Budzynski 2017-06-26 01:24:33 +02:00
parent 35ed9fc286
commit f3c8535870
3 changed files with 46 additions and 39 deletions

View File

@ -1,9 +1,11 @@
use std::io;
use std::io::Write;
use std::error::Error;
use clap::{ArgMatches, SubCommand, App};
use mdbook::MDBook;
use {get_book_dir, confirm};
use get_book_dir;
// Init command implementation
pub fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
@ -57,6 +59,17 @@ pub fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
Ok(())
}
// Simple function that user comfirmation
fn confirm() -> bool {
io::stdout().flush().unwrap();
let mut s = String::new();
io::stdin().read_line(&mut s).ok();
match &*s.trim() {
"Y" | "y" | "yes" | "Yes" => true,
_ => false,
}
}
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("init")
.about("Create boilerplate structure and files in the directory")

View File

@ -6,22 +6,20 @@ extern crate env_logger;
extern crate open;
use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use clap::{App, ArgMatches, SubCommand, AppSettings};
use clap::{App, ArgMatches, AppSettings};
pub mod build;
pub mod init;
pub mod test;
#[cfg(feature = "serve")]
pub mod serve;
#[cfg(feature = "watch")]
pub mod watch;
use mdbook::MDBook;
const NAME: &'static str = "mdbook";
fn main() {
@ -29,16 +27,15 @@ fn main() {
// Create a list of valid arguments and sub-commands
let app = App::new(NAME)
.about("Create a book in form of a static website from markdown files")
.author("Mathieu David <mathieudavid@mathieudavid.org>")
// Get the version from our Cargo.toml using clap's crate_version!() macro
.version(concat!("v",crate_version!()))
.setting(AppSettings::SubcommandRequired)
.after_help("For more information about a specific command, try `mdbook <command> --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook")
.subcommand(init::make_subcommand())
.subcommand(build::make_subcommand())
.subcommand(SubCommand::with_name("test")
.about("Test that code samples compile"));
.about("Create a book in form of a static website from markdown files")
.author("Mathieu David <mathieudavid@mathieudavid.org>")
// Get the version from our Cargo.toml using clap's crate_version!() macro
.version(concat!("v",crate_version!()))
.setting(AppSettings::SubcommandRequired)
.after_help("For more information about a specific command, try `mdbook <command> --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook")
.subcommand(init::make_subcommand())
.subcommand(build::make_subcommand())
.subcommand(test::make_subcommand());
#[cfg(feature = "watch")]
let app = app.subcommand(watch::make_subcommand());
@ -53,7 +50,7 @@ fn main() {
("watch", Some(sub_matches)) => watch::watch(sub_matches),
#[cfg(feature = "serve")]
("serve", Some(sub_matches)) => serve::serve(sub_matches),
("test", Some(sub_matches)) => test(sub_matches),
("test", Some(sub_matches)) => test::test(sub_matches),
(_, _) => unreachable!(),
};
@ -63,29 +60,6 @@ fn main() {
}
}
// Simple function that user comfirmation
fn confirm() -> bool {
io::stdout().flush().unwrap();
let mut s = String::new();
io::stdin().read_line(&mut s).ok();
match &*s.trim() {
"Y" | "y" | "yes" | "Yes" => true,
_ => false,
}
}
fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
book.test()?;
Ok(())
}
fn get_book_dir(args: &ArgMatches) -> PathBuf {
if let Some(dir) = args.value_of("dir") {
// Check if path is relative from current dir, or absolute...

20
src/bin/test.rs Normal file
View File

@ -0,0 +1,20 @@
use std::error::Error;
use clap::{ArgMatches, SubCommand, App};
use mdbook::MDBook;
use get_book_dir;
// test command implementation
pub fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
book.test()?;
Ok(())
}
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("test").about("Test that code samples compile")
}