123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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
- },
- },
- },
- }
- }
|