clean up run_with_callback api

This commit is contained in:
Markus Unterwaditzer
2025-11-17 10:45:28 +01:00
committed by Markus Unterwaditzer
parent bb6135c682
commit 73a5d324c4
2 changed files with 9 additions and 7 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ async fn run_installer(app_handle: tauri::AppHandle, args: String) -> anyhow::Re
tauri::async_runtime::spawn_blocking(move || {
installer::run_with_callback(
// TODO: we should split using something similar to shlex in python
args.split_whitespace().map(String::from).collect(),
args.split_whitespace(),
Some(Box::new(move |output| {
app_handle
.emit("installer-output", output)
+8 -6
View File
@@ -285,34 +285,36 @@ async fn run(args: Args) -> Result<(), Error> {
pub type OutputCallback = Box<dyn Fn(&str) + Send + Sync>;
/// Run the installer with CLI arguments and optional output callback
///
/// # Example
/// ```no_run
/// use installer;
///
/// // if the callback is None, stdout/stderr is going to be used
/// let result = installer::run_with_callback(
/// vec!["installer".to_string(), "orbic-network".to_string(), "--admin-password".to_string(), "12345".to_string()],
/// ["installer", "orbic-network", "--admin-password", "12345"],
/// Some(Box::new(|output| {
/// print!("{}", output);
/// }))
/// );
/// ```
pub fn run_with_callback(args: Vec<String>, callback: Option<OutputCallback>) -> Result<(), Error> {
// Set up the callback if provided
pub fn run_with_callback<'a>(
args: impl IntoIterator<Item = &'a str>,
callback: Option<OutputCallback>,
) -> Result<(), Error> {
let _guard;
if let Some(cb) = callback {
_guard = output::set_output_callback(move |s: &str| cb(s));
}
// Create a Tokio runtime and run the installer
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.context("Failed to create Tokio runtime")?
.block_on(async {
// Parse arguments
let parsed_args = Args::try_parse_from(&args).context("Failed to parse arguments")?;
let parsed_args =
Args::try_parse_from(args.into_iter()).context("Failed to parse arguments")?;
// Run the installer
run(parsed_args).await