Skip to content

Commit

Permalink
style(*) remove one-line conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
p0pr0ck5 authored and thibaultcha committed Jun 12, 2017
1 parent 8ca38dc commit c2919fd
Show file tree
Hide file tree
Showing 52 changed files with 512 additions and 170 deletions.
4 changes: 3 additions & 1 deletion kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ function _M.post(params, dao_collection, success)
if err then
return app_helpers.yield_error(err)
else
if success then success(utils.deep_copy(data)) end
if success then
success(utils.deep_copy(data))
end
return responses.send_HTTP_CREATED(data)
end
end
Expand Down
20 changes: 15 additions & 5 deletions kong/cmd/utils/nginx_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ local function send_signal(kong_conf, signal)
log.verbose("sending %s signal to nginx running at %s", signal, kong_conf.nginx_pid)

local code = kill.kill(kong_conf.nginx_pid, "-s "..signal)
if code ~= 0 then return nil, "could not send signal" end
if code ~= 0 then
return nil, "could not send signal"
end

return true
end
Expand Down Expand Up @@ -69,7 +71,9 @@ end

function _M.start(kong_conf)
local nginx_bin, err = find_nginx_bin()
if not nginx_bin then return nil, err end
if not nginx_bin then
return nil, err
end

if kill.is_running(kong_conf.nginx_pid) then
return nil, "nginx is already running in "..kong_conf.prefix
Expand All @@ -80,7 +84,9 @@ function _M.start(kong_conf)
log.debug("starting nginx: %s", cmd)

local ok, _, _, stderr = pl_utils.executeex(cmd)
if not ok then return nil, stderr end
if not ok then
return nil, stderr
end

log.debug("nginx started")

Expand All @@ -101,15 +107,19 @@ function _M.reload(kong_conf)
end

local nginx_bin, err = find_nginx_bin()
if not nginx_bin then return nil, err end
if not nginx_bin then
return nil, err
end

local cmd = fmt("%s -p %s -c %s -s %s",
nginx_bin, kong_conf.prefix, "nginx.conf", "reload")

log.debug("reloading nginx: %s", cmd)

local ok, _, _, stderr = pl_utils.executeex(cmd)
if not ok then return nil, stderr end
if not ok then
return nil, stderr
end

return true
end
Expand Down
60 changes: 45 additions & 15 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ end
local function gen_default_ssl_cert(kong_config, admin)
-- create SSL folder
local ok, err = pl_dir.makepath(pl_path.join(kong_config.prefix, "ssl"))
if not ok then return nil, err end
if not ok then
return nil, err
end

local ssl_cert, ssl_cert_key, ssl_cert_csr
if admin then
Expand Down Expand Up @@ -139,7 +141,9 @@ end

local function get_ulimit()
local ok, _, stdout, stderr = pl_utils.executeex "ulimit -n"
if not ok then return nil, stderr end
if not ok then
return nil, stderr
end
local sanitized_limit = pl_stringx.strip(stdout)
if sanitized_limit:lower():match("unlimited") then
return 65536
Expand All @@ -152,7 +156,9 @@ local function gather_system_infos(compile_env)
local infos = {}

local ulimit, err = get_ulimit()
if not ulimit then return nil, err end
if not ulimit then
return nil, err
end

infos.worker_rlimit = ulimit
infos.worker_connections = math.min(16384, ulimit)
Expand All @@ -174,7 +180,9 @@ local function compile_conf(kong_config, conf_template)
end
if kong_config.nginx_optimizations then
local infos, err = gather_system_infos()
if not infos then return nil, err end
if not infos then
return nil, err
end
compile_env = pl_tablex.merge(compile_env, infos, true) -- union
end

Expand Down Expand Up @@ -203,29 +211,39 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)
if not pl_path.exists(kong_config.prefix) then
log("prefix directory %s not found, trying to create it", kong_config.prefix)
local ok, err = pl_dir.makepath(kong_config.prefix)
if not ok then return nil, err end
if not ok then
return nil, err
end
elseif not pl_path.isdir(kong_config.prefix) then
return nil, kong_config.prefix.." is not a directory"
end

-- create directories in prefix
for _, dir in ipairs {"logs", "serf", "pids"} do
local ok, err = pl_dir.makepath(pl_path.join(kong_config.prefix, dir))
if not ok then return nil, err end
if not ok then
return nil, err
end
end

-- create log files in case they don't already exist
if not pl_path.exists(kong_config.nginx_err_logs) then
local ok, err = pl_file.write(kong_config.nginx_err_logs, "")
if not ok then return nil, err end
if not ok then
return nil, err
end
end
if not pl_path.exists(kong_config.nginx_acc_logs) then
local ok, err = pl_file.write(kong_config.nginx_acc_logs, "")
if not ok then return nil, err end
if not ok then
return nil, err
end
end
if not pl_path.exists(kong_config.nginx_admin_acc_logs) then
local ok, err = pl_file.write(kong_config.nginx_admin_acc_logs, "")
if not ok then return nil, err end
if not ok then
return nil, err
end
end

log.verbose("saving serf identifier to %s", kong_config.serf_node_id)
Expand All @@ -235,7 +253,9 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)
end

local resty_bin, err = find_resty_bin()
if not resty_bin then return nil, err end
if not resty_bin then
return nil, err
end

log.verbose("saving serf shell script handler to %s", kong_config.serf_event)
-- setting serf admin ip
Expand All @@ -247,20 +267,26 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)
local script = fmt(script_template, admin_ip, kong_config.admin_port, resty_bin)
pl_file.write(kong_config.serf_event, script)
local ok, _, _, stderr = pl_utils.executeex("chmod +x "..kong_config.serf_event)
if not ok then return nil, stderr end
if not ok then
return nil, stderr
end

-- generate default SSL certs if needed
if kong_config.ssl and not kong_config.ssl_cert and not kong_config.ssl_cert_key then
log.verbose("SSL enabled, no custom certificate set: using default certificate")
local ok, err = gen_default_ssl_cert(kong_config)
if not ok then return nil, err end
if not ok then
return nil, err
end
kong_config.ssl_cert = kong_config.ssl_cert_default
kong_config.ssl_cert_key = kong_config.ssl_cert_key_default
end
if kong_config.admin_ssl and not kong_config.admin_ssl_cert and not kong_config.admin_ssl_cert_key then
log.verbose("Admin SSL enabled, no custom certificate set: using default certificate")
local ok, err = gen_default_ssl_cert(kong_config, true)
if not ok then return nil, err end
if not ok then
return nil, err
end
kong_config.admin_ssl_cert = kong_config.admin_ssl_cert_default
kong_config.admin_ssl_cert_key = kong_config.admin_ssl_cert_key_default
end
Expand All @@ -284,12 +310,16 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)

-- write NGINX conf
local nginx_conf, err = compile_nginx_conf(kong_config, nginx_template)
if not nginx_conf then return nil, err end
if not nginx_conf then
return nil, err
end
pl_file.write(kong_config.nginx_conf, nginx_conf)

-- write Kong's NGINX conf
local nginx_kong_conf, err = compile_kong_conf(kong_config)
if not nginx_kong_conf then return nil, err end
if not nginx_kong_conf then
return nil, err
end
pl_file.write(kong_config.nginx_kong_conf, nginx_kong_conf)

-- write kong.conf in prefix (for workers and CLI)
Expand Down
24 changes: 18 additions & 6 deletions kong/cmd/utils/serf_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function _M.start(kong_config, dao)

-- make sure Serf is in PATH
local ok, err = check_serf_bin(kong_config)
if not ok then return nil, err end
if not ok then
return nil, err
end

local serf = Serf.new(kong_config, dao)
local args = setmetatable({
Expand All @@ -102,7 +104,9 @@ function _M.start(kong_config, dao)

-- start Serf agent
local ok = pl_utils.execute(cmd)
if not ok then return nil end
if not ok then
return nil
end

log.verbose("waiting for serf agent to be running")

Expand All @@ -127,15 +131,21 @@ function _M.start(kong_config, dao)

-- cleanup current node from cluster to prevent inconsistency of data
local ok, err = serf:cleanup()
if not ok then return nil, err end
if not ok then
return nil, err
end

log.verbose("auto-joining serf cluster")
local ok, err = serf:autojoin()
if not ok then return nil, err end
if not ok then
return nil, err
end

log.verbose("registering serf node in datastore")
local ok, err = serf:add_node()
if not ok then return nil, err end
if not ok then
return nil, err
end

log.verbose("cluster joined and node registered in datastore")

Expand All @@ -146,7 +156,9 @@ function _M.stop(kong_config, dao)
log.verbose("leaving serf cluster")
local serf = Serf.new(kong_config, dao)
local ok, err = serf:leave()
if not ok then return nil, err end
if not ok then
return nil, err
end
log.verbose("left serf cluster")

log.verbose("stopping serf agent at %s", kong_config.serf_pid)
Expand Down
16 changes: 12 additions & 4 deletions kong/conf_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ local function load(path, custom_conf)
local s = pl_stringio.open(kong_default_conf)
local defaults, err = pl_config.read(s)
s:close()
if not defaults then return nil, "could not load default conf: "..err end
if not defaults then
return nil, "could not load default conf: "..err
end

---------------------
-- Configuration file
Expand All @@ -372,7 +374,9 @@ local function load(path, custom_conf)
log.verbose("no config file, skipping loading")
else
local f, err = pl_file.read(path)
if not f then return nil, err end
if not f then
return nil, err
end

log.verbose("reading config file at %s", path)
local s = pl_stringio.open(f)
Expand All @@ -381,7 +385,9 @@ local function load(path, custom_conf)
list_delim = "_blank_" -- mandatory but we want to ignore it
})
s:close()
if not from_file_conf then return nil, err end
if not from_file_conf then
return nil, err
end
end

-----------------------
Expand All @@ -393,7 +399,9 @@ local function load(path, custom_conf)

-- validation
local ok, err, errors = check_and_infer(conf)
if not ok then return nil, err, errors end
if not ok then
return nil, err, errors
end

conf = tablex.merge(conf, defaults) -- intersection (remove extraneous properties)

Expand Down
8 changes: 6 additions & 2 deletions kong/core/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ local function load_targets_into_memory(upstream_id)
log(DEBUG, "fetching targets for upstream: ",tostring(upstream_id))

local target_history, err = singletons.dao.targets:find_all {upstream_id = upstream_id}
if err then return nil, err end
if err then
return nil, err
end

-- perform some raw data updates
for _, target in ipairs(target_history) do
Expand Down Expand Up @@ -221,7 +223,9 @@ local get_balancer = function(target)
order = upstream.orderlist,
dns = dns_client,
})
if not balancer then return balancer, err end
if not balancer then
return balancer, err
end

balancer.__targets_history = {}
balancers[upstream.name] = balancer -- overwrite our existing one
Expand Down
4 changes: 3 additions & 1 deletion kong/core/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ return function(options)
local old_cdef = ffi.cdef
local exists = {}
ffi.cdef = function(def)
if exists[def] then return end
if exists[def] then
return
end
exists[def] = true
return old_cdef(def)
end
Expand Down
Loading

0 comments on commit c2919fd

Please sign in to comment.