Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI commands do not work on Windows machine #2

Open
abal3 opened this issue Nov 17, 2021 · 1 comment
Open

CLI commands do not work on Windows machine #2

abal3 opened this issue Nov 17, 2021 · 1 comment

Comments

@abal3
Copy link

abal3 commented Nov 17, 2021

When running the CLI commands e.g. console.log(exec.command("date")); nothing is outputted to the console.
No errors are thrown either.

@amommersteeg-d2l
Copy link

So there is a something strange when using this extension with Windows. I have noticed that the command runs/works but return nothing to the k6 terminal, just an empty line, like \n was printed.

I believe by default cmd is used to run commands. You can force it run as powershell console.log(exec.command("'powershell" ["date"]));

Anyways to overcome the issues with Windows I ended modifying this extension to use go-cmd using their buffer code example to return stdout and stderr. Disclaimer: I am new to go-lang & xk6-extensions

package cmd

import (
	"os"
	"fmt"
	"strings"

	"github.com/go-cmd/cmd"
	"go.k6.io/k6/js/modules"
)


func init() {
    modules.Register("k6/x/cmd", new(CombinedOutput))
}

type CombinedOutput struct{
    Stdout string
    Stderr string
}

func (c *CombinedOutput) RunCmd(name string, args []string) bool {

	envCmd := cmd.NewCmd(name, args...)
	status := <-envCmd.Start()
  
	c.Stdout = strings.Join(status.Stdout, "\n")
	c.Stderr = strings.Join(status.Stderr, "\n")
	
	if envCmd.Status().Error != nil || len(status.Stderr) > 0{
		return false
	}else{
		return true
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants