mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-17 22:42:01 -08:00
120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package stdlib
|
|
|
|
import (
|
|
"os/exec"
|
|
|
|
"github.com/d5/tengo/v2"
|
|
)
|
|
|
|
func makeOSExecCommand(cmd *exec.Cmd) *tengo.ImmutableMap {
|
|
return &tengo.ImmutableMap{
|
|
Value: map[string]tengo.Object{
|
|
// combined_output() => bytes/error
|
|
"combined_output": &tengo.UserFunction{
|
|
Name: "combined_output",
|
|
Value: FuncARYE(cmd.CombinedOutput),
|
|
},
|
|
// output() => bytes/error
|
|
"output": &tengo.UserFunction{
|
|
Name: "output",
|
|
Value: FuncARYE(cmd.Output),
|
|
}, //
|
|
// run() => error
|
|
"run": &tengo.UserFunction{
|
|
Name: "run",
|
|
Value: FuncARE(cmd.Run),
|
|
}, //
|
|
// start() => error
|
|
"start": &tengo.UserFunction{
|
|
Name: "start",
|
|
Value: FuncARE(cmd.Start),
|
|
}, //
|
|
// wait() => error
|
|
"wait": &tengo.UserFunction{
|
|
Name: "wait",
|
|
Value: FuncARE(cmd.Wait),
|
|
}, //
|
|
// set_path(path string)
|
|
"set_path": &tengo.UserFunction{
|
|
Name: "set_path",
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
|
if len(args) != 1 {
|
|
return nil, tengo.ErrWrongNumArguments
|
|
}
|
|
s1, ok := tengo.ToString(args[0])
|
|
if !ok {
|
|
return nil, tengo.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
}
|
|
cmd.Path = s1
|
|
return tengo.UndefinedValue, nil
|
|
},
|
|
},
|
|
// set_dir(dir string)
|
|
"set_dir": &tengo.UserFunction{
|
|
Name: "set_dir",
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
|
if len(args) != 1 {
|
|
return nil, tengo.ErrWrongNumArguments
|
|
}
|
|
s1, ok := tengo.ToString(args[0])
|
|
if !ok {
|
|
return nil, tengo.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
}
|
|
cmd.Dir = s1
|
|
return tengo.UndefinedValue, nil
|
|
},
|
|
},
|
|
// set_env(env array(string))
|
|
"set_env": &tengo.UserFunction{
|
|
Name: "set_env",
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
|
if len(args) != 1 {
|
|
return nil, tengo.ErrWrongNumArguments
|
|
}
|
|
|
|
var env []string
|
|
var err error
|
|
switch arg0 := args[0].(type) {
|
|
case *tengo.Array:
|
|
env, err = stringArray(arg0.Value, "first")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
case *tengo.ImmutableArray:
|
|
env, err = stringArray(arg0.Value, "first")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
default:
|
|
return nil, tengo.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "array",
|
|
Found: arg0.TypeName(),
|
|
}
|
|
}
|
|
cmd.Env = env
|
|
return tengo.UndefinedValue, nil
|
|
},
|
|
},
|
|
// process() => imap(process)
|
|
"process": &tengo.UserFunction{
|
|
Name: "process",
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
|
if len(args) != 0 {
|
|
return nil, tengo.ErrWrongNumArguments
|
|
}
|
|
return makeOSProcess(cmd.Process), nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|