Skip to content

Commit 23ca5e8

Browse files
committed
Add goos.environ() function
1 parent 7c75a2e commit 23ca5e8

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

goos/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ if not(page_size > 0) then error("bad pagesize") end
2626
goos.mkdir_all("./test/test_dir/test_dir/all")
2727
local stat, err = goos.stat("./test/test_dir/test_dir/all")
2828
if err then error(err) end
29+
30+
-- environ
31+
local env = goos.environ()
32+
print(env.PATH) -- prints the PATH environment variable
33+
print(env.HOME) -- prints the HOME environment variable
34+
for key, value in pairs(env) do
35+
print(key .. "=" .. value)
36+
end
2937
```

goos/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goos
33

44
import (
55
"os"
6+
"strings"
67

78
lua "github.com/yuin/gopher-lua"
89
)
@@ -52,3 +53,18 @@ func MkdirAll(L *lua.LState) int {
5253
}
5354
return 0
5455
}
56+
57+
// Environ lua goos.environ() returns table
58+
func Environ(L *lua.LState) int {
59+
envVars := os.Environ()
60+
result := L.NewTable()
61+
for _, env := range envVars {
62+
// Split on first '=' to handle values that contain '='
63+
parts := strings.SplitN(env, "=", 2)
64+
if len(parts) == 2 {
65+
result.RawSetString(parts[0], lua.LString(parts[1]))
66+
}
67+
}
68+
L.Push(result)
69+
return 1
70+
}

goos/example_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,27 @@ print(err == nil)
8282
// Output:
8383
// true
8484
}
85+
86+
// goos.environ()
87+
func ExampleEnviron() {
88+
state := lua.NewState()
89+
Preload(state)
90+
source := `
91+
local goos = require("goos")
92+
local env = goos.environ()
93+
-- Check that we get a table
94+
print(type(env) == "table")
95+
-- Check that we have at least one environment variable
96+
local count = 0
97+
for k, v in pairs(env) do
98+
count = count + 1
99+
end
100+
print(count > 0)
101+
`
102+
if err := state.DoString(source); err != nil {
103+
log.Fatal(err.Error())
104+
}
105+
// Output:
106+
// true
107+
// true
108+
}

goos/loader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ var api = map[string]lua.LGFunction{
2525
"hostname": Hostname,
2626
"get_pagesize": Getpagesize,
2727
"mkdir_all": MkdirAll,
28+
"environ": Environ,
2829
}

goos/test/test_api.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ end
1919
function Test_pagesize(t)
2020
assert(goos.get_pagesize() > 0, "pagesize")
2121
end
22+
23+
function Test_environ(t)
24+
local env = goos.environ()
25+
assert(env, "environ should return table")
26+
-- Check that we get a table with environment variables
27+
local count = 0
28+
for k, v in pairs(env) do
29+
count = count + 1
30+
assert(type(k) == "string", "key should be string")
31+
assert(type(v) == "string", "value should be string")
32+
end
33+
assert(count > 0, "environ should return at least one environment variable")
34+
-- PATH should exist on most systems
35+
assert(env.PATH or env.Path, "PATH environment variable should exist")
36+
end

0 commit comments

Comments
 (0)