Return value from std process into no blocking mode

Hello,

I do a app with a backend, Rust, asnd a frontend,Typescript.

From the frontend I start a std::process

pub fn setup_dependencies(ue4path: String) -> std::string::String {
    match detect_os()
    {
        "windows" => return startcommand("powershell", (&"/Setup.bat".to_string()).as_str()),
        "macos" => return startcommand("sh", ( &"/Setup.command".to_string()).as_str()),
        "linux" => return startcommand("sh", (&"/Setup.sh".to_string()).as_str()),
        _ => return "Failed to download dependencies".to_string()  
    }
}
fn startcommand(terminal: &str, command: &str) -> std::string::String {
    let command = Command::new(terminal)
        .arg(command)
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

        let mut line = String::new();
        let child_out = BufReader::new(command.stdout.unwrap());
        child_out
        .lines()
        .map(|l|l.unwrap())
        .for_each(|l| line.push_str(&*l));
        //.for_each(|line| println!("{:?}", line));
        return line ;
}

The script starts download some app, then installs them.
I would like to read output in real time (non blocking mode) and show these logs into the frontend.
I know the only way I can do that is to use child process. But, at the end of the script, this one ask for admin privileges.
Without spawn a child, I can switch the privilege ( I can see the box which asks admin privilege), but I cannot display output in real time.
With a child, can see the output in real time, but cannot use the script, I cannot see the admin requirements.

So how to display logs in non blocking mode into main process, without spawing a child ,

Thanks

Hi, I think to get an answer you need to break the problem down and be more specific. From reading your question I can't even make out what OS you are having problems with. Is the dialog box the windows "Do you want to let this program make changes to your computer"? Is it a dialog created by the install script that asks root permissions on linux using sudo?

I would say start by making it work on one OS first. If you run into issues, be precise about what is going on.

There's many ways to skin a cat. Which one is appropriate here I cannot tell from the info you gave. One thing you could do if you know that the install script always needs administrator privileges, is to first elevate in the launcher, deal with user permission/dialogs and then invoke the install script directly with admin privs.

I haven't written windows or mac software lately, so I don't know much about the apis, but on linux you're generally supposed to use polkit if you need admin privileges. That's more for installed software and not so much for installers though. But if this is some kind of package manager that stays installed and will repeatedly install software, polkit might be the appropriate technology.

Try to give clear steps of what you are trying to achieve, how and where it fails.

Sorry for missing some elements.
I am on Windows10. But can work on Mac or Linux.
I use tauri, Build smaller, faster, and more secure desktop applications with a web frontend | Tauri Apps. It use rust as backend and can use js as frontend.
To start the app, I have to use the powershell or cmd.
So I can see the process log into powershell.

My goal is to catch the output of the process, and display it, into js component.
The issue I get is the blocking mode of std process.

With the admin privilege, if I use the main process to start script, the exe asks with a box (I think It's the better way, for me).

I hope I forget nothing.
I can share full code, on GH

The standard library does not have a non-blocking alternative.

If you need this to work in a web framework that uses Futures/async, then you either need to use library like tokio::process or spawn a thread for it.

You can also use channels to send lines from the thread to some other part of your server that sends a response.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.