]> git.e2factory.org Git - e2factory.git/commitdiff
Merge read_file() and read_template() into a local function
authorTobias Ulmer <tu@emlix.com>
Thu, 24 Oct 2013 16:45:58 +0000 (18:45 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:01:23 +0000 (15:01 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
global/e2-create-project.lua

index d72b508d0a739ae87084c2c394df2e331dd5d69f..aecd590fd0421582def9ec3c2c76afb5263ba5e6 100644 (file)
@@ -2168,36 +2168,6 @@ function e2lib.write_file(file, data)
     return true, nil
 end
 
---- read a file into a string
--- @param file string: filename
--- @return string: the file content
--- @return nil, or an error object
-function e2lib.read_file(file)
-    local f, msg = io.open(file, "r")
-    if not f then
-        return nil, err.new("%s", msg)
-    end
-    local s, msg = f:read("*a")
-    f:close()
-    if not s then
-        return nil, err.new("%s", msg)
-    end
-    return s, nil
-end
-
---- read a template file, located relative to the current template directory
--- @param file string: relative filename
--- @return string: the file content
--- @return an error object on failure
-function e2lib.read_template(file)
-    local e = err.new("error reading template file")
-    local filename = string.format("%s/%s", e2lib.globals.template_path, file)
-    local template, re = e2lib.read_file(filename)
-    if not template then
-        return nil, e:cat(re)
-    end
-    return template, nil
-end
 
 --- parse a server:location string, taking a default server into account
 -- @param serverloc string: the string to parse
index 4667e56b67f2fa5a3e5b1ad9ded0ac17caebd290..fa26ff0f4b0358306a823f87a83887212336adde 100644 (file)
 ]]
 
 local e2lib = require("e2lib")
+local eio = require("eio")
 local cache = require("cache")
 local generic_git = require("generic_git")
 local err = require("err")
 local e2option = require("e2option")
 local buildconfig = require("buildconfig")
 
+--- Read a template file, located relative to the current template directory.
+-- @param path Filename relative to the template directory.
+-- @return File contents as a string, or false on error.
+-- @return Error object on failure.
+local function read_template(path)
+    local e, rc, re, file, filename, template, buf
+    e = err.new("error reading template file")
+
+    filename = e2lib.join(e2lib.globals.template_path, path)
+    file, re = eio.fopen(filename, "r")
+    if not file then
+        return false, e:cat(re)
+    end
+
+    template = ""
+    repeat
+        buf, re = eio.fread(file)
+        if not buf then
+            eio.fclose(file)
+            return false, e:cat(re)
+        end
+        template = template .. buf
+    until buf == ""
+
+    rc, re = eio.fclose(file)
+    if not rc then
+        return false, e:cat(re)
+    end
+
+    return template
+end
+
+--- Create a new project.
+-- @param arg Commandline arguments.
+-- @return True on success, false on error.
+-- @return Error object on failure.
 local function e2_create_project(arg)
     local rc, re = e2lib.init()
     if not rc then
@@ -148,23 +185,23 @@ local function e2_create_project(arg)
         return false, e:cat(re)
     end
 
-    local gitignore = e2lib.read_template("gitignore")
+    local gitignore, re = read_template("gitignore")
     if not gitignore then
         return false, re
     end
-    local chroot, re = e2lib.read_template("proj/chroot")
+    local chroot, re = read_template("proj/chroot")
     if not chroot then
         return false, re
     end
-    local licences, re = e2lib.read_template("proj/licences")
+    local licences, re = read_template("proj/licences")
     if not licences then
         return false, re
     end
-    local env, re = e2lib.read_template("proj/env")
+    local env, re = read_template("proj/env")
     if not env then
         return false, re
     end
-    local pconfig, re = e2lib.read_template("proj/config")
+    local pconfig, re = read_template("proj/config")
     if not pconfig then
         return false, re
     end