Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 4e41f78

Browse files
committed
Add environment variable functions
Add functions `getenv`, `setenv`, and `env`.
1 parent 1acd2c8 commit 4e41f78

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ Returns:
4848

4949
- The current working directory (string).
5050

51+
### env {#system-env}
52+
53+
`env ()`
54+
55+
Retrieve the entire environment.
56+
57+
Returns:
58+
59+
- A table mapping environment variables names to their string value
60+
(table).
61+
62+
### getenv {#system-getenv}
63+
64+
`getenv (var)`
65+
66+
Return the value of the environment variable `var`, or `nil` if there
67+
is no such value.
68+
69+
Parameters:
70+
71+
`var`:
72+
: name of the environment variable (string)
73+
74+
Returns:
75+
76+
- value of the variable, or nil if the variable is not defined (string
77+
or nil).
78+
5179
### ls {#system-ls}
5280

5381
`ls ([directory])`
@@ -59,7 +87,7 @@ Parameters:
5987
`directory`:
6088
: Path of the directory whose contents should be listed (string).
6189
Defaults to `.`.
62-
90+
6391
Returns:
6492

6593
- A table of all entries in `directory` without the special entries (`.`
@@ -71,6 +99,20 @@ Returns:
7199

72100
An alias for [`currentdir`](#system-currentdir)
73101

102+
### setenv {#system-setenv}
103+
104+
`setenv (var, value)`
105+
106+
Set the specified environment variable to a new value.
107+
108+
Parameters:
109+
110+
`var`:
111+
: name of the environment variable (string).
112+
113+
`value`:
114+
: new value (string).
115+
74116
### tmpdirname {#system-tmpdirname}
75117

76118
`tmpdirname ()`
@@ -91,7 +133,7 @@ The operation may fail if the operating system has no notion of
91133
temporary directory.
92134

93135
The function doesn't verify whether the path exists.
94-
136+
95137
Returns:
96138

97139
- The current directory for temporary files (string).
@@ -116,7 +158,7 @@ Parameters:
116158
`callback`:
117159
: Function which takes the name of the temporary directory as its
118160
first argument (function).
119-
161+
120162
Returns:
121163

122164
- The result of the call to `callback`.

src/Foreign/Lua/Module/System.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import System.IO.Error (IOError, isDoesNotExistError)
2525

2626
import qualified Foreign.Lua as Lua
2727
import qualified System.Directory as Directory
28+
import qualified System.Environment as Env
2829
import qualified System.IO.Temp as Temp
2930

3031
-- | Pushes the @text@ module to the lua stack.
@@ -33,8 +34,11 @@ pushModule = do
3334
Lua.newtable
3435
addFunction "chdir" chdir
3536
addFunction "currentdir" currentdir
37+
addFunction "env" env
38+
addFunction "getenv" getenv
3639
addFunction "ls" ls
3740
addFunction "pwd" currentdir
41+
addFunction "setenv" setenv
3842
addFunction "tmpdirname" tmpdirname
3943
addFunction "with_tmpdir" with_tmpdir
4044
return 1
@@ -135,6 +139,23 @@ chdir fp = ioToLua $ Directory.setCurrentDirectory fp
135139
currentdir :: Lua FilePath
136140
currentdir = ioToLua Directory.getCurrentDirectory
137141

142+
-- | Retrieve the entire environment
143+
env :: Lua NumResults
144+
env = do
145+
kvs <- ioToLua Env.getEnvironment
146+
let addValue (k, v) = Lua.push k *> Lua.push v *> Lua.rawset (-3)
147+
Lua.newtable
148+
mapM_ addValue kvs
149+
return (NumResults 1)
150+
151+
-- | Returns the value of an environment variable
152+
getenv :: String -> Lua (Optional String)
153+
getenv name = ioToLua (Optional <$> Env.lookupEnv name)
154+
155+
-- | Set the specified environment variable to a new value.
156+
setenv :: String -> String -> Lua ()
157+
setenv name value = ioToLua (Env.setEnv name value)
158+
138159
-- | Convert a System IO operation to a Lua operation.
139160
ioToLua :: IO a -> Lua a
140161
ioToLua action = do

test/system-module-tests.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ assert(system.with_tmpdir('foo', write_read_token) == token)
1919
-- tmpdirname
2020
assert(type(system.tmpdirname()) == 'string', "tmpdirname should return a string")
2121

22+
-- env
23+
assert(type(system.env()) == 'table')
24+
2225
-- ls
2326
assert(type(system.ls('.')) == 'table')
2427
assert(#system.ls('.') == #system.ls())
@@ -52,3 +55,7 @@ function create_then_count_files ()
5255
end
5356

5457
assert(in_tmpdir(create_then_count_files) == 1, 'Number of files should be 1')
58+
59+
system.setenv('TESTING', token)
60+
assert(system.getenv 'TESTING' == token,
61+
'setting and getting env var is inconsistent')

0 commit comments

Comments
 (0)