Added command-line options

This commit is contained in:
Sofia 2018-04-16 19:31:38 +03:00
parent 933189b7b2
commit 9e84a2454d
6 changed files with 97 additions and 29 deletions

View File

@ -12,17 +12,17 @@ css = []
#google_robots = "all"
#google_site_verification = ""
#[navbar]
#items = ["about", "other", "google"]
[navbar]
items = ["about", "other", "google"]
# [navbar.item.about]
# title = "About"
# link = "/"
[navbar.item.about]
title = "About"
link = "/"
# [navbar.item.other]
# title = "Other"
# link = "/other"
[navbar.item.other]
title = "Other"
link = "/other"
# [navbar.item.google]
# title = "Google!"
# link = "https://google.com"
[navbar.item.google]
title = "Google!"
link = "https://google.com"

View File

@ -3,6 +3,7 @@ use template::Template;
use renderer;
use logger::{LogLevel, Logger};
use error::Error;
use options::BuildOps;
use std::fs::{create_dir_all, File};
use std::io::prelude::*;
@ -20,7 +21,11 @@ fn fetch_config() -> Result<Config, Error> {
}
}
fn write_file<T: Into<String>, U: Into<String>>(path_str: T, content: U) -> Result<(), Error> {
fn write_file<T: Into<String>, U: Into<String>>(
path_str: T,
content: U,
overwrite: bool,
) -> Result<(), Error> {
let path_str = path_str.into();
let content = content.into();
@ -35,7 +40,7 @@ fn write_file<T: Into<String>, U: Into<String>>(path_str: T, content: U) -> Resu
format!("Could not find parent folder or file name for {}", path_str),
));
}
if path.exists() {
if path.exists() && !overwrite {
Err(Error::new(
LogLevel::SEVERE,
format!(
@ -51,14 +56,14 @@ fn write_file<T: Into<String>, U: Into<String>>(path_str: T, content: U) -> Resu
}
}
pub fn build(logger: &Logger) -> Result<(), Error> {
pub fn build(logger: &Logger, ops: &BuildOps) -> Result<(), Error> {
logger.log(LogLevel::INFO, "Starting build");
let config = fetch_config()?;
if config.global_config.website.use_default_css {
let css_path = "css/default.css";
logger.log(LogLevel::DETAIL, format!("Adding public/{}", css_path));
write_file(css_path, DEFAULT_CSS)?;
write_file(css_path, DEFAULT_CSS, ops.overwrite)?;
}
logger.log(LogLevel::INFO, "Generating page templates");
@ -92,7 +97,7 @@ pub fn build(logger: &Logger) -> Result<(), Error> {
let html_path = config.clone().page_config.page.html_path;
logger.log(LogLevel::DETAILER, format!("Writing public/{}", html_path));
if let Some(render) = renders.get(idx) {
write_file(html_path, render.clone())?;
write_file(html_path, render.clone(), ops.overwrite)?;
} else {
return Err(Error::new(
LogLevel::SEVERE,

View File

@ -10,6 +10,7 @@ impl Logger {
pub fn log<T: Into<String>>(&self, log_level: LogLevel, message: T) {
if self.log_level.clone() as i32 >= log_level.clone() as i32 {
match log_level {
LogLevel::QUIET => (),
LogLevel::SEVERE => eprintln!("[SEVERE]: {}", message.into()),
LogLevel::INFO => println!("[INFO]: {}", message.into()),
LogLevel::WARNING => eprintln!("[WARNING]: {}", message.into()),
@ -22,10 +23,25 @@ impl Logger {
#[allow(dead_code)]
#[derive(Clone)]
#[repr(u8)]
pub enum LogLevel {
SEVERE = 0,
INFO = 1,
WARNING = 2, // Default
DETAIL = 3,
DETAILER = 4,
QUIET = 0,
SEVERE = 1,
INFO = 2,
WARNING = 3, // Default
DETAIL = 4,
DETAILER = 5,
}
impl From<u8> for LogLevel {
fn from(number: u8) -> LogLevel {
match number {
0 => LogLevel::QUIET,
1 => LogLevel::SEVERE,
2 => LogLevel::INFO,
4 => LogLevel::DETAIL,
5 => LogLevel::DETAILER,
_ => LogLevel::WARNING, // 3, also fallback
}
}
}

View File

@ -2,9 +2,12 @@ extern crate pulldown_cmark;
extern crate regex;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate structopt;
extern crate toml;
mod logger;
mod options;
mod error;
mod config_toml;
mod config;
@ -12,16 +15,25 @@ mod template;
mod renderer;
mod builder;
use structopt::StructOpt;
use logger::{LogLevel, Logger};
use options::{Opt, Subcommands};
fn main() {
let logger = Logger::new(LogLevel::DETAILER);
let opt = Opt::from_args();
match builder::build(&logger) {
Ok(_) => logger.log(LogLevel::DETAILER, "Building finished successfully."),
Err(err) => {
logger.log(err.severity(), err.description());
logger.log(LogLevel::SEVERE, "Aborting building due to error.");
}
let log_level = (3 - opt.quiet as i8 + opt.verbose as i8).max(0).min(5);
let logger = Logger::new(LogLevel::from(log_level as u8));
match opt.cmd {
Subcommands::Build(build) => match builder::build(&logger, &build) {
Ok(_) => logger.log(LogLevel::DETAILER, "Building finished successfully."),
Err(err) => {
logger.log(err.severity(), err.description());
logger.log(LogLevel::SEVERE, "Aborting building due to error.");
}
},
Subcommands::NewPage => println!("Unsupported!"),
}
}

32
src/options.rs Normal file
View File

@ -0,0 +1,32 @@
#[derive(StructOpt, Debug)]
#[structopt(name = "Teasca.de Generator",
about = "A website generator, used mainly for generating teasca.de")]
pub struct Opt {
/// Verbose flag, makes the program more noisy. (-vv for maximum noisiness)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
pub verbose: u8,
/// Quiet flag, makes the program less noisy. (-qqq for minimum noisiness)
#[structopt(short = "q", long = "quiet", parse(from_occurrences))]
pub quiet: u8,
#[structopt(subcommand)]
pub cmd: Subcommands,
}
#[derive(StructOpt, Debug)]
pub enum Subcommands {
/// Build the website
#[structopt(name = "build")]
Build(BuildOps),
/// Create a new page
#[structopt(name = "new-page")]
NewPage,
}
#[derive(StructOpt, Debug)]
pub struct BuildOps {
/// Overwrites necessary files to create new ones.
#[structopt(short = "o", long = "overwrite")]
pub overwrite: bool,
}

View File

@ -1,2 +1,5 @@
# OTher
This page is not like the others
This page is not like the others
Example stuff that makes this site different:
- This list