build: allow to switch async runtime
This commit is contained in:
@@ -36,6 +36,9 @@ pub enum Commands {
|
||||
#[arg(short, long)]
|
||||
user_id: u32,
|
||||
},
|
||||
|
||||
ListAllUser,
|
||||
|
||||
Logout {
|
||||
#[arg(short, long)]
|
||||
user_id: u32,
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
use std::{
|
||||
fs::OpenOptions,
|
||||
io::{BufRead, stdin},
|
||||
};
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use nyquest_preset::nyquest::ClientBuilder;
|
||||
use palc::Parser;
|
||||
use sdgb_api::{
|
||||
ApiError,
|
||||
all_net::QRCode,
|
||||
auth_lite::{SDGB, SDHJ, delivery_raw},
|
||||
title::{
|
||||
@@ -12,13 +19,14 @@ use sdgb_api::{
|
||||
},
|
||||
},
|
||||
};
|
||||
use spdlog::{error, info};
|
||||
use spdlog::{error, info, warn};
|
||||
|
||||
use crate::commands::Cli;
|
||||
|
||||
mod commands;
|
||||
|
||||
#[compio::main]
|
||||
#[cfg_attr(feature = "compio", compio::main)]
|
||||
#[cfg_attr(feature = "tokio", tokio::main)]
|
||||
async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
nyquest_preset::register();
|
||||
let cmd = <Cli as Parser>::parse();
|
||||
@@ -78,6 +86,55 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
};
|
||||
println!("{}", String::from_utf8_lossy(&resp));
|
||||
}
|
||||
|
||||
commands::Commands::ListAllUser => {
|
||||
let mut stdin = stdin().lock();
|
||||
let mut buf = String::new();
|
||||
let mut user_ids = Vec::new();
|
||||
|
||||
while stdin.read_line(&mut buf).is_ok_and(|size| size != 0) {
|
||||
if buf.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let user_id: u32 = buf.trim().parse()?;
|
||||
buf.clear();
|
||||
user_ids.push(user_id);
|
||||
}
|
||||
|
||||
let users = futures_util::stream::iter(user_ids)
|
||||
.map(async |user_id| {
|
||||
info!("preview: {user_id}");
|
||||
let resp = Sdgb1_50::request::<_, GetUserPreviewApiResp>(
|
||||
&client,
|
||||
APIMethod::GetUserPreviewApi,
|
||||
user_id,
|
||||
GetUserPreviewApi { user_id },
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Err(e) = &resp {
|
||||
if matches!(e, ApiError::JSON { .. }) {
|
||||
warn!("account unregistered: {user_id}");
|
||||
} else {
|
||||
error!("preview failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
resp
|
||||
})
|
||||
.buffer_unordered(20)
|
||||
.filter_map(async |r| r.ok())
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
let output = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.create(true)
|
||||
.open("users.json")?;
|
||||
serde_json::to_writer_pretty(output, &users)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user