diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-12-24 16:12:46 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-12-24 16:12:46 +0800 |
| commit | ca29e7b2152260059417c142641cd19ddbb512c4 (patch) | |
| tree | 8927915b3cc5229e6cd503e886de33e596d5814c /crates/vcs_actions | |
| parent | 4cccc5d093bed8abbe35689c061486b79c44c558 (diff) | |
Add local output channel for CLI feedback in track actions
Add `try_get_local_output` helper to retrieve output channel from
context and `local_println!` macro for sending formatted strings. Use
these in track actions to send progress messages to CLI instead of
stdout.
Also reduce log level for connection events from info to debug.
Diffstat (limited to 'crates/vcs_actions')
| -rw-r--r-- | crates/vcs_actions/src/actions.rs | 22 | ||||
| -rw-r--r-- | crates/vcs_actions/src/actions/track_action.rs | 36 | ||||
| -rw-r--r-- | crates/vcs_actions/src/connection/action_service.rs | 6 |
3 files changed, 54 insertions, 10 deletions
diff --git a/crates/vcs_actions/src/actions.rs b/crates/vcs_actions/src/actions.rs index d3c0dd6..260a6be 100644 --- a/crates/vcs_actions/src/actions.rs +++ b/crates/vcs_actions/src/actions.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use action_system::action::ActionContext; use cfg_file::config::ConfigFile; use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; -use tokio::sync::Mutex; +use tokio::sync::{Mutex, mpsc::Sender}; use vcs_data::{ constants::SERVER_PATH_MEMBER_PUB, data::{ @@ -64,6 +64,16 @@ pub fn try_get_user_directory(ctx: &ActionContext) -> Result<Arc<UserDirectory>, Ok(user_directory) } +/// Try to get the LocalWorkspace instance from the context. +pub fn try_get_local_output(ctx: &ActionContext) -> Result<Arc<Sender<String>>, TcpTargetError> { + let Some(output) = ctx.get_arc::<Sender<String>>() else { + return Err(TcpTargetError::NotFound( + "Client sender not found".to_string(), + )); + }; + Ok(output) +} + /// Authenticate member based on context and return MemberId pub async fn auth_member( ctx: &ActionContext, @@ -196,3 +206,13 @@ macro_rules! write_and_return { return Ok($result); }}; } + +/// The macro to send formatted string to output channel. +/// Usage: local_println!(output, "format string", arg1, arg2, ...) +#[macro_export] +macro_rules! local_println { + ($output:expr, $($arg:tt)*) => {{ + let formatted = format!($($arg)*); + let _ = $output.send(formatted).await; + }}; +} diff --git a/crates/vcs_actions/src/actions/track_action.rs b/crates/vcs_actions/src/actions/track_action.rs index a67480c..63c1b67 100644 --- a/crates/vcs_actions/src/actions/track_action.rs +++ b/crates/vcs_actions/src/actions/track_action.rs @@ -28,9 +28,12 @@ use vcs_data::{ }, }; -use crate::actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_local_workspace, - try_get_vault, +use crate::{ + actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_local_output, + try_get_local_workspace, try_get_vault, + }, + local_println, }; pub type NextVersion = String; @@ -404,9 +407,14 @@ async fn proc_create_tasks_local( print_infos: bool, ) -> Result<CreateTaskResult, TcpTargetError> { let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; let mut mut_instance = instance.lock().await; let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Creating {} files...", relative_paths.len()); + } + // Wait for remote detection of whether the sheet exists let has_sheet = mut_instance.read_msgpack::<bool>().await?; if !has_sheet { @@ -458,7 +466,7 @@ async fn proc_create_tasks_local( // Print success info if print_infos { - println!("+ {}", path.display()); + local_println!(local_output, "+ {}", path.display()); } success_relative_pathes.push(path); @@ -545,11 +553,16 @@ async fn proc_update_tasks_local( file_update_info: HashMap<PathBuf, (NextVersion, UpdateDescription)>, ) -> Result<UpdateTaskResult, TcpTargetError> { let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; let mut mut_instance = instance.lock().await; let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; let mut success = Vec::new(); + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Updating {} files...", relative_paths.len()); + } + for path in relative_paths.iter() { let Ok(mapping) = local_sheet.mapping_data(path) else { // Is mapping not found, write empty @@ -616,7 +629,13 @@ async fn proc_update_tasks_local( // Print success info if print_infos { - println!("↑ {} ({} -> {})", path.display(), version, next_version); + local_println!( + local_output, + "↑ {} ({} -> {})", + path.display(), + version, + next_version + ); } } } @@ -777,9 +796,14 @@ async fn proc_sync_tasks_local( print_infos: bool, ) -> Result<SyncTaskResult, TcpTargetError> { let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; let mut mut_instance = instance.lock().await; let mut success: Vec<PathBuf> = Vec::new(); + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Syncing {} files...", relative_paths.len()); + } + for path in relative_paths { let Some((version, description, vfid)) = mut_instance.read_msgpack::<SyncVersionInfo>().await? @@ -880,7 +904,7 @@ async fn proc_sync_tasks_local( // Print success info if print_infos { - println!("↓ {}", path.display()); + local_println!(local_output, "↓ {}", path.display()); } } Ok(SyncTaskResult::Success(success)) diff --git a/crates/vcs_actions/src/connection/action_service.rs b/crates/vcs_actions/src/connection/action_service.rs index a736ed8..f137126 100644 --- a/crates/vcs_actions/src/connection/action_service.rs +++ b/crates/vcs_actions/src/connection/action_service.rs @@ -8,7 +8,7 @@ use std::{ use action_system::{action::ActionContext, action_pool::ActionPool}; use cfg_file::config::ConfigFile; -use log::{error, info, warn}; +use log::{debug, error, info, warn}; use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; use tokio::{ net::{TcpListener, TcpStream}, @@ -134,7 +134,7 @@ fn build_server_future( accept_result = listener.accept(), if !shutdown_requested => { match accept_result { Ok((stream, _addr)) => { - info!("New connection. (now {})", active_connections); + debug!("New connection. (now {})", active_connections); let _ = tx.send(1).await; let vault_clone = vault.clone(); @@ -143,7 +143,7 @@ fn build_server_future( spawn(async move { process_connection(stream, vault_clone, action_pool_clone).await; - info!("A connection closed. (now {})", active_connections); + debug!("A connection closed. (now {})", active_connections); let _ = tx_clone.send(-1).await; }); } |
