cli: Refactor to make tests better.
This commit is contained in:
parent
4eb0dd1af3
commit
379dbe597d
|
@ -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);
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::errors::RookeriesError;
|
use crate::{cli::success_message, errors::RookeriesError};
|
||||||
use crate::success_message;
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use copy_dir::copy_dir;
|
use copy_dir::copy_dir;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
|
|
21
src/lib.rs
21
src/lib.rs
|
@ -10,13 +10,12 @@ extern crate serde_json;
|
||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
extern crate zip;
|
extern crate zip;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, default::Default};
|
||||||
use std::default::Default;
|
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use colored::Colorize;
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub mod cli;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod files;
|
pub mod files;
|
||||||
pub mod template;
|
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);
|
|
||||||
}
|
|
||||||
|
|
94
src/main.rs
94
src/main.rs
|
@ -1,5 +1,3 @@
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate copy_dir;
|
extern crate copy_dir;
|
||||||
extern crate docopt;
|
extern crate docopt;
|
||||||
|
@ -16,84 +14,36 @@ extern crate toml;
|
||||||
|
|
||||||
extern crate rookeries;
|
extern crate rookeries;
|
||||||
|
|
||||||
use actix_web::middleware::Logger;
|
use actix_web::{middleware::Logger, App, HttpServer};
|
||||||
use actix_web::{App, HttpServer};
|
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use docopt::Docopt;
|
use docopt::Docopt;
|
||||||
use env_logger::{Builder, Env};
|
|
||||||
use rookeries::errors::{fatal_error, RookeriesError};
|
use rookeries::{
|
||||||
use rookeries::files::{
|
cli::caution_message,
|
||||||
build_path, copy_directory, copy_file, create_directory, get_rookeries_home_cache_dir, FileType,
|
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 serde_json::Value;
|
||||||
use std::collections::HashMap;
|
use std::{
|
||||||
use std::ffi::OsString;
|
collections::HashMap,
|
||||||
use std::fs::{read_dir, read_to_string, remove_dir_all, write};
|
ffi::OsString,
|
||||||
use std::path::{Path, PathBuf};
|
fs::{read_dir, read_to_string, remove_dir_all, write},
|
||||||
use std::process::Command;
|
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 = "???";
|
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> {
|
fn get_project_directory(args: &Args) -> std::result::Result<PathBuf, RookeriesError> {
|
||||||
let project_directory = match &args.arg_project {
|
let project_directory = match &args.arg_project {
|
||||||
None => String::from("."),
|
None => String::from("."),
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::files::FileType;
|
use crate::files::FileType;
|
||||||
use crate::{
|
use crate::{
|
||||||
caution_message, details_message, errors::RookeriesError, files::get_rookeries_home_cache_dir,
|
cli::{caution_message, details_message, header_message, success_message},
|
||||||
header_message, success_message,
|
errors::RookeriesError,
|
||||||
|
files::get_rookeries_home_cache_dir,
|
||||||
};
|
};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
|
@ -27,27 +27,7 @@ fn test_version_message_returned() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_help_screen_returned() {
|
fn test_help_screen_returned() {
|
||||||
// TODO: Breakout usage string into a cli library module.
|
let expected_output = rookeries::cli::USAGE.trim_start().to_string();
|
||||||
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 actual_output = Command::new("cargo")
|
let actual_output = Command::new("cargo")
|
||||||
.args(&["run", "--", "--help"])
|
.args(&["run", "--", "--help"])
|
||||||
|
|
Loading…
Reference in New Issue