summaryrefslogtreecommitdiff
path: root/crates/vcs_actions/src/registry/client_registry.rs
blob: 484c4f4ac8ad20ff01a5135b15dd3c2e3fd71761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use action_system::{action::ActionContext, action_pool::ActionPool};
use tcp_connection::error::TcpTargetError;

use crate::{
    actions::local_actions::register_set_upstream_vault_action,
    connection::protocol::RemoteActionInvoke,
};

fn register_actions(pool: &mut ActionPool) {
    // Pool register here
    register_set_upstream_vault_action(pool);
}

pub fn client_action_pool() -> ActionPool {
    // Create pool
    let mut pool = ActionPool::new();

    // Register actions
    register_actions(&mut pool);

    // Add process events
    pool.set_on_proc_begin(|ctx, args| Box::pin(on_proc_begin(ctx, args)));

    // Return
    pool
}

async fn on_proc_begin(
    ctx: &ActionContext,
    _args: &(dyn std::any::Any + Send + Sync),
) -> Result<(), TcpTargetError> {
    // Is ctx remote
    let is_remote = ctx.is_remote();

    // Action name and arguments
    let action_name = ctx.action_name().to_string();
    let action_args_json = ctx.action_args_json().clone();

    // Get instance
    let Some(instance) = ctx.instance() else {
        return Err(TcpTargetError::Unsupported(
            "Missing ConnectionInstance in current context, this ActionPool does not support this call"
                .to_string()));
    };

    // If it's remote, invoke action at server
    if is_remote {
        // Build protocol message
        let msg = RemoteActionInvoke {
            action_name,
            action_args_json,
        };

        // Send
        let mut instance = instance.lock().await;
        instance.write_msgpack(&msg).await?;
    }

    // Return OK, wait for client to execute Action locally
    Ok(())
}