cmd icon indicating copy to clipboard operation
cmd copied to clipboard

have not Stdout when use Options

Open Phuong39 opened this issue 3 years ago • 1 comments

when i use Options

package main

import (
	"fmt"
	"github.com/go-cmd/cmd"
	"os/exec"
	"syscall"
)

func main() {

	opt := cmd.Options{
		Streaming: true,
		BeforeExec: []func(cmd *exec.Cmd){
			func(cmd *exec.Cmd) { cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} },
		},
	}


	newCmd := cmd.NewCmdOptions(opt, "cmd.exe", "/c", "whoami")
	//newCmd := cmd.NewCmd("cmd.exe", "/c", "whoami")

	gotStatus := <-newCmd.Start()

	fmt.Println(gotStatus.Stdout)

}

have no Stdout ===>output : [ ]

if not use Options it have a Stdout

package main

import (
	"fmt"
	"github.com/go-cmd/cmd"
)

func main() {

	//opt := cmd.Options{
	//	Streaming: true,
	//	BeforeExec: []func(cmd *exec.Cmd){
	//		func(cmd *exec.Cmd) { cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} },
	//	},
	//}

	//newCmd := cmd.NewCmdOptions(opt, "cmd.exe", "/c", "whoami")
	newCmd := cmd.NewCmd("cmd.exe", "/c", "whoami")

	gotStatus := <-newCmd.Start()

	fmt.Println(gotStatus.Stdout)

}

===>output : [desktop-123\nobody]

Phuong39 avatar Mar 07 '22 10:03 Phuong39

No obvious reason why this should be the case. This pkg is 100% tested and somewhat widely used, so such a fundament bug probably wouldn't have show up by now. Can you (or someone else) debug this to find the cause? I don't have a Windows machine so I cannot debug it.

daniel-nichter avatar Mar 13 '22 15:03 daniel-nichter

I am getting the same issue

vishu42 avatar Mar 02 '23 08:03 vishu42

i don't get stdout or stderr if using options

vishu42 avatar Mar 02 '23 08:03 vishu42

I am on a mac

vishu42 avatar Mar 02 '23 08:03 vishu42

I can reproduce this. I'll take a look.

daniel-nichter avatar Mar 02 '23 15:03 daniel-nichter

This is not a bug. What's happening is related to Streaming: true. From the docs for this option:

// If Streaming is true, Cmd.Stdout and Cmd.Stderr channels are created and // STDOUT and STDERR output lines are written them in real time. This is // faster and more efficient than polling Cmd.Status. The caller must read both // streaming channels, else lines are dropped silently.

So do this before running the cmd,

    go func() {
        for line := range newCmd.Stdout {
            fmt.Println(line)
        }
    }()

And it will print the (streaming) output.

Since this isn't a bug, will close issue. Please re-open if I'm wrong.

daniel-nichter avatar Mar 03 '23 01:03 daniel-nichter