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

feat: system/exec() function call ( #1043) #1067

Merged
merged 1 commit into from
Aug 7, 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
58 changes: 58 additions & 0 deletions internal/pkg/bifs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,61 @@ func BIF_system(input1 *mlrval.Mlrval) *mlrval.Mlrval {

return mlrval.FromString(outputString)
}

func BIF_exec(mlrvals []*mlrval.Mlrval) *mlrval.Mlrval {

if len(mlrvals) == 0 {
return mlrval.ERROR
}

cmd := exec.Command(mlrvals[0].AcquireStringValue())
combinedOutput := false

args := []string { mlrvals[0].AcquireStringValue() }
if len(mlrvals) > 1 {
for _, val := range mlrvals[1].GetArray()[0:] {
args = append(args, val.AcquireStringValue())
}
}
cmd.Args = args

if len(mlrvals) > 2 {

for pe := mlrvals[2].AcquireMapValue().Head; pe != nil; pe = pe.Next {
if pe.Key == "env" {
env := []string {}
for _, val := range pe.Value.GetArray()[0:] {
env = append(env, val.AcquireStringValue())
}
cmd.Env = env
}
if pe.Key == "dir" {
cmd.Dir = pe.Value.AcquireStringValue()
}
if pe.Key == "combined_output" {
combinedOutput = pe.Value.AcquireBoolValue()
}

if pe.Key == "stdin_string" {
cmd.Stdin = strings.NewReader(pe.Value.AcquireStringValue())
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could really use if ... else if ... else and handle the unexpected "none of the above" in the else

that said, miller DSL source code doesn't propagate go-style error returns as much as it should, so without reworking the BIF API, the best you could do would be to print to stderr and exit 1 (which isn't great but is what i've done all over the place in the DSL)

}

outputBytes := []byte(nil)
err := error(nil)

if combinedOutput {
outputBytes, err = cmd.CombinedOutput()
} else {
outputBytes, err = cmd.Output()
}

if err != nil {
return mlrval.ERROR
}

outputString := strings.TrimRight(string(outputBytes), "\n")
return mlrval.FromString(outputString)
}
18 changes: 18 additions & 0 deletions internal/pkg/dsl/cst/builtin_function_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,24 @@ either case it should return a boolean.`,
unaryFunc: bifs.BIF_system,
},

{
name: "exec",
class: FUNC_CLASS_SYSTEM,
help: `'$output = exec(
"command",
["arg1", "arg2"],
{"env": ["ENV_VAR=ENV_VALUE"],
"dir": "/tmp/run_command_here",
"stdin_string": "this is input fed to program",
"combined_output": true
)'
Run a command via executable, path, args and environment, yielding its stdout minus final carriage return.`,
examples: []string{
`exec("echo", ["Hello", "$YOUR_NAME"], {"env": "YOUR_NAME=World"}) outputs "Hello World"`,
},
variadicFunc: bifs.BIF_exec,
},

{
name: "version",
class: FUNC_CLASS_SYSTEM,
Expand Down