summaryrefslogtreecommitdiff
path: root/crates/vcs_actions/src/actions/local_actions.rs
blob: e2788ff401397d0c393f2c0dea8ac04f24ece03a (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
use std::net::SocketAddr;

use action_system::{action::ActionContext, macros::action_gen};
use log::info;
use tcp_connection::error::TcpTargetError;

#[action_gen]
pub async fn hello_world_action(ctx: ActionContext, _n: ()) -> Result<(), TcpTargetError> {
    // Ensure the instance is available
    let Some(instance) = ctx.instance() else {
        return Err(TcpTargetError::NotFound(
            "Connection Instance Lost.".to_string(),
        ));
    };

    if ctx.is_proc_on_local() {
        // Local execution - communication is handled by on_proc_begin
        info!("Hello World action executed locally");
    } else if ctx.is_proc_on_remote() {
        // Remote execution - read the message from the client
        let read = instance.lock().await.read_text().await?;
        info!("{}", read)
    }

    Ok(())
}

#[action_gen]
pub async fn set_upstream_vault_action(
    ctx: ActionContext,
    _upstream: SocketAddr,
) -> Result<(), TcpTargetError> {
    // Ensure the instance is available
    let Some(instance) = ctx.instance() else {
        return Err(TcpTargetError::NotFound(
            "Connection Instance Lost.".to_string(),
        ));
    };

    if ctx.is_proc_on_local() {
        // Invoke on local
        // Send the message to the server
        let _ = instance.lock().await.write_text("Hello World!").await;
    } else if ctx.is_proc_on_remote() {
        // Remote execution - read the message from the client
        let read = instance.lock().await.read_text().await?;
        info!("Received: {}", read)
    }

    Ok(())
}