diff --git a/example.js b/example.js index a920d5a..674ba6d 100644 --- a/example.js +++ b/example.js @@ -2,5 +2,7 @@ import exec from 'k6/x/exec'; export default function () { console.log(exec.command("date")); - console.log(exec.command("ls",["-a","-l"])); + console.log(exec.command("ls",["-a","-l"], { + "dir": "sub-directory" // optional directory in which the command has to be run + })); } \ No newline at end of file diff --git a/exec.go b/exec.go index 3d0fc45..7e592d1 100644 --- a/exec.go +++ b/exec.go @@ -22,6 +22,11 @@ type EXEC struct { vu modules.VU } +// CommandOptions contains the options that can be passed to command. +type CommandOptions struct { + Dir string +} + // Ensure the interfaces are implemented correctly. var ( _ modules.Module = &RootModule{} @@ -41,8 +46,12 @@ func (exec *EXEC) Exports() modules.Exports { } // Command is a wrapper for Go exec.Command -func (*EXEC) Command(name string, args []string) string { - out, err := exec.Command(name, args...).Output() +func (*EXEC) Command(name string, args []string, option CommandOptions) string { + cmd := exec.Command(name, args...) + if option.Dir != "" { + cmd.Dir = option.Dir + } + out, err := cmd.Output() if err != nil { log.Fatal(err.Error() + " on command: " + name + " " + strings.Join(args, " ")) }