From b7cb6f89f137e20f1d6dbb1f66e40f319c978a45 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 8 Mar 2023 11:27:51 +0800 Subject: fix: repl set save true not work if not rerun(#34) --- src/config.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index dc07c95..8ac99b7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -103,29 +103,14 @@ impl Config { Ok(path) } - pub fn open_message_file(&self) -> Result> { + pub fn save_message(&self, input: &str, output: &str) -> Result<()> { if !self.save { - return Ok(None); + return Ok(()); } - let path = Config::messages_file()?; - let file: Option = if self.save { - let file = OpenOptions::new() - .create(true) - .append(true) - .open(&path) - .with_context(|| format!("Failed to create/append {}", path.display()))?; - Some(file) - } else { - None - }; - Ok(file) - } - - pub fn save_message(&self, file: Option<&mut File>, input: &str, output: &str) -> Result<()> { - if output.is_empty() || !self.save || file.is_none() { + let mut file = self.open_message_file()?; + if output.is_empty() || !self.save { return Ok(()); } - let file = file.unwrap(); let timestamp = now(); let output = match self.role.as_ref() { None => { @@ -261,7 +246,7 @@ impl Config { match key { "api_key" => { if unset { - return Ok("Not allowed".into()); + return Ok("Error: Not allowed".into()); } else { self.api_key = value.to_string(); } @@ -298,6 +283,15 @@ impl Config { Ok("".into()) } + fn open_message_file(&self) -> Result { + let path = Config::messages_file()?; + OpenOptions::new() + .create(true) + .append(true) + .open(&path) + .with_context(|| format!("Failed to create/append {}", path.display())) + } + fn load_roles(&mut self) -> Result<()> { let path = Self::roles_file()?; if !path.exists() { -- cgit v1.2.3