diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 11 | ||||
| -rw-r--r-- | src/display.rs | 62 | ||||
| -rw-r--r-- | src/main.rs | 22 |
3 files changed, 95 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..1f3a846 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,11 @@ +use std::path::{Path, PathBuf}; + +pub fn cookies_file() -> PathBuf { + let exe = std::env::current_exe().unwrap(); + exe.parent().unwrap_or(Path::new(".")).join("cookies.json") +} + +pub fn books_root() -> PathBuf { + let exe = std::env::current_exe().unwrap(); + exe.parent().unwrap_or(Path::new(".")).join("Books") +} diff --git a/src/display.rs b/src/display.rs new file mode 100644 index 0000000..6316f95 --- /dev/null +++ b/src/display.rs @@ -0,0 +1,62 @@ +use colored::*; +use std::{fs::File, path::PathBuf}; +use tracing::{error, info}; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +pub struct Display { + pub log_file: PathBuf, + pub output_dir: Option<PathBuf>, +} + +impl Display { + pub fn new(book_id: &str) -> Self { + let log_file = std::env::current_dir() + .unwrap() + .join(format!("info_{}.log", book_id)); + + let d = Self { + log_file, + output_dir: None, + }; + + let file = File::create(&d.log_file).expect("Cannot create log file"); + + tracing_subscriber::registry() + .with(EnvFilter::from_default_env()) + .with(fmt::layer().with_writer(std::io::stdout)) + .with(fmt::layer().with_writer(file).with_ansi(false)) + .init(); + + d.intro(); + info!("** Welcome to SafariBooks (Rust) **"); + d + } + + pub fn intro(&self) { + let banner = r#" + ____ __ _ ____ _ +/ ___| __ _ / _| __ _ _ __(_) _ \ _ _ ___| |_ +\___ \ / _` | |_ / _` | '__| | |_) | | | / __| __| + ___) | (_| | _| (_| | | | | _ <| |_| \__ \ |_ +|____/ \__,_|_| \__,_|_| |_|_| \_\\__,_|___/\__| +"#; + println!("{}", banner.yellow()); + println!("{}", "~".repeat(32)); + } + + pub fn info(&self, msg: &str) { + println!("{} {}", "[*]".yellow(), msg); + info!("{msg}"); + } + + pub fn error_and_exit(&self, msg: &str) -> ! { + eprintln!("{} {}", "[!]".on_red().white(), msg); + error!("{msg}"); + std::process::exit(1); + } + + pub fn set_output_dir(&mut self, dir: PathBuf) { + self.output_dir = Some(dir.clone()); + self.info(&format!("Output directory:\n {}", dir.display())); + } +} diff --git a/src/main.rs b/src/main.rs index 3b3e84d..ad33122 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,30 @@ mod cli; +mod config; +mod display; use clap::Parser; use cli::Args; +use display::Display; fn main() { let args = Args::parse(); + + let mut ui = Display::new(&args.bookid); + + let cookies = config::cookies_file(); + if !cookies.exists() { + ui.error_and_exit( + "cookies.json not found.\n\ + This version requires an existing authenticated session.", + ); + } + + ui.info(&format!("Using cookies file: {}", cookies.display())); + + let output_dir = config::books_root().join(format!("(pending) ({})", args.bookid)); + + ui.set_output_dir(output_dir); + + ui.info("Initialization complete."); + ui.info("No network operations performed in this version."); } |
