os_exec.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package stdlib
  2. import (
  3. "os/exec"
  4. "github.com/d5/tengo/v2"
  5. )
  6. func makeOSExecCommand(cmd *exec.Cmd) *tengo.ImmutableMap {
  7. return &tengo.ImmutableMap{
  8. Value: map[string]tengo.Object{
  9. // combined_output() => bytes/error
  10. "combined_output": &tengo.UserFunction{
  11. Name: "combined_output",
  12. Value: FuncARYE(cmd.CombinedOutput),
  13. },
  14. // output() => bytes/error
  15. "output": &tengo.UserFunction{
  16. Name: "output",
  17. Value: FuncARYE(cmd.Output),
  18. }, //
  19. // run() => error
  20. "run": &tengo.UserFunction{
  21. Name: "run",
  22. Value: FuncARE(cmd.Run),
  23. }, //
  24. // start() => error
  25. "start": &tengo.UserFunction{
  26. Name: "start",
  27. Value: FuncARE(cmd.Start),
  28. }, //
  29. // wait() => error
  30. "wait": &tengo.UserFunction{
  31. Name: "wait",
  32. Value: FuncARE(cmd.Wait),
  33. }, //
  34. // set_path(path string)
  35. "set_path": &tengo.UserFunction{
  36. Name: "set_path",
  37. Value: func(args ...tengo.Object) (tengo.Object, error) {
  38. if len(args) != 1 {
  39. return nil, tengo.ErrWrongNumArguments
  40. }
  41. s1, ok := tengo.ToString(args[0])
  42. if !ok {
  43. return nil, tengo.ErrInvalidArgumentType{
  44. Name: "first",
  45. Expected: "string(compatible)",
  46. Found: args[0].TypeName(),
  47. }
  48. }
  49. cmd.Path = s1
  50. return tengo.UndefinedValue, nil
  51. },
  52. },
  53. // set_dir(dir string)
  54. "set_dir": &tengo.UserFunction{
  55. Name: "set_dir",
  56. Value: func(args ...tengo.Object) (tengo.Object, error) {
  57. if len(args) != 1 {
  58. return nil, tengo.ErrWrongNumArguments
  59. }
  60. s1, ok := tengo.ToString(args[0])
  61. if !ok {
  62. return nil, tengo.ErrInvalidArgumentType{
  63. Name: "first",
  64. Expected: "string(compatible)",
  65. Found: args[0].TypeName(),
  66. }
  67. }
  68. cmd.Dir = s1
  69. return tengo.UndefinedValue, nil
  70. },
  71. },
  72. // set_env(env array(string))
  73. "set_env": &tengo.UserFunction{
  74. Name: "set_env",
  75. Value: func(args ...tengo.Object) (tengo.Object, error) {
  76. if len(args) != 1 {
  77. return nil, tengo.ErrWrongNumArguments
  78. }
  79. var env []string
  80. var err error
  81. switch arg0 := args[0].(type) {
  82. case *tengo.Array:
  83. env, err = stringArray(arg0.Value, "first")
  84. if err != nil {
  85. return nil, err
  86. }
  87. case *tengo.ImmutableArray:
  88. env, err = stringArray(arg0.Value, "first")
  89. if err != nil {
  90. return nil, err
  91. }
  92. default:
  93. return nil, tengo.ErrInvalidArgumentType{
  94. Name: "first",
  95. Expected: "array",
  96. Found: arg0.TypeName(),
  97. }
  98. }
  99. cmd.Env = env
  100. return tengo.UndefinedValue, nil
  101. },
  102. },
  103. // process() => imap(process)
  104. "process": &tengo.UserFunction{
  105. Name: "process",
  106. Value: func(args ...tengo.Object) (tengo.Object, error) {
  107. if len(args) != 0 {
  108. return nil, tengo.ErrWrongNumArguments
  109. }
  110. return makeOSProcess(cmd.Process), nil
  111. },
  112. },
  113. },
  114. }
  115. }