From: Tobias Ulmer Date: Thu, 24 Oct 2013 16:45:58 +0000 (+0200) Subject: Merge read_file() and read_template() into a local function X-Git-Tag: e2factory-2.3.15rc1~438 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=2b26fccc0840d83e9e01af48774ce9dba68bcfd1;p=e2factory.git Merge read_file() and read_template() into a local function Signed-off-by: Tobias Ulmer --- diff --git a/generic/e2lib.lua b/generic/e2lib.lua index d72b508..aecd590 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -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 diff --git a/global/e2-create-project.lua b/global/e2-create-project.lua index 4667e56..fa26ff0 100644 --- a/global/e2-create-project.lua +++ b/global/e2-create-project.lua @@ -29,12 +29,49 @@ ]] 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