diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-03 10:24:27 +0800 |
|---|---|---|
| committer | sigoden <sigoden@gmail.com> | 2023-03-03 10:24:27 +0800 |
| commit | 17ee12a7f900e6419f56a90222cb282a905a3d5d (patch) | |
| tree | 563baa17f519bb8349d298b29aa3ed6755ca17c9 /src/main.rs | |
| parent | 627a2d08ce0932241a21a281712e58427c15d478 (diff) | |
| download | aichat-17ee12a7f900e6419f56a90222cb282a905a3d5d.tar.gz | |
feat: support initial configuration items
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 27 |
1 files 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<Runtime> { } 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(()) } |
