From d4b4c6aed05dd12d1ee7bf2c5a7b6f49ac03f59f Mon Sep 17 00:00:00 2001 From: Maxime Van Hees Date: Tue, 15 Jul 2025 10:00:36 +0200 Subject: [PATCH] improve API token error handling + improve passing .rhai scripts as argument --- src/main.rs | 33 ++++++++++++++++++++++++--------- src/rhai_api.rs | 11 +++++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 483fd70..31adf51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,8 @@ fn main() -> Result<(), Box> { let (reply_tx, reply_rx) = mpsc::channel::(); async_handler::run_worker(command_rx, reply_tx); + let hetzner_api_token = env::var("HETZNER_API_TOKEN").unwrap_or_default(); + let rhai_thread = thread::spawn(move || -> Result<(), Box> { let reply_rx = Arc::new(Mutex::new(reply_rx)); let mut engine = Engine::new(); @@ -27,22 +29,35 @@ fn main() -> Result<(), Box> { let mut scope = Scope::new(); scope.push( "HETZNER_API_TOKEN", - env::var("HETZNER_API_TOKEN").unwrap_or_else(|_| { - let args: Vec = env::args().collect(); - args.get(1).cloned().unwrap_or_default() - }), + hetzner_api_token ); - let script = std::env::args().nth(2); - if let Some(s) = script { - engine.run_with_scope(&mut scope, &s)?; - } else { - engine.run_file_with_scope(&mut scope, "example.rhai".into())?; + let script_path = env::args().nth(1).ok_or_else(|| { + eprintln!("Error: Expected a .rhai script file as an argument."); + "No .rhai script provided" + })?; + + if !script_path.ends_with(".rhai") { + eprintln!("Error: The provided file must have a .rhai extension."); + return Err("Invalid file extension".into()); } + + engine.run_file_with_scope(&mut scope, script_path.into())?; Ok(()) }); if let Err(err) = rhai_thread.join().unwrap() { + match *err { + EvalAltResult::ErrorRuntime(ref val, _) if val.is_string() => { + if let Some(s) = val.clone().try_cast::() { + if s.contains("HETZNER_API_TOKEN cannot be empty") { + eprintln!("\nError: The HETZNER_API_TOKEN environment variable was not set, which is required for this script."); + return Ok(()); + } + } + }, + _ => {} + } eprintln!("Error in Rhai script: {}", *err); } diff --git a/src/rhai_api.rs b/src/rhai_api.rs index 269a462..b43157f 100644 --- a/src/rhai_api.rs +++ b/src/rhai_api.rs @@ -10,6 +10,15 @@ pub fn register_hetzner_api( command_tx: Sender, reply_rx: Arc>>, ) { + engine + .register_type_with_name::("HetznerClient") + .register_fn("new_hetzner_client", |api_token: &str| -> Result> { + if api_token.is_empty() { + return Err("HETZNER_API_TOKEN cannot be empty.".into()); + } + Ok(HetznerClient::new(api_token)) + }); + let list_servers_tx = command_tx.clone(); let list_servers_rx = reply_rx.clone(); let get_server_status_tx = command_tx.clone(); @@ -22,8 +31,6 @@ pub fn register_hetzner_api( let ping_server_rx = reply_rx.clone(); engine - .register_type_with_name::("HetznerClient") - .register_fn("new_hetzner_client", HetznerClient::new) .register_fn( "list_servers", move |client: &mut HetznerClient| -> Result, Box> {