11use anyhow:: Result as AnyResult ;
22use clap:: { CommandFactory , Parser } ;
3- use jetkvm_control:: device:: { rpc_get_device_id, rpc_ping} ;
43use jetkvm_control:: jetkvm_config:: JetKvmConfig ;
54use jetkvm_control:: jetkvm_rpc_client:: JetKvmRpcClient ;
6- use jetkvm_control:: system:: rpc_get_edid;
75use tokio:: time:: { sleep, Duration } ;
86use tracing:: { error, info, warn} ;
97use tracing_subscriber;
@@ -26,10 +24,16 @@ struct CliConfig {
2624 /// The password for authentication.
2725 #[ arg( short = 'P' , long) ]
2826 password : Option < String > ,
27+
28+ // When the "lua" feature is enabled, the first positional argument is the Lua script path.
29+ #[ cfg( feature = "lua" ) ]
30+ /// Path to the Lua script to execute.
31+ #[ arg( required = true , index = 1 ) ]
32+ lua_script : String ,
2933}
3034
3135/// Loads configuration from file (or uses the default) and then applies CLI overrides.
32- fn load_and_override_config ( cli_config : CliConfig ) -> JetKvmConfig {
36+ fn load_and_override_config ( cli_config : & CliConfig ) -> JetKvmConfig {
3337 let mut config = JetKvmConfig :: load ( ) . unwrap_or_else ( |err| {
3438 warn ! (
3539 "Failed to load config.toml ({}). Using default configuration." ,
@@ -38,17 +42,17 @@ fn load_and_override_config(cli_config: CliConfig) -> JetKvmConfig {
3842 JetKvmConfig :: default ( )
3943 } ) ;
4044
41- if let Some ( host) = cli_config. host {
42- config. host = host;
45+ if let Some ( host) = & cli_config. host {
46+ config. host = host. clone ( ) ;
4347 }
44- if let Some ( port) = cli_config. port {
45- config. port = port;
48+ if let Some ( port) = & cli_config. port {
49+ config. port = port. clone ( ) ;
4650 }
47- if let Some ( api) = cli_config. api {
48- config. api = api;
51+ if let Some ( api) = & cli_config. api {
52+ config. api = api. clone ( ) ;
4953 }
50- if let Some ( password) = cli_config. password {
51- config. password = password;
54+ if let Some ( password) = & cli_config. password {
55+ config. password = password. clone ( ) ;
5256 }
5357 config
5458}
@@ -65,7 +69,7 @@ async fn main() -> AnyResult<()> {
6569 info ! ( "CLI config provided: {:?}" , cli_config) ;
6670
6771 // Load configuration from file (or default) and override with CLI options.
68- let config = load_and_override_config ( cli_config) ;
72+ let config = load_and_override_config ( & cli_config) ;
6973
7074 // Validate that the critical field 'host' is set.
7175 if config. host . trim ( ) . is_empty ( ) {
@@ -76,49 +80,48 @@ async fn main() -> AnyResult<()> {
7680 std:: process:: exit ( 1 ) ;
7781 }
7882
79- // Create and use the client.
83+ // Create and connect the client.
8084 let mut client = JetKvmRpcClient :: new ( config) ;
81-
8285 if let Err ( err) = client. connect ( ) . await {
8386 error ! ( "Failed to connect to RPC server: {:?}" , err) ;
8487 std:: process:: exit ( 1 ) ;
8588 }
8689 client. wait_for_channel_open ( ) . await ?;
87- let ping = rpc_ping ( & client) . await ;
88- info ! ( "Ping: {:?}" , ping) ;
89- let device_id = rpc_get_device_id ( & client) . await ;
90- info ! ( "Device ID: {:?}" , device_id) ;
91- let edid = rpc_get_edid ( & client) . await ;
92- info ! ( "EDID: {:?}" , edid) ;
9390
91+ // Lua mode: if the "lua" feature is enabled, read and execute the provided Lua script.
9492 #[ cfg( feature = "lua" ) ]
9593 {
9694 use jetkvm_control:: lua_engine:: LuaEngine ;
9795 use std:: sync:: Arc ;
9896 use tokio:: sync:: Mutex ;
99- let client_arc = Arc :: new ( Mutex :: new ( client) ) ;
10097
101- // Create and configure the Lua engine.
98+ // Read the Lua script from the file path given as the first positional argument.
99+ let script = tokio:: fs:: read_to_string ( & cli_config. lua_script ) . await ?;
100+ info ! ( "Executing Lua script from {}" , & cli_config. lua_script) ;
101+
102+ // Wrap the client in an Arc/Mutex for the Lua engine.
103+ let client_arc = Arc :: new ( Mutex :: new ( client) ) ;
102104 let lua_engine = LuaEngine :: new ( client_arc) ;
103105 lua_engine. register_builtin_functions ( ) ?;
104106
105- // Optionally, execute a Lua script.
106- let script = r#"
107- print("Executing Lua script...")
108- send_windows_key()
109- delay(250)
110- send_text("notepad")
111- send_return()
112- delay(250)
113- send_text("Hello World!")
114- send_ctrl_a()
115- send_ctrl_c()
116- right_click(100, 200)
117- "# ;
118-
119- lua_engine. exec_script ( script) . await ?;
107+ lua_engine. exec_script ( & script) . await ?;
120108 info ! ( "Lua script executed successfully." ) ;
121109 }
122110
111+ // Normal mode: if the "lua" feature is not enabled, perform normal actions.
112+ #[ cfg( not( feature = "lua" ) ) ]
113+ {
114+ use jetkvm_control:: device:: { rpc_get_device_id, rpc_ping} ;
115+ use jetkvm_control:: system:: rpc_get_edid;
116+
117+ let ping = rpc_ping ( & client) . await ;
118+ info ! ( "Ping: {:?}" , ping) ;
119+ let device_id = rpc_get_device_id ( & client) . await ;
120+ info ! ( "Device ID: {:?}" , device_id) ;
121+ let edid = rpc_get_edid ( & client) . await ;
122+ info ! ( "EDID: {:?}" , edid) ;
123+ }
124+
123125 Ok ( ( ) )
124126}
127+
0 commit comments