cli: Refactor to make tests better.

This commit is contained in:
Dorian 2019-10-07 08:45:24 -04:00
parent 4eb0dd1af3
commit 379dbe597d
6 changed files with 105 additions and 116 deletions

76
src/cli.rs Normal file
View File

@ -0,0 +1,76 @@
use colored::Colorize;
use env_logger::{Builder, Env};
pub const USAGE: &str = "
Usage:
rookeries (-h | --help)
rookeries --version
rookeries init [<project>] [-q | --quiet | -v | --verbose]
rookeries build [<project>] [-q | --quiet | -v | --verbose]
rookeries serve [<project>] [--port=<port>] [-q | --quiet | -v | --verbose]
Options:
-h --help Show this help screen.
--version Show version of the app.
--port=<port> The port number to serve a built site on [defaults: 8000].
-q, --quiet No output printed to console.
-v, --verbose Use verbose output.
Commands:
init [<project>] Initialize a project in the selected directory [defaults: .].
build [<project>] Builds the project into a static site [defaults: .].
serve [<project>] Serves the static site built from the project [defaults: .].
";
const ROOKERIES_LOG_ENV: &str = "ROOKERIES_LOG_LEVEL";
const ROOKERIES_LOG_DEFAULT: &str = "error,rookeries=info,actix_web=info";
const ROOKERIES_LOG_VERBOSE: &str = "debug,rookeries=debug,actix_web=debug";
#[derive(Debug, Deserialize)]
pub struct Args {
pub flag_version: bool,
pub arg_project: Option<String>,
pub cmd_build: bool,
pub cmd_init: bool,
pub cmd_serve: bool,
pub flag_port: u16,
pub flag_quiet: bool,
pub flag_verbose: bool,
}
pub fn initialize_logging(args: &Args) {
if args.flag_quiet {
return;
}
let logging_defaults = if args.flag_verbose {
ROOKERIES_LOG_VERBOSE
} else {
ROOKERIES_LOG_DEFAULT
};
let env = Env::default().filter_or(ROOKERIES_LOG_ENV, logging_defaults);
let mut builder = Builder::from_env(env);
builder
.default_format_level(false)
.default_format_timestamp(false)
.default_format_module_path(false);
builder.init();
}
pub fn header_message(message: &str) {
info!("");
info!("{} {}", "".bright_blue(), message);
}
pub fn success_message(message: &str) {
info!(" {} {}", "".green(), message);
}
pub fn caution_message(message: &str) {
info!(" {} {}", "".yellow(), message);
}
pub fn details_message(message: &str) {
debug!(" {} {}", "".yellow(), message);
}

View File

@ -1,5 +1,4 @@
use crate::errors::RookeriesError;
use crate::success_message;
use crate::{cli::success_message, errors::RookeriesError};
use colored::Colorize;
use copy_dir::copy_dir;
use directories::ProjectDirs;

View File

@ -10,13 +10,12 @@ extern crate serde_json;
extern crate uuid;
extern crate zip;
use std::collections::HashMap;
use std::default::Default;
use std::{collections::HashMap, default::Default};
use chrono::{DateTime, Utc};
use colored::Colorize;
use serde_json::Value;
pub mod cli;
pub mod errors;
pub mod files;
pub mod template;
@ -57,19 +56,3 @@ impl Default for Site {
}
}
}
pub fn header_message(message: &str) {
info!("");
info!("{} {}", "".bright_blue(), message);
}
pub fn success_message(message: &str) {
info!(" {} {}", "".green(), message);
}
pub fn caution_message(message: &str) {
info!(" {} {}", "".yellow(), message);
}
pub fn details_message(message: &str) {
debug!(" {} {}", "".yellow(), message);
}

View File

@ -1,5 +1,3 @@
#[macro_use]
extern crate serde_derive;
extern crate chrono;
extern crate copy_dir;
extern crate docopt;
@ -16,84 +14,36 @@ extern crate toml;
extern crate rookeries;
use actix_web::middleware::Logger;
use actix_web::{App, HttpServer};
use actix_web::{middleware::Logger, App, HttpServer};
use chrono::prelude::*;
use colored::Colorize;
use docopt::Docopt;
use env_logger::{Builder, Env};
use rookeries::errors::{fatal_error, RookeriesError};
use rookeries::files::{
build_path, copy_directory, copy_file, create_directory, get_rookeries_home_cache_dir, FileType,
use rookeries::{
cli::caution_message,
cli::header_message,
cli::initialize_logging,
cli::success_message,
cli::Args,
cli::USAGE,
errors::{fatal_error, RookeriesError},
files::{
build_path, copy_directory, copy_file, create_directory, get_rookeries_home_cache_dir,
FileType,
},
template, Page, Site,
};
use rookeries::template;
use rookeries::{caution_message, header_message, success_message, Page, Site};
use serde_json::Value;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::{read_dir, read_to_string, remove_dir_all, write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{
collections::HashMap,
ffi::OsString,
fs::{read_dir, read_to_string, remove_dir_all, write},
path::{Path, PathBuf},
process::Command,
};
const USAGE: &str = "
Usage:
rookeries (-h | --help)
rookeries --version
rookeries init [<project>] [-q | --quiet | -v | --verbose]
rookeries build [<project>] [-q | --quiet | -v | --verbose]
rookeries serve [<project>] [--port=<port>] [-q | --quiet | -v | --verbose]
Options:
-h --help Show this help screen.
--version Show version of the app.
--port=<port> The port number to serve a built site on [defaults: 8000].
-q, --quiet No output printed to console.
-v, --verbose Use verbose output.
Commands:
init [<project>] Initialize a project in the selected directory [defaults: .].
build [<project>] Builds the project into a static site [defaults: .].
serve [<project>] Serves the static site built from the project [defaults: .].
";
const ROOKERIES_LOG_ENV: &str = "ROOKERIES_LOG_LEVEL";
const ROOKERIES_LOG_DEFAULT: &str = "error,rookeries=info,actix_web=info";
const ROOKERIES_LOG_VERBOSE: &str = "debug,rookeries=debug,actix_web=debug";
const ROOKERIES_UNKNOWN_PLACEHOLDER: &str = "???";
#[derive(Debug, Deserialize)]
struct Args {
flag_version: bool,
arg_project: Option<String>,
cmd_build: bool,
cmd_init: bool,
cmd_serve: bool,
flag_port: u16,
flag_quiet: bool,
flag_verbose: bool,
}
fn initialize_logging(args: &Args) {
if args.flag_quiet {
return;
}
let logging_defaults = if args.flag_verbose {
ROOKERIES_LOG_VERBOSE
} else {
ROOKERIES_LOG_DEFAULT
};
let env = Env::default().filter_or(ROOKERIES_LOG_ENV, logging_defaults);
let mut builder = Builder::from_env(env);
builder
.default_format_level(false)
.default_format_timestamp(false)
.default_format_module_path(false);
builder.init();
}
fn get_project_directory(args: &Args) -> std::result::Result<PathBuf, RookeriesError> {
let project_directory = match &args.arg_project {
None => String::from("."),

View File

@ -1,7 +1,8 @@
use crate::files::FileType;
use crate::{
caution_message, details_message, errors::RookeriesError, files::get_rookeries_home_cache_dir,
header_message, success_message,
cli::{caution_message, details_message, header_message, success_message},
errors::RookeriesError,
files::get_rookeries_home_cache_dir,
};
use colored::Colorize;
use std::fs;

View File

@ -27,27 +27,7 @@ fn test_version_message_returned() {
#[test]
fn test_help_screen_returned() {
// TODO: Breakout usage string into a cli library module.
let expected_output = "Usage:
rookeries (-h | --help)
rookeries --version
rookeries init [<project>] [-q | --quiet | -v | --verbose]
rookeries build [<project>] [-q | --quiet | -v | --verbose]
rookeries serve [<project>] [--port=<port>] [-q | --quiet | -v | --verbose]
Options:
-h --help Show this help screen.
--version Show version of the app.
--port=<port> The port number to serve a built site on [defaults: 8000].
-q, --quiet No output printed to console.
-v, --verbose Use verbose output.
Commands:
init [<project>] Initialize a project in the selected directory [defaults: .].
build [<project>] Builds the project into a static site [defaults: .].
serve [<project>] Serves the static site built from the project [defaults: .].
"
.to_string();
let expected_output = rookeries::cli::USAGE.trim_start().to_string();
let actual_output = Command::new("cargo")
.args(&["run", "--", "--help"])