mirror of
https://github.com/EFForg/rayhunter.git
synced 2026-06-21 03:38:24 -07:00
clean up run_with_callback api
This commit is contained in:
committed by
Markus Unterwaditzer
parent
bb6135c682
commit
73a5d324c4
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user