diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index f72ea42cbbfbd..6c5ff7675f758 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -25,6 +25,7 @@ local profile = require("apisix.core.profile") local template = require("resty.template") local argparse = require("argparse") local pl_path = require("pl.path") +local lfs = require("lfs") local signal = require("posix.signal") local errno = require("posix.errno") @@ -38,6 +39,7 @@ local tonumber = tonumber local io_open = io.open local execute = os.execute local os_rename = os.rename +local os_remove = os.remove local table_insert = table.insert local getenv = os.getenv local max = math.max @@ -469,9 +471,8 @@ Please modify "admin_key" in conf/config.yaml . -- Therefore we need to check the absolute version instead cert_path = pl_path.abspath(cert_path) - local ok, err = util.is_file_exist(cert_path) - if not ok then - util.die(err, "\n") + if not pl_path.exists(cert_path) then + util.die("certificate path", cert_path, "doesn't exist\n") end yaml_conf.apisix.ssl.ssl_trusted_certificate = cert_path @@ -763,16 +764,19 @@ local function start(env, ...) if customized_yaml then profile.apisix_home = env.apisix_home .. "/" local local_conf_path = profile:yaml_path("config") + local local_conf_path_bak = local_conf_path .. ".bak" - local err = util.execute_cmd_with_error("mv " .. local_conf_path .. " " - .. local_conf_path .. ".bak") - if #err > 0 then - util.die("failed to mv config to backup, error: ", err) + local ok, err = os_rename(local_conf_path, local_conf_path_bak) + if not ok then + util.die("failed to backup config, error: ", err) end - err = util.execute_cmd_with_error("ln " .. customized_yaml .. " " .. local_conf_path) - if #err > 0 then - util.execute_cmd("mv " .. local_conf_path .. ".bak " .. local_conf_path) - util.die("failed to link customized config, error: ", err) + local ok, err1 = lfs.link(customized_yaml, local_conf_path) + if not ok then + ok, err = os_rename(local_conf_path_bak, local_conf_path) + if not ok then + util.die("failed to recover original config file, error: ", err) + end + util.die("failed to link customized config, error: ", err1) end print("Use customized yaml: ", customized_yaml) @@ -787,15 +791,15 @@ end local function cleanup() local local_conf_path = profile:yaml_path("config") - local bak_exist = io_open(local_conf_path .. ".bak") - if bak_exist then - local err = util.execute_cmd_with_error("rm " .. local_conf_path) - if #err > 0 then + local local_conf_path_bak = local_conf_path .. ".bak" + if pl_path.exists(local_conf_path_bak) then + local ok, err = os_remove(local_conf_path) + if not ok then print("failed to remove customized config, error: ", err) end - err = util.execute_cmd_with_error("mv " .. local_conf_path .. ".bak " .. local_conf_path) - if #err > 0 then - util.die("failed to mv original config file, error: ", err) + ok, err = os_rename(local_conf_path_bak, local_conf_path) + if not ok then + util.die("failed to recover original config file, error: ", err) end end end @@ -804,9 +808,10 @@ end local function test(env, backup_ngx_conf) -- backup nginx.conf local ngx_conf_path = env.apisix_home .. "/conf/nginx.conf" - local ngx_conf_exist = util.is_file_exist(ngx_conf_path) + local ngx_conf_path_bak = ngx_conf_path .. ".bak" + local ngx_conf_exist = pl_path.exists(ngx_conf_path) if ngx_conf_exist then - local ok, err = os_rename(ngx_conf_path, ngx_conf_path .. ".bak") + local ok, err = os_rename(ngx_conf_path, ngx_conf_path_bak) if not ok then util.die("failed to backup nginx.conf, error: ", err) end @@ -820,7 +825,7 @@ local function test(env, backup_ngx_conf) -- restore nginx.conf if ngx_conf_exist then - local ok, err = os_rename(ngx_conf_path .. ".bak", ngx_conf_path) + local ok, err = os_rename(ngx_conf_path_bak, ngx_conf_path) if not ok then util.die("failed to restore original nginx.conf, error: ", err) end diff --git a/apisix/cli/util.lua b/apisix/cli/util.lua index bab857d9d113c..daadee51e5581 100644 --- a/apisix/cli/util.lua +++ b/apisix/cli/util.lua @@ -121,18 +121,4 @@ function _M.write_file(file_path, data) end -function _M.is_file_exist(file_path) - local file, err = open(file_path) - if not file then - return false, "failed to open file: " - .. file_path - .. ", error info: " - .. err - end - - file:close() - return true -end - - return _M