use std::env;
use anyhow::Context;
use anyhow::Result;
use console::style;
use tracing::debug;
use crate::cli::Cli;
use crate::cli::Command;
mod cli;
fn main() -> Result<()> {
let dotenv = dotenvy::dotenv();
let key = "RUST_LOG";
if env::var(key).is_err() {
env::set_var(key, "info"); }
tracing_subscriber::fmt::init();
match dotenv {
Ok(pb) => {
debug!("`.env` file loaded: {:?}", pb);
}
Err(e) => {
debug!("`.env` file not found or not readable: {}", e);
}
}
let Cli {
command: cmd,
global_opts,
} = cli::parse_arguments();
let config = cli::config::init(global_opts)?;
debug!("{:?}", config);
match cmd {
Command::RefDefs(subcmd) => {
cli::refdefs_commands::run(subcmd, config)?;
}
Command::Links(subcmd) => {
cli::links_commands::run(subcmd, config)?;
}
Command::Markdown(subcmd) => {
cli::markdown_commands::run(subcmd, config)?;
}
Command::SiteMap(args) => {
let markdown_src_dir_path = config.markdown_src_dir_path(args.src, "./src/")?;
let base_url = config.base_url(args.base)?;
let sitemap_dest_file_path = config.sitemap_file_path(args.dest);
println!(
"Generating {} from the list of Markdown files in {}...",
style(sitemap_dest_file_path.display()).cyan(),
style(markdown_src_dir_path.display()).cyan(),
);
mdbook_utils::generate_sitemap(markdown_src_dir_path, base_url, sitemap_dest_file_path)
.context("[main] Failed to generate the sitemap.")?;
println!("{}", style("Done.").green());
}
Command::Debug(args) => {
let markdown_src_dir_path = config.markdown_src_dir_path(args.src, "./src/")?;
let log_dest_path = config.dest_file_path(args.dest, "debug.log");
println!(
"Parsing Markdown files in {} and writing raw events to {}...",
style(markdown_src_dir_path.display()).cyan(),
style(log_dest_path.display()).cyan()
);
mdbook_utils::debug_parse_to(markdown_src_dir_path, log_dest_path)
.context("[main] Failed to generate the debug log.")?;
println!("{}", style("Done.").green());
}
Command::Test => {
mdbook_utils::test().context("[main] Failed to generate the test file.")?;
println!("{}", style("Done.").green());
}
}
Ok(())
}