func runCommand()

in dev-tools/mage/benchmark.go [131:152]


func runCommand(env map[string]string, cmd string, outputFile string, args ...string) (bool, error) {
	var stdOut io.Writer
	var stdErr io.Writer
	if outputFile != "" {
		fileOutput, err := os.Create(createDir(outputFile))
		if err != nil {
			return false, fmt.Errorf("failed to create %s output file: %w", cmd, err)
		}
		defer func(fileOutput *os.File) {
			err := fileOutput.Close()
			if err != nil {
				log.Fatalf("Failed to close file %s", err)
			}
		}(fileOutput)
		stdOut = io.MultiWriter(os.Stdout, fileOutput)
		stdErr = io.MultiWriter(os.Stderr, fileOutput)
	} else {
		stdOut = os.Stdout
		stdErr = os.Stderr
	}
	return sh.Exec(env, stdOut, stdErr, cmd, args...)
}