script: Pass-through of a Program's Standard Error

Hello, and thank you for this wonderful package!

I’d like to use script with some programs that use standard error to prompt the user to do something before providing output. One example is ykman which prompts the user to “Touch your YubiKey…”. After doing so, the program will print the one time password code to standard out. Ideally, I’d like the program’s standard error to pass-through directly to os.Stderr, but it appears that script.Exec() instead interleaves the program’s stderr and stdout. This means the user of this script doesn’t receive the prompt from the program, and also means the prompt needs to be filtered out of the output. I’m curious if anyone has come up with a way to handle this. Below is a minimal example. Thanks!

package main

import (
	"github.com/bitfield/script"
)

func main() {
	p := script.Exec(`
		ykman oath accounts code --single "someAccountId"
	`)
	code, _ := p.String()
	script.Echo(code).Stdout()
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (19 by maintainers)

Most upvoted comments

Thank you, @bitfield! I’ve created #148 to represent that bug as you requested.

Out of curiosity, is there an interest in providing a version of Exec() that doesn’t interleave standard error? It seems like having the opportunity to keep a program’s standard error separate (originally requested in #17 could provide a solution for #128. Doing so would remove the need for extra filtering code that could potentially be brittle and error-prone.

Something like:

script.Exec("ykman oath accounts code --single "someAccountId").WithStdout(buf).Stderr()

or

script.Exec("ykman oath accounts code --single "someAccountId").WithStderr(ignore).Stdout()

or

script.Exec("ykman oath accounts code --single "someAccountId").CombinedOutput() // similar to os/exec.Cmd.CombinedOutput

I’m just spitballing ideas here. Happy to dig in to more potential use cases to find a workable API.

No, that’s a bug. Exec doesn’t correctly detect the exit status of a failing command here. Would you mind opening a separate issue for this, and I’ll fix it shortly.