mirror of
https://github.com/supleed2/omg-rs.git
synced 2024-12-22 21:55:51 +00:00
Add exit to auth flow and rename username to name
This commit is contained in:
parent
3316f6d8f0
commit
4d2f326837
|
@ -4,7 +4,7 @@ use clap::{Parser, Subcommand};
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
/// Set a custom username
|
/// Set a custom username
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub username: Option<String>,
|
pub name: Option<String>,
|
||||||
|
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
pub command: Option<Commands>,
|
pub command: Option<Commands>,
|
||||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -10,14 +10,17 @@ use cli::{Cli, Commands};
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
api_key: Option<String>,
|
api_key: Option<String>,
|
||||||
username: Option<String>,
|
name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
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 {
|
||||||
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")
|
let config = ProjectDirs::from("com", "supleed2", "omg")
|
||||||
|
@ -30,19 +33,19 @@ fn main() -> anyhow::Result<()> {
|
||||||
.or(config.api_key)
|
.or(config.api_key)
|
||||||
.expect("omg.lol API key not provided as either environment variable or in config file");
|
.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(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");
|
.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);
|
println!("API key: {}", api_key);
|
||||||
Ok(())
|
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")
|
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()
|
.config_dir()
|
||||||
.join("config.toml");
|
.join("config.toml");
|
||||||
let _ = std::fs::create_dir_all(
|
let _ = std::fs::create_dir_all(
|
||||||
|
@ -52,17 +55,17 @@ fn save_api_key(api_key: &str) -> anyhow::Result<()> {
|
||||||
);
|
);
|
||||||
let Config {
|
let Config {
|
||||||
api_key: _,
|
api_key: _,
|
||||||
username,
|
name,
|
||||||
} = read_to_string(&config_path)
|
} = read_to_string(&config_path)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|str| toml::from_str::<Config>(&str).ok())
|
.and_then(|str| toml::from_str::<Config>(&str).ok())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let toml_str = toml::to_string_pretty(&Config {
|
let toml_str = toml::to_string_pretty(&Config {
|
||||||
api_key: Some(api_key.to_string()),
|
api_key: Some(api_key.to_string()),
|
||||||
username,
|
name,
|
||||||
})
|
})
|
||||||
.expect("Unable to convert updated config to TOML (when trying to save API key).");
|
.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
|
// Tutorial at: https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html
|
||||||
|
|
Loading…
Reference in a new issue