From 29513caaca2bfdf197e3bbdc27c24f0081639fbd Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 3 Apr 2019 13:57:46 -0300 Subject: persist: save_default_lua_version refactor into a separate function --- src/luarocks/cmd/config.lua | 9 +-------- src/luarocks/persist.lua | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index 5c7cc6c9..e9b2fef4 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua @@ -278,17 +278,10 @@ function config_cmd.command(flags, var, val) end local prefix = dir.dir_name(cfg.config_files[scope].file) - local ok, err = fs.make_dir(prefix) + local ok, err = persist.save_default_lua_version(prefix, val) if not ok then return nil, "could not set default Lua version: " .. err end - local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") - if not fd then - return nil, "could not set default Lua version: " .. err - end - - fd:write('return "' .. val .. '"\n') - fd:close() print("Lua version will default to " .. val .. " in " .. prefix) end diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index b8060ea0..1c4b82b5 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua @@ -1,12 +1,12 @@ --- Utility module for loading files into tables and -- saving tables into files. --- Implemented separately to avoid interdependencies, --- as it is used in the bootstrapping stage of the cfg module. local persist = {} local core = require("luarocks.core.persist") local util = require("luarocks.util") +local dir = require("luarocks.dir") +local fs = require("luarocks.fs") persist.load_into_table = core.load_into_table @@ -239,4 +239,18 @@ function persist.load_config_file_if_basic(filename, cfg) return tbl end +function persist.save_default_lua_version(prefix, lua_version) + local ok, err = fs.make_dir(prefix) + if not ok then + return nil, err + end + local fd, err = io.open(dir.path(prefix, "default-lua-version.lua"), "w") + if not fd then + return nil, err + end + fd:write('return "' .. lua_version .. '"\n') + fd:close() + return true +end + return persist -- cgit v1.2.3-55-g6feb