10

I want to execute perforce command line "p4" from Go to do the login job. "p4 login" require user to input password.

How can I run a program that requires user's input in Go?

The following code doesn't work.

err  = exec.Command(p4cmd, "login").Run()
if err != nil {
    log.Fatal(err)
}

2 Answers 2

8
// To run any system commands. EX: Cloud Foundry CLI commands: `CF login`
cmd := exec.Command("cf", "login")

// Sets standard output to cmd.stdout writer
cmd.Stdout = os.Stdout

// Sets standard input to cmd.stdin reader
cmd.Stdin = os.Stdin

cmd.Run()
1
  • 1
    @Anna i have added comments to above code. Does this help? Thanks
    – Vidhi Shah
    Oct 30, 2019 at 19:48
7

From the os/exec.Command docs:

// Stdin specifies the process's standard input. If Stdin is
// nil, the process reads from the null device (os.DevNull).
Stdin io.Reader

Set the command's Stdin field before executing it.

1
  • 2
    Despite this being seven years old, some more context for this answer would be great.
    – user3827326
    Aug 16, 2019 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.