Add ability to set the webhook url

Add help menu
This commit is contained in:
Yash Karandikar 2022-04-19 09:46:13 -05:00
parent 1587a7e6cc
commit b6dfbe8a19

View file

@ -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 <webhook_url> - set webhook",
"dimgup-cli <file> - 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<String> {
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)?;