Skip to content
New issue

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

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

Already on GitHub? # to your account

[Feat] Add options to override the directory #9

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}));
}
13 changes: 11 additions & 2 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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, " "))
}
Expand Down