1 year ago

#328343

test-img

PaleHazy

Piping Command to another Command, what am I doing wrong here?

I was following this question how to do a sandwich pipe in rust?

this is the command I eventually want to run (linux/bash) from rust works fine written in a shell like this: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

this is the code I have representing what I have attempted..

fn add_docker_gpg_key() {
    
    let mut curl = Command::new("curl")
        .arg("-fsSL")
        .arg("https://download.docker.com/linux/ubuntu/gpg")
        .stdout(Stdio::piped())
        .spawn()
        .expect("curl fail");

    let mut apt_key = Command::new("apt-key")
        .arg("add")
        .arg("-")
        .stdin(Stdio::piped())
        .spawn()
        .expect("api=key fail");

    {
        let l_in = BufReader::new(curl.stdout.take().unwrap());
        let mut r_out = apt_key.stdin.take().unwrap();

        for line in l_in.lines() {
            println!("{:?}", line);
            write!(&mut r_out, "{}", line.unwrap()).unwrap();
        }
    }
    let left_ecode = curl.wait().expect("failed to wait on child");
    let right_ecode = apt_key.wait().expect("failed to wait on child");
    assert!(left_ecode.success());
    assert!(right_ecode.success());
}

end of script image I get this when I run it

what is best practice to do this?

linux

curl

rust

apt-key

0 Answers

Your Answer

Accepted video resources