From 17ee12a7f900e6419f56a90222cb282a905a3d5d Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 3 Mar 2023 10:24:27 +0800 Subject: feat: support initial configuration items --- src/main.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0f19adf..5d28b73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -356,17 +356,38 @@ fn init_runtime() -> Result { } fn create_config_file(config_path: &Path) -> Result<()> { + let confirm_map_err = |_| anyhow!("Error with questionnaire, try again later"); + let text_map_err = |_| anyhow!("An error happened when asking for your key, try again later."); let ans = Confirm::new("No config file, create a new one?") .with_default(true) .prompt() - .map_err(|_| anyhow!("Error with questionnaire, try again later"))?; + .map_err(confirm_map_err)?; if !ans { exit(0); } let api_key = Text::new("Openai API Key:") .prompt() - .map_err(|_| anyhow!("An error happened when asking for your key, try again later."))?; - std::fs::write(config_path, format!("api_key = \"{api_key}\"\n")) + .map_err(text_map_err)?; + let mut raw_config = format!("api_key: {api_key}\n"); + + let ans = Confirm::new("Use proxy?") + .with_default(false) + .prompt() + .map_err(confirm_map_err)?; + if ans { + let proxy = Text::new("Set proxy:").prompt().map_err(text_map_err)?; + raw_config.push_str(&format!("proxy: {proxy}\n")); + } + + let ans = Confirm::new("Save chat messages") + .with_default(false) + .prompt() + .map_err(confirm_map_err)?; + if ans { + raw_config.push_str("save: true\n"); + } + + std::fs::write(config_path, raw_config) .map_err(|err| anyhow!("Failed to write to config file, {err}"))?; Ok(()) } -- cgit v1.2.3