How to automate a terminal that may require stdin input?

Hello everyone. Hope you're having a wonderful day.

I've been trying to make a CLI app that can automate multiple cmd/terminal commands ( whether it requires input or not ). For instance:

  1. I want to cd into a directory
  2. Then I want to run git pull origin master
  3. Then I want to enter my github username
  4. Then I want to enter my github password

So far, I have actually made this: GitHub - p32929/fay_cli: A simple cross platform CLI app written in Rust to automate multiple shell ( bash or cmd ) commands
but it cannot take user inputs & I'm not sure how to automate the process.

Edit:
More specifically, My questions are:

  1. How to know if a child process is waiting for an input?
  2. How to execute chain of commands just like a terminal but automatically?

Thanks

Well, if your question is “how to read from stdin”, here’s the answer:

fn readln() -> String {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).unwrap();
    input.trim_end().to_string()
}

Note that unwrapping is usually bad, so you’d better try another error handling strategy.

More specifically, My questions are:

  1. How to know if a child process is waiting for an input?
  2. How to execute chain of commands just like a terminal but automatically?

Thanks

  1. I don't think there's a good way to know that. Rather, you expect the process to be in a state where it's waiting for input if you run it in a certain way.

  2. I think you need a psuedotty here. The rexpect crate looks promising, but I haven't used it.

Thanks a lot for your answer.
BTW, in your answer 1, Can you please write what is that certain way?

I'm just wondering, there are several terminal apps made using rust. not sure how they do it

The psuedotty is emulating a user at a terminal running a program by hand. So however you run a program in a terminal to make it prompt you for input would be that certain way. rexpect gives you a way to emulate that interactive behavior.

As far as I can remember, I actually tried rexpect while building the CLI app, but it seems, it only supports linux. I use windows.

Ah sorry I was afraid of that. Maybe try looking at the winpty-rs crate? I use Linux and don't know how this would work on Windows, so maybe someone else has a better answer.

Its okay. Thanks a lot for your replies. It means a lot.

1 Like

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.