1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
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
},
},
},
}
}
|