Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions goos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ if not(page_size > 0) then error("bad pagesize") end
goos.mkdir_all("./test/test_dir/test_dir/all")
local stat, err = goos.stat("./test/test_dir/test_dir/all")
if err then error(err) end

-- environ
local env = goos.environ()
print(env.PATH) -- prints the PATH environment variable
print(env.HOME) -- prints the HOME environment variable
for key, value in pairs(env) do
print(key .. "=" .. value)
end
```
13 changes: 13 additions & 0 deletions goos/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goos

import (
"os"
"strings"

lua "github.com/yuin/gopher-lua"
)
Expand Down Expand Up @@ -52,3 +53,15 @@ func MkdirAll(L *lua.LState) int {
}
return 0
}

// Environ lua goos.environ() returns table
func Environ(L *lua.LState) int {
envVars := os.Environ()
result := L.NewTable()
for _, env := range envVars {
parts := strings.SplitN(env, "=", 2)
result.RawSetString(parts[0], lua.LString(parts[1]))
}
L.Push(result)
return 1
}
24 changes: 24 additions & 0 deletions goos/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,27 @@ print(err == nil)
// Output:
// true
}

// goos.environ()
func ExampleEnviron() {
state := lua.NewState()
Preload(state)
source := `
local goos = require("goos")
local env = goos.environ()
-- Check that we get a table
print(type(env) == "table")
-- Check that we have at least one environment variable
local count = 0
for k, v in pairs(env) do
count = count + 1
end
print(count > 0)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// true
// true
}
1 change: 1 addition & 0 deletions goos/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ var api = map[string]lua.LGFunction{
"hostname": Hostname,
"get_pagesize": Getpagesize,
"mkdir_all": MkdirAll,
"environ": Environ,
}
15 changes: 15 additions & 0 deletions goos/test/test_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ end
function Test_pagesize(t)
assert(goos.get_pagesize() > 0, "pagesize")
end

function Test_environ(t)
local env = goos.environ()
assert(env, "environ should return table")
-- Check that we get a table with environment variables
local count = 0
for k, v in pairs(env) do
count = count + 1
assert(type(k) == "string", "key should be string")
assert(type(v) == "string", "value should be string")
end
assert(count > 0, "environ should return at least one environment variable")
-- PATH should exist on most systems
assert(env.PATH or env.Path, "PATH environment variable should exist")
end