diff --git a/src/config.rs b/src/config.rs index 644acb8..c671f3a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,6 @@ -use std::{env, error::Error, fs, io, path::PathBuf}; +use crate::{RED, BLUE, WARN, RESET}; + +use std::{env, error::Error, fs, io, path::PathBuf, process::exit}; use home::home_dir; use serde::{Deserialize, Serialize}; @@ -48,6 +50,23 @@ impl Default for Config { } impl Config { + + pub fn set_tos(&mut self, tos: bool) -> Result<(), Box> { + self.tos = tos; + self.save()?; + Ok(()) + } + + pub fn check_tos(&self) -> () { + if ! self.tos { + eprintln!("{RED}You need to agree to duckduckgo AI chat TOS to continue.{RESET}"); + eprintln!("{RED}Visit it on this URL: {RESET}{BLUE}https://duckduckgo.com/?q=duckduckgo+ai+chat&ia=chat{RESET}"); + eprintln!("Once you read it, pass --agree-tos parameter to agree."); + eprintln!("{WARN}Note: if you want to, modify `tos` parameter in {}{RESET}", Config::get_path::().join(Config::get_file_name::()).display()); + exit(3); + } + } + pub fn get_path>() -> T { match env::var("HEY_CONFIG_PATH") { Ok(v) => v, diff --git a/src/main.rs b/src/main.rs index 223d4d9..e0f0234 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,18 +47,11 @@ async fn main() { if ! config.tos { println!("{GREEN}TOS accepted{RESET}"); } - config.tos = true; - config.save().expect("Error saving config"); + config.set_tos(true).expect("Error saving config"); exit(0); } - if ! config.tos { - eprintln!("{RED}You need to agree to duckduckgo AI chat TOS to continue.{RESET}"); - eprintln!("{RED}Visit it on this URL: {RESET}{BLUE}https://duckduckgo.com/?q=duckduckgo+ai+chat&ia=chat{RESET}"); - eprintln!("Once you read it, pass --agree-tos parameter to agree."); - eprintln!("{WARN}Note: if you want to, modify `tos` parameter in {}{RESET}", Config::get_path::().join(Config::get_file_name::()).display()); - exit(3); - } + config.check_tos(); println!("{GREEN}Contacting DuckDuckGo chat AI...{RESET}");