Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await repetition #83

Closed
gngpp opened this issue Dec 14, 2022 · 6 comments
Closed

await repetition #83

gngpp opened this issue Dec 14, 2022 · 6 comments

Comments

@gngpp
Copy link

gngpp commented Dec 14, 2022

question:call function await on match, prompt repeats.
image

code:

    const PEER: &str = "peer";
    const PEER_SERVER: &str = "peer-server";
    let node_type_option = vec![PEER, PEER_SERVER];
    let node_type_select = inquire::Select::new("Select the peer node type that needs to be revoked.", node_type_option).prompt();
    match node_type_select {
        Ok(node_type) => {
            match node_type {
                PEER => {
                    let mut configuration = Configuration::new(config).await?;
                    let node_list = configuration.list().await?;
                    let node_option = node_list.iter()
                        .map(|v| v.name())
                        .collect::<Vec<&str>>();
                    let node_select = inquire::Select::new("Select node", node_option).prompt();
                    match node_select {
                        Ok(choice) => {
                            println!("{}", choice);
                        }
                        Err(_) => {}
                    }
                }
                PEER_SERVER => {
                    println!("{}", PEER_SERVER);
                }
                _ => {}
            }
        },

        Err(_) => {
            println!("There was an error, please try again")
        }
    }
    Ok(())
@gngpp
Copy link
Author

gngpp commented Dec 14, 2022

This problem arises whenever asynchronous function calls are made before inquire::Select instances

pub(crate) async fn subcommand_print_peer_handler(_config: String) -> anyhow::Result<()> {
    let mut configuration = <Configuration as AsyncTryFrom<String>>::try_from(_config).await?;

    let node_type_select = inquire::Select::new(
        "Select the node configuration to print",
        vec![PEER_TYPE, PEER_SERVER_TYPE],
    )
    .prompt();

    match node_type_select {
        Ok(node_type) => {
            match node_type {
                PEER_TYPE => {
                    let node_list = configuration.list().await?;
                    let mut node_option = node_list.iter().map(|v| v.name()).collect::<Vec<&str>>();
                    node_option.sort();

                    let option = inquire::Select::new("select peer", node_option)
                        .with_help_message(
                            "This will print the configuration and generate the QR code",
                        )
                        .prompt();
                    match option {
                        Ok(node_name) => {
                            let string = configuration.peer_str(node_name).await?;
                            println!("generated configuration:\n{}\n", string);
                            qr2term::print_qr(string)
                                .context("Failed to generate QRCode configuration")?;
                        }
                        Err(_) => {}
                    }
                }
                PEER_SERVER_TYPE => {
                    let string = configuration.peer_server_str().await?;
                    println!("{}", string);
                }
                _ => {
                    println!("Unable to find matching node type")
                }
            }
            drop(configuration);
        }
        Err(_) => {
            println!("please try again")
        }
    }
    Ok(())
}

@mikaelmello
Copy link
Owner

Hey! I haven't taken a deep look yet, but would it possible to write me a sample that I could reproduce locally?

I'll try to work on it this weekend

@gngpp
Copy link
Author

gngpp commented Dec 15, 2022

Hey! I haven't taken a deep look yet, but would it possible to write me a sample that I could reproduce locally?

I'll try to work on it this weekend

> git clone https://github.com/gngpp/wgsdc.git && cd wgsdc
> cargo run --example demo

The problem found so far: the sudo library is used: sudo::escalate_if_needed().unwrap() is used to obtain the system sudo permission

@gngpp
Copy link
Author

gngpp commented Dec 15, 2022

Hey! I haven't taken a deep look yet, but would it possible to write me a sample that I could reproduce locally?
I'll try to work on it this weekend

> git clone https://github.com/gngpp/wgsdc.git && cd wgsdc
> cargo run --example demo

The problem found so far: the sudo library is used: sudo::escalate_if_needed().unwrap() is used to obtain the system sudo permission

Using inquire and https://crates.io/crates/sudo at the same time may cause the above repeated prompts

@mikaelmello
Copy link
Owner

I did some digging and I'm afraid there isn't anything inquire can do to avoid that.

When escalate_if_needed is called, the sudo crate seems to be restarting the entire program to get into a privileged state. So the prompts you make before requesting sudo will be inevitably executed again.

Executing sudo::escalate_if_needed() in the beginning of the program should fix the issue you're having.

@gngpp
Copy link
Author

gngpp commented Dec 20, 2022

I did some digging and I'm afraid there isn't anything inquire can do to avoid that.

When escalate_if_needed is called, the sudo crate seems to be restarting the entire program to get into a privileged state. So the prompts you make before requesting sudo will be inevitably executed again.

Executing sudo::escalate_if_needed() in the beginning of the program should fix the issue you're having.

That's all. Thank you very much for your reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants