How do I replace requested user input with a variable on a process multiple times?

Viewed 62

I'm trying to make a CLI wrapper, but I don't know how to replace the requested user input with a variable or something.

The command in which I wanted to do that does the following: firstly prints something, then requests for user input, and lastly prints some output again.

The code I've done so far(the .stdout_to_string() method is made by myself in another crate and it coverts the stdout to a string, as the name suggests =)) ):

let mut child = Command::new("solana-keygen")
    // .current_dir("")
    .arg("new")
    .arg("--outfile")
    .arg("wallet.json")
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn()
    .expect("Sorry");

let mut stdin = child.stdin.take().expect("Failed to open stdin");
stdin
    .write_all("".as_bytes())
    .expect("Failed to write to stdin");

stdin.flush().expect("Failed flushing");

let output = child.wait_with_output().expect("Failed to read stdout");

println!("{}", output.stdout_to_string());

This is what i get when i run the program(as a test):

Finished test [unoptimized + debuginfo] target(s) in 0.00s
 Running tests/test.rs (target/debug/deps/test-669b5016889a8807) 
 running 1 test
 For added security, enter a BIP39 passphrase
 NOTE! This passphrase improves security of the recovery seed phrase NOT the keypair file itself, which is stored as insecure plain text
 BIP39 Passphrase (empty for none):

The program was supposed to replace the requested user input automatically. After i press enter in the terminal, i get something like this:

Wrote new keypair to wallet.json
======================================================================
pubkey: A8nA9fo4BVD8qqRwFR6RRNxwFApVbstQjoGZRxyeDWSx
=======================================================================
Save this seed phrase and your BIP39 passphrase to recover your new keypair:
[SECRET PHRASE]
======================================================================

Does anyone know how I could solve this?

1 Answers

It works, just add the newline you want to send the program

use std::io::Write;
use std::process::{Command, Stdio};

fn main() {
    let mut child = Command::new("solana-keygen")
        // .current_dir("")
        .arg("new")
        .arg("--outfile")
        .arg("wallet.json")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    let mut stdin = child.stdin.take().expect("Failed to open stdin");
    stdin
        .write_all("\n".as_bytes())
        .expect("Failed to write to stdin");

    stdin.flush().expect("Failed flushing");

    let output = child.wait_with_output().expect("Failed to read stdout");

    println!("\n{:?}", output);
}

Result:

For added security, enter a BIP39 passphrase

NOTE! This passphrase improves security of the recovery seed phrase NOT the
keypair file itself, which is stored as insecure plain text

BIP39 Passphrase (empty for none): 
Output { status: ExitStatus(unix_wait_status(0)), stdout: "Generating a new keypair\n\nWrote new keypair to wallet.json\n==========================================================================\npubkey: HrR1CFqt6r8daa29o2E6tzkGCaF6zqAAAb1to9BsoYqx\n==========================================================================\nSave this seed phrase and your BIP39 passphrase to recover your new keypair:\nlawn leg file awesome print analyst voice noodle crisp feature rifle again\n==========================================================================\n", stderr: "" }
Related