From 3316f6d8f021ddecb597691f2c799ddf5394ab2c Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Sat, 22 Apr 2023 18:37:06 +0100 Subject: [PATCH] Move saving api key to function --- src/main.rs | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index fc1ddb3..a97794e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,31 +17,7 @@ fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if let Some(Commands::Auth { api_key }) = cli.command { - let config_path = ProjectDirs::from("com", "supleed2", "omg") - .context("Unable to access app config directory (while saving API key).")? - .config_dir().join("config.toml"); - let _ = std::fs::create_dir_all(&config_path.parent().expect("Unable to get parent dir of config.toml")); - let Config { api_key: _, username } = read_to_string(&config_path).ok() - .and_then(|str| toml::from_str::(&str).ok()) - .unwrap_or_default(); - let toml_str = toml::to_string_pretty(&Config { - api_key: Some(api_key), - username, - }) - .expect("Unable to convert updated config to TOML (when trying to save API key)."); - match std::fs::write(&config_path, toml_str) { - Ok(_) => { - println!("Saved API key to config file: {}", config_path.display()); - std::process::exit(0); - } - Err(_) => { - eprintln!( - "Failed to save API key to config file: {}", - config_path.display() - ); - std::process::exit(1); - } - }; + save_api_key(&api_key)?; } let config = ProjectDirs::from("com", "supleed2", "omg") @@ -64,5 +40,30 @@ fn main() -> anyhow::Result<()> { Ok(()) } +fn save_api_key(api_key: &str) -> anyhow::Result<()> { + let config_path = ProjectDirs::from("com", "supleed2", "omg") + .context("Unable to access app config directory (while saving API key).")? + .config_dir() + .join("config.toml"); + let _ = std::fs::create_dir_all( + &config_path + .parent() + .expect("Unable to get parent dir of config.toml"), + ); + let Config { + api_key: _, + username, + } = read_to_string(&config_path) + .ok() + .and_then(|str| toml::from_str::(&str).ok()) + .unwrap_or_default(); + let toml_str = toml::to_string_pretty(&Config { + api_key: Some(api_key.to_string()), + username, + }) + .expect("Unable to convert updated config to TOML (when trying to save API key)."); + std::fs::write(&config_path, toml_str).context("Failed to save API key to config file") +} + // Tutorial at: https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html // More info at: https://docs.rs/clap/latest/clap/_derive/index.html