From: Tobias Ulmer Date: Sun, 12 Jan 2014 03:02:21 +0000 (+0100) Subject: environment: add tofile function X-Git-Tag: e2factory-2.3.15rc1~269 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=eee9d6c2a554fa0f4a486e9b8cd94ea203ee5974;p=e2factory.git environment: add tofile function tofile() creates a shell-sourceable file containing the environment. Signed-off-by: Tobias Ulmer --- diff --git a/local/environment.lua b/local/environment.lua index 97a0bf1..86cb80a 100644 --- a/local/environment.lua +++ b/local/environment.lua @@ -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(...)