@@ -3,10 +3,12 @@ package lua_global
33import (
44 "fmt"
55 "net/url"
6+ "os"
67 "regexp"
78 "runtime"
89 "strings"
910
11+ "github.com/gvcgo/version-manager/internal/utils"
1012 lua "github.com/yuin/gopher-lua"
1113)
1214
@@ -148,6 +150,56 @@ func LenString(L *lua.LState) int {
148150 return 1
149151}
150152
151- // TODO: execute system command
153+ /*
154+ lua: string = vmrGetEnv(key string)
155+ */
156+ func GetOsEnv (L * lua.LState ) int {
157+ key := L .ToString (1 )
158+ if key == "" {
159+ L .Push (lua .LString ("" ))
160+ return 1
161+ }
162+ L .Push (lua .LString (os .Getenv (key )))
163+ return 1
164+ }
165+
166+ /*
167+ lua: bool = vmrSetOsEnv()
168+ */
169+ func SetOsEnv (L * lua.LState ) int {
170+ key := L .ToString (1 )
171+ value := L .ToString (2 )
172+ err := os .Setenv (key , value )
173+ if err != nil {
174+ L .Push (lua .LFalse )
175+ } else {
176+ L .Push (lua .LTrue )
177+ }
178+ return 1
179+ }
152180
153- // TODO: check prequisite
181+ /*
182+ lua: result string, ok bool = vmrExecSystemCmd(to_collect_output bool, workdir string, args {a, b, c, d, ...})
183+ */
184+ func ExecSystemCmd (L * lua.LState ) int {
185+ toCollectOutput := L .ToBool (1 )
186+ workDir := L .ToString (2 )
187+
188+ args := make ([]string , 0 )
189+ array := L .ToTable (3 )
190+ array .ForEach (func (l1 , l2 lua.LValue ) {
191+ args = append (args , l2 .String ())
192+ })
193+
194+ runner := utils .NewSysCommandRunner (toCollectOutput , workDir , args ... )
195+ err := runner .Run ()
196+ result := runner .GetOutput ()
197+ L .Push (lua .LString (result ))
198+ if err != nil {
199+ L .Push (lua .LFalse )
200+ } else {
201+ L .Push (lua .LTrue )
202+ }
203+
204+ return 2
205+ }
0 commit comments