diff options
author | KunoiSayami <[email protected]> | 2021-05-09 02:08:34 +0800 |
---|---|---|
committer | KunoiSayami <[email protected]> | 2021-05-09 02:08:34 +0800 |
commit | e7f1a83e3b90208dccc33a0647a15cf12519d13a (patch) | |
tree | fce7bf72bc7202bff9eb14f9e9e46e63903c7756 | |
parent | d5f0c9c0a137cee561f1a94fb3425d52170ac196 (diff) |
feat: Add deluser commandv0.1.5
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 35 |
3 files changed, 37 insertions, 2 deletions
@@ -350,7 +350,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cgit-simple-authentication" -version = "0.1.4" +version = "0.1.5" dependencies = [ "anyhow", "base64", @@ -1,6 +1,6 @@ [package] name = "cgit-simple-authentication" -version = "0.1.4" +version = "0.1.5" authors = ["KunoiSayami <[email protected]>"] edition = "2018" diff --git a/src/main.rs b/src/main.rs index 9c15d87..89be8e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -283,6 +283,33 @@ async fn cmd_list_user(cfg: Config) -> Result<()> { Ok(()) } +async fn cmd_delete_user(matches: &ArgMatches<'_>, cfg: Config) -> Result<()> { + let user = matches.value_of("user").unwrap_or(""); + if user.is_empty() { + return Err(anyhow::Error::msg("Please input a valid username")) + } + + let mut conn = sqlx::SqliteConnection::connect(cfg.get_database_location()).await?; + + let items = sqlx::query_as::<_, (i32,)>(r#"SELECT 1 FROM "accounts" WHERE "user" = ?"#) + .bind(user) + .fetch_all(&mut conn) + .await?; + + if items.is_empty() { + return Err(anyhow::Error::msg(format!("User {} not found", user))) + } + + sqlx::query(r#"DELETE FROM "accounts" WHERE "user" = ?"#) + .bind(user) + .execute(&mut conn) + .await?; + + println!("Delete {} from database", user); + + Ok(()) +} + async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> { match arg_matches.subcommand() { ("authenticate-cookie", Some(matches)) => { @@ -308,6 +335,9 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> { ("users", Some(_matches)) => { cmd_list_user(cfg).await?; } + ("deluser", Some(matches)) => { + cmd_delete_user(matches, cfg).await?; + } _ => {} } Ok(0) @@ -387,6 +417,11 @@ fn main() -> Result<()> { .arg(Arg::with_name("user").required(true)) .arg(Arg::with_name("password").required(true)), ) + .subcommand( + SubCommand::with_name("deluser") + .about("Delete user from database") + .arg(Arg::with_name("user").required(true)) + ) .get_matches(); // Load filter configurations |