From b6dfbe8a19b7d91f5e9413e9605c6cc020b55510 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Tue, 19 Apr 2022 09:46:13 -0500 Subject: [PATCH] Add ability to set the webhook url Add help menu --- src/main.rs | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2072e82..9cc9dbd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,13 @@ use std::path::PathBuf; mod discord; const MAXBUF: u64 = 8_380_416; +const HELP: &[&str] = &[ + concat!("dimgup-cli ", env!("CARGO_PKG_VERSION")), + "dimgup-cli config - set webhook", + "dimgup-cli - upload file to discord", + "dimgup-cli help - show this message", +]; + macro_rules! loop_try { ($e:expr) => { match $e { @@ -40,6 +47,16 @@ fn get_webhook(path: &Path) -> anyhow::Result { Ok(std::fs::read_to_string(path)?) } +fn set_webhook(webhook: &str) -> anyhow::Result<()> { + let proj_dirs = ProjectDirs::from("xyz", "karx", "dimgup-cli").unwrap(); + let mut b = proj_dirs.config_dir().to_path_buf(); + std::fs::create_dir_all(&b)?; + b.push("webhook"); + let mut file = File::create(&b)?; + file.write_all(webhook.as_bytes())?; + Ok(()) +} + fn get_file_info(path: &Path) -> anyhow::Result<(File, u64, String)> { let filename = path.file_name().unwrap().to_str().unwrap(); let mut handle = File::open(path)?; @@ -64,18 +81,26 @@ fn do_upload(mut file: File, size: u64, name: &str, webhook: &str) -> anyhow::Re } fn main() -> anyhow::Result<()> { - let conf = get_config_file(); - let webhook = match get_webhook(&conf) { - Ok(w) => w, - Err(e) => bail!( - "No webhook added. Please put your discord webhook URL into {:#?}", - conf - ), - }; - let filename = std::env::args() .nth(1) .ok_or_else(|| anyhow!("No filename was provided"))?; + if filename == "config" { + let webhook = std::env::args() + .nth(2) + .ok_or_else(|| anyhow!("No webhook was provided"))?; + set_webhook(&webhook)?; + println!("Webhook set successfully"); + return Ok(()); + } else if filename == "help" { + println!("{}", HELP.join("\n")); + return Ok(()); + } + + let conf = get_config_file(); + let webhook = match get_webhook(&conf) { + Ok(w) => w, + Err(_) => bail!("Please set your webhook. See --help for more info."), + }; let filename = PathBuf::from(filename); let (handle, size, name) = get_file_info(&filename)?;