2019-01-18 21:17:24 +03:00
|
|
|
package stdlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
|
2019-12-30 00:38:51 +03:00
|
|
|
"github.com/d5/tengo/v2"
|
2019-01-18 21:17:24 +03:00
|
|
|
)
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
func makeOSExecCommand(cmd *exec.Cmd) *tengo.ImmutableMap {
|
|
|
|
return &tengo.ImmutableMap{
|
|
|
|
Value: map[string]tengo.Object{
|
2019-01-18 21:17:24 +03:00
|
|
|
// combined_output() => bytes/error
|
2019-12-20 22:40:38 +03:00
|
|
|
"combined_output": &tengo.UserFunction{
|
|
|
|
Name: "combined_output",
|
|
|
|
Value: FuncARYE(cmd.CombinedOutput),
|
|
|
|
},
|
2019-01-18 21:17:24 +03:00
|
|
|
// output() => bytes/error
|
2019-12-20 22:40:38 +03:00
|
|
|
"output": &tengo.UserFunction{
|
|
|
|
Name: "output",
|
|
|
|
Value: FuncARYE(cmd.Output),
|
|
|
|
}, //
|
2019-01-18 21:17:24 +03:00
|
|
|
// run() => error
|
2019-12-20 22:40:38 +03:00
|
|
|
"run": &tengo.UserFunction{
|
|
|
|
Name: "run",
|
|
|
|
Value: FuncARE(cmd.Run),
|
|
|
|
}, //
|
2019-01-18 21:17:24 +03:00
|
|
|
// start() => error
|
2019-12-20 22:40:38 +03:00
|
|
|
"start": &tengo.UserFunction{
|
|
|
|
Name: "start",
|
|
|
|
Value: FuncARE(cmd.Start),
|
|
|
|
}, //
|
2019-01-18 21:17:24 +03:00
|
|
|
// wait() => error
|
2019-12-20 22:40:38 +03:00
|
|
|
"wait": &tengo.UserFunction{
|
|
|
|
Name: "wait",
|
|
|
|
Value: FuncARE(cmd.Wait),
|
|
|
|
}, //
|
2019-01-18 21:17:24 +03:00
|
|
|
// set_path(path string)
|
2019-12-20 22:40:38 +03:00
|
|
|
"set_path": &tengo.UserFunction{
|
2019-03-01 21:48:02 +03:00
|
|
|
Name: "set_path",
|
2019-12-20 22:40:38 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
2019-01-18 21:17:24 +03:00
|
|
|
if len(args) != 1 {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrWrongNumArguments
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
2019-12-20 22:40:38 +03:00
|
|
|
s1, ok := tengo.ToString(args[0])
|
2019-01-18 21:17:24 +03:00
|
|
|
if !ok {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
2019-02-21 03:26:11 +03:00
|
|
|
Name: "first",
|
|
|
|
Expected: "string(compatible)",
|
|
|
|
Found: args[0].TypeName(),
|
|
|
|
}
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
|
|
|
cmd.Path = s1
|
2019-12-20 22:40:38 +03:00
|
|
|
return tengo.UndefinedValue, nil
|
2019-01-18 21:17:24 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// set_dir(dir string)
|
2019-12-20 22:40:38 +03:00
|
|
|
"set_dir": &tengo.UserFunction{
|
2019-03-01 21:48:02 +03:00
|
|
|
Name: "set_dir",
|
2019-12-20 22:40:38 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
2019-01-18 21:17:24 +03:00
|
|
|
if len(args) != 1 {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrWrongNumArguments
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
2019-12-20 22:40:38 +03:00
|
|
|
s1, ok := tengo.ToString(args[0])
|
2019-01-18 21:17:24 +03:00
|
|
|
if !ok {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
2019-02-21 03:26:11 +03:00
|
|
|
Name: "first",
|
|
|
|
Expected: "string(compatible)",
|
|
|
|
Found: args[0].TypeName(),
|
|
|
|
}
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
|
|
|
cmd.Dir = s1
|
2019-12-20 22:40:38 +03:00
|
|
|
return tengo.UndefinedValue, nil
|
2019-01-18 21:17:24 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// set_env(env array(string))
|
2019-12-20 22:40:38 +03:00
|
|
|
"set_env": &tengo.UserFunction{
|
2019-03-01 21:48:02 +03:00
|
|
|
Name: "set_env",
|
2019-12-20 22:40:38 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
2019-01-18 21:17:24 +03:00
|
|
|
if len(args) != 1 {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrWrongNumArguments
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
var env []string
|
|
|
|
var err error
|
|
|
|
switch arg0 := args[0].(type) {
|
2019-12-20 22:40:38 +03:00
|
|
|
case *tengo.Array:
|
2019-02-21 03:26:11 +03:00
|
|
|
env, err = stringArray(arg0.Value, "first")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-20 22:40:38 +03:00
|
|
|
case *tengo.ImmutableArray:
|
2019-02-21 03:26:11 +03:00
|
|
|
env, err = stringArray(arg0.Value, "first")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrInvalidArgumentType{
|
2019-02-21 03:26:11 +03:00
|
|
|
Name: "first",
|
|
|
|
Expected: "array",
|
|
|
|
Found: arg0.TypeName(),
|
|
|
|
}
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
2019-02-21 03:26:11 +03:00
|
|
|
cmd.Env = env
|
2019-12-20 22:40:38 +03:00
|
|
|
return tengo.UndefinedValue, nil
|
2019-01-18 21:17:24 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// process() => imap(process)
|
2019-12-20 22:40:38 +03:00
|
|
|
"process": &tengo.UserFunction{
|
2019-03-01 21:48:02 +03:00
|
|
|
Name: "process",
|
2019-12-20 22:40:38 +03:00
|
|
|
Value: func(args ...tengo.Object) (tengo.Object, error) {
|
2019-01-18 21:17:24 +03:00
|
|
|
if len(args) != 0 {
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil, tengo.ErrWrongNumArguments
|
2019-01-18 21:17:24 +03:00
|
|
|
}
|
2019-01-30 06:52:00 +03:00
|
|
|
return makeOSProcess(cmd.Process), nil
|
2019-01-18 21:17:24 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|