diff --git a/src/main.rs b/src/main.rs index d0941fd..2a1f48f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,14 +8,36 @@ mod repl; mod skey; mod structs; +use rpassword::prompt_password; use rustyline::Editor; use std::{cell::RefCell, rc::Rc}; use crate::lk::LK; -use crate::repl::LKRead; +use crate::parser::command_parser; +use crate::repl::{LKEval, LKRead}; +use crate::structs::{Command, LKErr, INIT_FILE}; pub fn main() { let lk = Rc::new(RefCell::new(LK::new())); + + match std::fs::read_to_string(INIT_FILE.as_path().to_str().unwrap()) { + Ok(script) => match command_parser::script(&script) { + Ok(cmd_list) => { + for cmd in cmd_list { + LKEval::new(cmd, lk.clone(), prompt_password).eval().print(); + } + } + Err(err) => { + LKEval::new(Command::Error(LKErr::ParseError(err)), lk.clone(), prompt_password).eval().print(); + } + }, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), + Err(err) => { + LKEval::new(Command::Error(LKErr::Error(format!("Failed to read init file {:?}: {}", INIT_FILE.as_path(), err).as_str())), lk.clone(), prompt_password) + .eval() + .print(); + } + } let mut lkread = LKRead::new(Editor::<()>::new().unwrap(), String::from("❯ "), lk.clone()); while lkread.read().eval().print() { diff --git a/src/repl.rs b/src/repl.rs index 3e8ec06..629ab8e 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -1,4 +1,3 @@ -use home::home_dir; use regex::Regex; use rpassword::prompt_password; use rustyline::error::ReadlineError; @@ -8,7 +7,7 @@ use std::{cell::RefCell, rc::Rc}; use crate::lk::LK; use crate::parser::command_parser; use crate::password::{fix_password_recursion, PasswordRef}; -use crate::structs::{Command, LKErr, Radix}; +use crate::structs::{Command, LKErr, Radix, HISTORY_FILE}; #[derive(Debug)] pub struct LKRead { @@ -45,8 +44,7 @@ impl LKRead { } pub fn read(&mut self) -> LKEval { - let history_file_path = home_dir().unwrap().join(".lesskey_history"); - let history_file = history_file_path.as_path().to_str().unwrap(); + let history_file = HISTORY_FILE.as_path().to_str().unwrap(); self.rl.clear_history(); match self.rl.load_history(&history_file) { Ok(_) => (), diff --git a/src/structs.rs b/src/structs.rs index 7559e01..022e9a2 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,5 +1,12 @@ use crate::password::{Comment, Name, PasswordRef}; +use home::home_dir; use std::fmt; +use std::path::PathBuf; + +lazy_static! { + pub static ref HISTORY_FILE: PathBuf = home_dir().unwrap().join(".lesskey_history"); + pub static ref INIT_FILE: PathBuf = home_dir().unwrap().join(".lesskeyrc"); +} #[derive(thiserror::Error, Debug, PartialEq)] pub enum LKErr<'a> {