yongzhy
yongzhy

Reputation: 977

How to execute command which requires input from user

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)
}

Upvotes: 10

Views: 6637

Answers (2)

Vidhi Shah
Vidhi Shah

Reputation: 81

// 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()

Upvotes: 8

axw
axw

Reputation: 7083

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.

Upvotes: 7

Related Questions