Move saving api key to function

This commit is contained in:
Aadi Desai 2023-04-22 18:37:06 +01:00
parent 168f90b9a8
commit 3316f6d8f0
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM

View file

@ -17,31 +17,7 @@ fn main() -> anyhow::Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
if let Some(Commands::Auth { api_key }) = cli.command { if let Some(Commands::Auth { api_key }) = cli.command {
let config_path = ProjectDirs::from("com", "supleed2", "omg") save_api_key(&api_key)?;
.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::<Config>(&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);
}
};
} }
let config = ProjectDirs::from("com", "supleed2", "omg") let config = ProjectDirs::from("com", "supleed2", "omg")
@ -64,5 +40,30 @@ fn main() -> anyhow::Result<()> {
Ok(()) 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::<Config>(&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 // Tutorial at: https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html
// More info at: https://docs.rs/clap/latest/clap/_derive/index.html // More info at: https://docs.rs/clap/latest/clap/_derive/index.html