diff options
| author | KunoiSayami <[email protected]> | 2021-05-09 01:55:14 +0800 |
|---|---|---|
| committer | KunoiSayami <[email protected]> | 2021-05-09 01:55:14 +0800 |
| commit | d5f0c9c0a137cee561f1a94fb3425d52170ac196 (patch) | |
| tree | 834b3a4a9f65359e76379086b2363e3b33504fbb /src | |
| parent | 3cdf24651f7ee7914cc95970951d269c13fd13a5 (diff) | |
feat: Add list user commandv0.1.4
* workflow: Delete Windows build target
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 9d6f78e..9c15d87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,7 @@ use sqlx::Connection; use std::env; use std::io::{stdin, Read}; use std::result::Result::Ok; +use tokio_stream::StreamExt as _; const COOKIE_LENGTH: usize = 45; @@ -256,6 +257,32 @@ async fn cmd_add_user(matches: &ArgMatches<'_>, cfg: Config) -> Result<()> { Ok(()) } +async fn cmd_list_user(cfg: Config) -> Result<()> { + let mut conn = sqlx::SqliteConnection::connect(cfg.get_database_location()).await?; + + let (count,) = sqlx::query_as::<_, (i32,)>(r#"SELECT COUNT(*) FROM "accounts""#) + .fetch_one(&mut conn) + .await?; + + if count > 0 { + let mut iter = + sqlx::query_as::<_, (String,)>(r#"SELECT "user" FROM "accounts""#).fetch(&mut conn); + + println!( + "There is {} user{} in database", + count, + if count > 1 { "s" } else { "" } + ); + while let Some(Ok((row,))) = iter.next().await { + println!("{}", row) + } + } else { + println!("There is not user exists.") + } + + Ok(()) +} + async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> { match arg_matches.subcommand() { ("authenticate-cookie", Some(matches)) => { @@ -278,6 +305,9 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> { ("adduser", Some(matches)) => { cmd_add_user(matches, cfg).await?; } + ("users", Some(_matches)) => { + cmd_list_user(cfg).await?; + } _ => {} } Ok(0) @@ -286,7 +316,7 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> { fn main() -> Result<()> { let logfile = FileAppender::builder() .encoder(Box::new(PatternEncoder::new( - "{d(%H:%M:%S)}-{M}-{l} - {m}\n", + "{d(%Y-%m-%d %H:%M:%S)}- {h({l})} - {m}{n}", ))) .build("/tmp/output.log")?; @@ -350,6 +380,7 @@ fn main() -> Result<()> { .args(sub_args), ) .subcommand(SubCommand::with_name("init").about("Init sqlite database")) + .subcommand(SubCommand::with_name("users").about("List all register user in database")) .subcommand( SubCommand::with_name("adduser") .about("Add user to database") |
