Skip to content

Commit 8fe46b0

Browse files
committed
feat: add .env loading from lua hooks
1 parent 55eb580 commit 8fe46b0

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
github.com/barelyhuman/go v0.2.2-0.20230713173609-2ee88bb52634
77
github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9
8+
github.com/joho/godotenv v1.5.1
89
github.com/otiai10/copy v1.9.0
910
github.com/vadv/gopher-lua-libs v0.4.1
1011
github.com/yuin/goldmark v1.5.4

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
4141
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4242
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
4343
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
44+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
45+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
4446
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
4547
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
4648
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=

lua/alvu/alvu.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"os"
55
"path"
66

7+
dotenv "github.com/joho/godotenv"
78
lua "github.com/yuin/gopher-lua"
89
)
910

1011
var api = map[string]lua.LGFunction{
11-
"files": GetFilesIndex,
12+
"files": GetFilesIndex,
13+
"get_env": GetEnv,
1214
}
1315

1416
// Preload adds json to the given Lua state's package.preload table. After it
@@ -81,3 +83,19 @@ func getFilesIndex(pathToIndex string) (paths []string, err error) {
8183

8284
return
8385
}
86+
87+
func GetEnv(L *lua.LState) int {
88+
// path to get the env from
89+
str := L.CheckString(1)
90+
// key to get from index
91+
key := L.CheckString(2)
92+
value := LGetEnv(L, str, key)
93+
L.Push(value)
94+
return 1
95+
}
96+
97+
func LGetEnv(L *lua.LState, fromFile string, str string) lua.LString {
98+
dotenv.Load(fromFile)
99+
val := os.Getenv(str)
100+
return lua.LString(val)
101+
}

0 commit comments

Comments
 (0)