improve API token error handling + improve passing .rhai scripts as argument
This commit is contained in:
parent
3423152b11
commit
d4b4c6aed0
33
src/main.rs
33
src/main.rs
@ -18,6 +18,8 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let (reply_tx, reply_rx) = mpsc::channel::<async_handler::Response>();
|
||||
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<EvalAltResult>> {
|
||||
let reply_rx = Arc::new(Mutex::new(reply_rx));
|
||||
let mut engine = Engine::new();
|
||||
@ -27,22 +29,35 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut scope = Scope::new();
|
||||
scope.push(
|
||||
"HETZNER_API_TOKEN",
|
||||
env::var("HETZNER_API_TOKEN").unwrap_or_else(|_| {
|
||||
let args: Vec<String> = 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::<String>() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,15 @@ pub fn register_hetzner_api(
|
||||
command_tx: Sender<Request>,
|
||||
reply_rx: Arc<Mutex<Receiver<Response>>>,
|
||||
) {
|
||||
engine
|
||||
.register_type_with_name::<HetznerClient>("HetznerClient")
|
||||
.register_fn("new_hetzner_client", |api_token: &str| -> Result<HetznerClient, Box<EvalAltResult>> {
|
||||
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>("HetznerClient")
|
||||
.register_fn("new_hetzner_client", HetznerClient::new)
|
||||
.register_fn(
|
||||
"list_servers",
|
||||
move |client: &mut HetznerClient| -> Result<Vec<WrappedServer>, Box<EvalAltResult>> {
|
||||
|
Loading…
Reference in New Issue
Block a user