]> git.e2factory.org Git - e2factory.git/commitdiff
environment: add tofile function
authorTobias Ulmer <tu@emlix.com>
Sun, 12 Jan 2014 03:02:21 +0000 (04:02 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:17 +0000 (15:41 +0100)
tofile() creates a shell-sourceable file containing the environment.

Signed-off-by: Tobias Ulmer <tu@emlix.com>
local/environment.lua

index 97a0bf1be4db6409184ef3066245c85ab6afd4a7..86cb80a5021851d79a7eb5bd9ce8b9cb5c79b19f 100644 (file)
@@ -29,6 +29,9 @@
 ]]
 
 local environment = {}
+local e2lib = require("e2lib")
+local eio = require("eio")
+local err = require("err")
 local hash = require("hash")
 local strict = require("strict")
 
@@ -105,6 +108,30 @@ function environment.get_dict(env)
     return dict
 end
 
+--- Write environment as key=value\n... string to file. File is created or
+-- truncated. Value is quoted with e2lib.shquote() so the file can be sourced
+-- by a shell.
+-- @param env Environment.
+-- @param file File name.
+-- @return True on success, false on error.
+-- @return Error object on failure.
+function environment.tofile(env, file)
+    local rc, re, e, out
+
+    out = {}
+    for var, val in env:iter() do
+        table.insert(out, string.format("%s=%s\n", var, e2lib.shquote(val)))
+    end
+
+    rc, re = eio.file_write(file, table.concat(out))
+    if not rc then
+        e = err.new("writing environment script")
+        return false, e:cat(re)
+    end
+
+    return true
+end
+
 local function unittest()
     local function p(...)
         --print(...)