From 2d667d4c3d63a44c4cc01f7fb7133e5714d60584 Mon Sep 17 00:00:00 2001 From: gennyble Date: Sun, 20 Apr 2025 06:52:12 -0500 Subject: add quick-n-easy import cli command --- src/main.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 680c944..af238dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, error::Error, sync::Arc}; +use std::{env, error::Error, path::PathBuf, sync::Arc}; use confindent::Confindent; use database::{BoardRow, Database, Error as DbError, PermissionSetting}; @@ -31,6 +31,7 @@ use twilight_util::builder::{ }; mod database; +mod import; const APP_ID: u64 = 1363055126264283136; @@ -62,7 +63,13 @@ macro_rules! success { async fn main() -> anyhow::Result<()> { dotenv::dotenv().ok(); - let conf = Confindent::from_file("/etc/leaberblord.conf").unwrap(); + let conf_path = if !PathBuf::from("/etc/leaberblord.conf").exists() { + "leaberblord.conf" + } else { + "/etc/leaberblord.conf" + }; + + let conf = Confindent::from_file(conf_path).unwrap(); let db_dir = conf .child_owned("Database") .unwrap_or("/var/leaberblord/leaberblord.sqlite".to_string()); @@ -73,6 +80,22 @@ async fn main() -> anyhow::Result<()> { let db = Arc::new(Database::new(db_dir)); db.create_tables(); + let arg = std::env::args().nth(1); + match arg.as_deref() { + Some("import") => { + let Some(file) = std::env::args().nth(2) else { + eprintln!("import subcommand requires a file path argument to read data from."); + return Ok(()); + }; + + let data = std::fs::read_to_string(file).unwrap(); + import::import(&db, data); + println!("Import finished!"); + return Ok(()); + } + _ => (), + } + let mut shard = Shard::new(ShardId::ONE, token.clone(), Intents::GUILD_MESSAGES); let http = Arc::new(HttpClient::new(token)); let mut cache = DefaultInMemoryCache::builder() @@ -300,7 +323,7 @@ async fn add_points( Some(p) if p > 0 => (p, p, "added to"), Some(p) if p < 0 => (p, -p, "removed from"), Some(0) => { - return success!("adding 0 points is a no-operation, so I won't doing anything"); + return success!("adding 0 points is a no-operation! I won't do anything :)"); } Some(_) | None => unreachable!(), }; -- cgit 1.4.1-3-g733a5