Add init call to create config.toml

This commit is contained in:
Sofia 2018-04-17 01:17:47 +03:00
parent 5f2b40ad43
commit ada105fa6f
3 changed files with 103 additions and 12 deletions

View File

@ -1,21 +1,17 @@
[website]
website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"]
website_name = "Test Website"
built_pages = []
use_default_css = true
javascript = []
css = []
[navbar]
items = ["about", "other", "google"]
[navbar.item.google]
title = "Google!"
link = "https://google.com"
items = ["test", "other"]
[navbar.item.test]
title = "Home"
link = "/home.html"
image_url = "/img/image.png"
[navbar.item.other]
title = "Other"
link = "/other"
[navbar.item.about]
title = "About"
link = "/"
link = "/other.html"

View File

@ -19,10 +19,15 @@ mod builder;
mod new_page;
mod file_writer;
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;
use logger::{LogLevel, Logger};
use options::{Opt, Subcommands};
use config_toml::{GlobalConfigToml, NavbarConfig, NavbarItem, WebsiteConfig};
fn main() {
let opt = Opt::from_args();
@ -48,5 +53,73 @@ fn main() {
logger.log(LogLevel::SEVERE, "Aborting building due to error.");
}
},
Subcommands::Initialize(ops) => {
logger.log(LogLevel::DETAIL, "Generating config.toml");
let mut navbar = None;
if !ops.no_default_navbar {
let mut hashmap = HashMap::new();
hashmap.insert(
"test".to_owned(),
NavbarItem {
title: "Home".to_owned(),
link: "/home.html".to_owned(),
image_url: Some("/img/image.png".to_owned()),
},
);
hashmap.insert(
"other".to_owned(),
NavbarItem {
title: "Other".to_owned(),
link: "/other.html".to_owned(),
image_url: None,
},
);
navbar = Some(NavbarConfig {
items: vec!["test".to_owned(), "other".to_owned()],
item_map: hashmap,
})
}
let config = GlobalConfigToml {
website: WebsiteConfig {
website_name: ops.name,
built_pages: Vec::new(),
use_default_css: true,
javascript: Vec::new(),
css: Vec::new(),
favicon: ops.favicon,
before_navbar_url: None,
before_content_url: None,
after_content_url: None,
twitter_author: ops.twitter_author,
google_robots: None,
google_site_verification: None,
},
navbar: navbar,
};
match toml::ser::to_string(&config) {
Ok(text) => {
match file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)
{
Ok(_) => logger.log(LogLevel::INFO, "config.toml successfully generated"),
Err(err) => logger.log(
LogLevel::SEVERE,
format!(
"Failed to write config.toml: {}",
err.description().to_owned()
),
),
}
}
Err(err) => logger.log(
LogLevel::SEVERE,
format!(
"Failed to serialize config.toml: {}",
err.description().to_owned()
),
),
}
}
}
}

View File

@ -24,6 +24,9 @@ pub enum Subcommands {
/// Create a new page
#[structopt(name = "new")]
New(NewOps),
/// Initializes a config.toml
#[structopt(name = "init")]
Initialize(InitializeOps),
}
#[derive(StructOpt, Debug)]
@ -54,3 +57,22 @@ pub struct NewOps {
#[structopt(short = "n", long = "no-modify-config")]
pub no_modify_config: bool,
}
#[derive(StructOpt, Debug)]
pub struct InitializeOps {
/// Name of the webpage
#[structopt()]
pub name: String,
/// Favicon
#[structopt(short = "f", long = "favicon")]
pub favicon: Option<String>,
/// Twitter author
#[structopt(short = "t", long = "twitter")]
pub twitter_author: Option<String>,
/// Overwrites existing config.toml
#[structopt(short = "o", long = "overwrite")]
pub overwrite: bool,
/// Disables the default navbar
#[structopt(short = "n", long = "no-navbar")]
pub no_default_navbar: bool,
}