diff --git a/src/cli.rs b/src/cli.rs index 03aa4ab..67235ae 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use clap::{Parser, Subcommand}; pub struct Cli { /// Set a custom username #[clap(short, long)] - pub username: Option, + pub name: Option, #[clap(subcommand)] pub command: Option, diff --git a/src/main.rs b/src/main.rs index a97794e..fee3dc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,17 @@ use cli::{Cli, Commands}; #[derive(Default, Deserialize, Serialize)] struct Config { api_key: Option, - username: Option, + name: Option, } fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if let Some(Commands::Auth { api_key }) = cli.command { - save_api_key(&api_key)?; + match save_api_key(&api_key) { + Ok(_) => std::process::exit(0), + Err(_) => std::process::exit(1), + }; } let config = ProjectDirs::from("com", "supleed2", "omg") @@ -30,19 +33,19 @@ fn main() -> anyhow::Result<()> { .or(config.api_key) .expect("omg.lol API key not provided as either environment variable or in config file"); - let username = cli.username + let name = cli.name .or(std::env::var("OMGLOL_USERNAME").ok()) - .or(config.username) + .or(config.name) .expect("omg.lol username not provided as command line option, environment variable or in config file"); - println!("omg-rs, ready for @{username}"); + println!("omg-rs, ready for @{name}"); println!("API key: {}", api_key); Ok(()) } -fn save_api_key(api_key: &str) -> anyhow::Result<()> { +fn save_api_key(api_key: &str) -> std::io::Result<()> { let config_path = ProjectDirs::from("com", "supleed2", "omg") - .context("Unable to access app config directory (while saving API key).")? + .expect("Unable to access app config directory (while saving API key).") .config_dir() .join("config.toml"); let _ = std::fs::create_dir_all( @@ -52,17 +55,17 @@ fn save_api_key(api_key: &str) -> anyhow::Result<()> { ); let Config { api_key: _, - username, + name, } = 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, + name, }) .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") + std::fs::write(&config_path, toml_str) } // Tutorial at: https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html