From 652f33795eb3871062e0687034d85ac8a9dceafd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Fri, 24 Apr 2026 11:41:13 +0800 Subject: [PATCH] feat: remove fallback to first saved account in get-context-token BREAKING CHANGE: get-context-token now requires --user-id explicitly. The old behavior of falling back to the first saved account when --user-id was omitted has been removed. Changes: - cli.rs: user_id changed from Option to String - get_context_token.rs: removed resolve_user_id call, user_id is now required - account.rs: removed resolve_user_id function - storage.rs: removed get_account_ids function - README.md: updated get-context-token example to show required --user-id - Cargo.toml: bumped version to 0.4.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/cli.rs | 4 ++-- src/commands/account.rs | 14 +------------- src/commands/get_context_token.rs | 11 ++++------- src/main.rs | 2 +- src/storage.rs | 11 ----------- 8 files changed, 11 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8be7f0b..e59fc01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1971,7 +1971,7 @@ dependencies = [ [[package]] name = "wechat-cli" -version = "0.3.0" +version = "0.4.0" dependencies = [ "aes", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index f54b289..85ff890 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wechat-cli" -version = "0.3.0" +version = "0.4.0" edition = "2024" rust-version = "1.85" description = "A CLI tool to interact with a Wechat iLink bot." diff --git a/README.md b/README.md index d746a44..2ea2a14 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ wechat-cli account delete --account Wait for the next incoming message and print the `context_token`: ```bash -wechat-cli get-context-token [--user-id ] +wechat-cli get-context-token --user-id ``` ### Send diff --git a/src/cli.rs b/src/cli.rs index 6a13afc..0a7a79c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -78,9 +78,9 @@ pub struct AccountDeleteArgs { #[derive(Debug, Args)] pub struct GetContextTokenArgs { - /// Saved account user ID. If omitted, the first saved account is used. + /// Saved account user ID. #[arg(long)] - pub user_id: Option, + pub user_id: String, } #[derive(Debug, Args)] diff --git a/src/commands/account.rs b/src/commands/account.rs index 66c5ec2..c23e742 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -1,22 +1,10 @@ -use anyhow::{Context, Result, anyhow, bail}; +use anyhow::{Result, bail}; use crate::{ storage::{self, AccountData, load_accounts}, wechat::api::WeixinApiClient, }; -pub fn resolve_user_id(explicit: Option<&str>) -> Result { - if let Some(user_id) = explicit { - return Ok(user_id.to_string()); - } - - let user_ids = storage::get_account_ids().context("failed to load saved users")?; - user_ids - .into_iter() - .next() - .ok_or_else(|| anyhow!("no saved user found, run `wechat-cli login` first")) -} - pub fn build_client(data: &AccountData) -> WeixinApiClient { WeixinApiClient::new(&data.bot_token, data.route_tag.clone()) } diff --git a/src/commands/get_context_token.rs b/src/commands/get_context_token.rs index 3a24eb8..9adf8cb 100644 --- a/src/commands/get_context_token.rs +++ b/src/commands/get_context_token.rs @@ -1,16 +1,13 @@ use anyhow::{Context, Result, bail}; use crate::{ - commands::account::{build_client, resolve_user_id}, - storage, - wechat::api::is_session_expired, + commands::account::build_client, storage, wechat::api::is_session_expired, wechat::models::InboundMessage, }; -pub async fn run(user_id: Option<&str>) -> Result<()> { - let resolved_id = resolve_user_id(user_id)?; - let session = storage::get_account_data(&resolved_id) - .with_context(|| format!("failed to load account data for `{resolved_id}`"))?; +pub async fn run(user_id: &str) -> Result<()> { + let session = storage::get_account_data(user_id) + .with_context(|| format!("failed to load account data for `{user_id}`"))?; let client = build_client(&session); let user_id = session.user_id; let mut consecutive_errors = 0u32; diff --git a/src/main.rs b/src/main.rs index 6cdea4d..1b72c52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ async fn main() -> AnyResult<()> { AccountCommand::Delete(args) => commands::account::delete_account(args.account)?, }, Command::GetContextToken(args) => { - commands::get_context_token::run(args.user_id.as_deref()).await?; + commands::get_context_token::run(&args.user_id).await?; } Command::Send(args) => { commands::send::run( diff --git a/src/storage.rs b/src/storage.rs index 7bfbe66..f5d98d8 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -71,17 +71,6 @@ fn save_accounts_file(accounts: &AccountsFile) -> Result<()> { Ok(()) } -/// Returns the list of saved stable user IDs from local storage. -pub fn get_account_ids() -> Result> { - let accounts = load_accounts_file()?; - Ok(accounts - .accounts - .into_iter() - .map(|account| account.user_id) - .filter(|id| id.ends_with("@im.wechat")) - .collect()) -} - /// Loads the saved credentials for the given stable user ID. pub fn get_account_data(account_id: &str) -> Result { let accounts = load_accounts_file()?;