<commit_before>--[[
Kong CLI utilities
 - Colorization
 - Logging
 - Disk I/O utils
 - nginx path/initialization
]]

local path = require("path").new("/")
local utils = require "kong.tools.utils"
local Object = require "classic"
local ansicolors = require "ansicolors"

local CLI_CONSTANTS = {
  GLOBAL_KONG_CONF = "/etc/kong/kong.yml",
  NGINX_CONFIG = "nginx.conf",
  NGINX_PID = "kong.pid"
}

--
-- Colors
--
local colors = {}
for _, v in ipairs({"red", "green", "yellow"}) do
  colors[v] = function(str) return ansicolors("%{"..v.."}"..str.."%{reset}") end
end

--
-- Logging
--
local Logger = Object:extend()

function Logger:new(silent)
  self.silent = silent
end

function Logger:log(str)
  if not self.silent then
    print(str)
  end
end

function Logger:success(str)
  self:log(colors.green("[SUCCESS] ")..str)
end

function Logger:warn(str)
  self:log(colors.yellow("[WARNING] ")..str)
end

function Logger:error(str)
  self:log(colors.red("[ERROR] ")..str)
end

function Logger:error_exit(str)
  self:error(str)
  os.exit(1)
end

local function get_infos()
  local constants = require "kong.constants"
  return { name = constants.NAME, version = constants.VERSION }
end

local logger = Logger()

local function retrieve_files(dir, pattern)
  local fs = require "luarocks.fs"

  if not pattern then pattern = "" end
  local files = {}

  local function tree(dir)
    for _, file in ipairs(fs.list_dir(dir)) do
      local f = path:join(dir, file)
      if fs.is_dir(f) then
        tree(f)
      elseif fs.is_file(f) and string.match(file, pattern) ~= nil then
        table.insert(files, f)
      end
    end
  end

  tree(dir)

  return files
end

--
-- NGINX
--
local function is_openresty(path_to_check)
  local cmd = tostring(path_to_check).." -v 2>&1"
  local handle = io.popen(cmd)
  local out = handle:read()
  handle:close()
  local matched = out:match("^nginx version: ngx_openresty/") or out:match("^nginx version: openresty/")
  if matched then
    return path_to_check
  end
end

local function find_nginx()
  local nginx_bin = "nginx"
  local nginx_search_paths = {
    "/usr/local/openresty/nginx/sbin/",
    "/usr/local/opt/openresty/bin/",
    "/usr/local/bin/",
    "/usr/sbin/",
    ""
  }

  for i = 1, #nginx_search_paths do
    local prefix = nginx_search_paths[i]
    local to_check = tostring(prefix)..tostring(nginx_bin)
    if is_openresty(to_check) then
      return to_check
    end
  end
end

local function prepare_nginx_working_dir(kong_config)
  if kong_config.send_anonymous_reports then
    kong_config.nginx = "error_log syslog:server=kong-hf.mashape.com:61828 error;\n"..kong_config.nginx
  end

  -- Create nginx folder if needed
  path:mkdir(path:join(kong_config.nginx_working_dir, "logs"))
  os.execute("touch "..path:join(kong_config.nginx_working_dir, "logs", "error.log"))
  os.execute("touch "..path:join(kong_config.nginx_working_dir, "logs", "access.log"))

  -- Extract nginx config to nginx folder
  utils.write_to_file(path:join(kong_config.nginx_working_dir, CLI_CONSTANTS.NGINX_CONFIG), kong_config.nginx)

  return kong_config.nginx_working_dir
end

local function get_luarocks_config_dir()
  local cfg = require "luarocks.cfg"
  local lpath = require "luarocks.path"
  local search = require "luarocks.search"
  local infos = get_infos()

  local tree_map = {}
  local results = {}

  for _, tree in ipairs(cfg.rocks_trees) do
    local rocks_dir = lpath.rocks_dir(tree)
    tree_map[rocks_dir] = tree
    search.manifest_search(results, rocks_dir, search.make_query(infos.name:lower(), nil))
  end

  local version
  for k, _ in pairs(results.kong) do
    version = k
  end

  local repo = tree_map[results.kong[version][1].repo]
  return lpath.conf_dir(infos.name:lower(), infos.version, repo)
end

local function get_kong_config_path(args_config)
  -- Use the rock's config if no config at default location
  if not utils.file_exists(args_config) then
    local kong_rocks_conf = path:join(get_luarocks_config_dir(), "kong.yml")
    logger:warn("No config at: "..args_config.." using default config instead.")
    args_config = kong_rocks_conf
  end

  -- Make sure the configuration file really exists
  if not utils.file_exists(args_config) then
    logger:warn("No config at: "..args_config)
    logger:error_exit("Could not find a configuration file.")
  end

  logger:log("Using config: "..args_config.."\n")

  -- TODO: validate configuration
  --[[local status, res = pcall(require, "kong.dao."..config.database..".factory")
    if not status then
      cutils.logger:error("Wrong config")
      os.exit(1)
    end]]

  return args_config
end

return {
  CONSTANTS = CLI_CONSTANTS,
  path = path,
  colors = colors,
  logger = logger,

  get_infos = get_infos,
  find_nginx = find_nginx,
  is_openresty = is_openresty,
  read_file = utils.read_file,
  retrieve_files = retrieve_files,
  file_exists = utils.file_exists,
  write_to_file = utils.write_to_file,
  get_kong_config_path = get_kong_config_path,
  prepare_nginx_working_dir = prepare_nginx_working_dir,
  load_configuration_and_dao = utils.load_configuration_and_dao
}
<commit_msg>adding permission error<commit_after>--[[
Kong CLI utilities
 - Colorization
 - Logging
 - Disk I/O utils
 - nginx path/initialization
]]

local path = require("path").new("/")
local utils = require "kong.tools.utils"
local Object = require "classic"
local ansicolors = require "ansicolors"

local CLI_CONSTANTS = {
  GLOBAL_KONG_CONF = "/etc/kong/kong.yml",
  NGINX_CONFIG = "nginx.conf",
  NGINX_PID = "kong.pid"
}

--
-- Colors
--
local colors = {}
for _, v in ipairs({"red", "green", "yellow"}) do
  colors[v] = function(str) return ansicolors("%{"..v.."}"..str.."%{reset}") end
end

--
-- Logging
--
local Logger = Object:extend()

function Logger:new(silent)
  self.silent = silent
end

function Logger:log(str)
  if not self.silent then
    print(str)
  end
end

function Logger:success(str)
  self:log(colors.green("[SUCCESS] ")..str)
end

function Logger:warn(str)
  self:log(colors.yellow("[WARNING] ")..str)
end

function Logger:error(str)
  self:log(colors.red("[ERROR] ")..str)
end

function Logger:error_exit(str)
  self:error(str)
  os.exit(1)
end

local function get_infos()
  local constants = require "kong.constants"
  return { name = constants.NAME, version = constants.VERSION }
end

local logger = Logger()

local function retrieve_files(dir, pattern)
  local fs = require "luarocks.fs"

  if not pattern then pattern = "" end
  local files = {}

  local function tree(dir)
    for _, file in ipairs(fs.list_dir(dir)) do
      local f = path:join(dir, file)
      if fs.is_dir(f) then
        tree(f)
      elseif fs.is_file(f) and string.match(file, pattern) ~= nil then
        table.insert(files, f)
      end
    end
  end

  tree(dir)

  return files
end

--
-- NGINX
--
local function is_openresty(path_to_check)
  local cmd = tostring(path_to_check).." -v 2>&1"
  local handle = io.popen(cmd)
  local out = handle:read()
  handle:close()
  local matched = out:match("^nginx version: ngx_openresty/") or out:match("^nginx version: openresty/")
  if matched then
    return path_to_check
  end
end

local function find_nginx()
  local nginx_bin = "nginx"
  local nginx_search_paths = {
    "/usr/local/openresty/nginx/sbin/",
    "/usr/local/opt/openresty/bin/",
    "/usr/local/bin/",
    "/usr/sbin/",
    ""
  }

  for i = 1, #nginx_search_paths do
    local prefix = nginx_search_paths[i]
    local to_check = tostring(prefix)..tostring(nginx_bin)
    if is_openresty(to_check) then
      return to_check
    end
  end
end

local function prepare_nginx_working_dir(kong_config)
  if kong_config.send_anonymous_reports then
    kong_config.nginx = "error_log syslog:server=kong-hf.mashape.com:61828 error;\n"..kong_config.nginx
  end

  -- Create nginx folder if needed
  local _, err = path:mkdir(path:join(kong_config.nginx_working_dir, "logs"))
  if err then
    logger:error_exit(err)
  end
  os.execute("touch "..path:join(kong_config.nginx_working_dir, "logs", "error.log"))
  os.execute("touch "..path:join(kong_config.nginx_working_dir, "logs", "access.log"))

  -- Extract nginx config to nginx folder
  utils.write_to_file(path:join(kong_config.nginx_working_dir, CLI_CONSTANTS.NGINX_CONFIG), kong_config.nginx)

  return kong_config.nginx_working_dir
end

local function get_luarocks_config_dir()
  local cfg = require "luarocks.cfg"
  local lpath = require "luarocks.path"
  local search = require "luarocks.search"
  local infos = get_infos()

  local tree_map = {}
  local results = {}

  for _, tree in ipairs(cfg.rocks_trees) do
    local rocks_dir = lpath.rocks_dir(tree)
    tree_map[rocks_dir] = tree
    search.manifest_search(results, rocks_dir, search.make_query(infos.name:lower(), nil))
  end

  local version
  for k, _ in pairs(results.kong) do
    version = k
  end

  local repo = tree_map[results.kong[version][1].repo]
  return lpath.conf_dir(infos.name:lower(), infos.version, repo)
end

local function get_kong_config_path(args_config)
  -- Use the rock's config if no config at default location
  if not utils.file_exists(args_config) then
    local kong_rocks_conf = path:join(get_luarocks_config_dir(), "kong.yml")
    logger:warn("No config at: "..args_config.." using default config instead.")
    args_config = kong_rocks_conf
  end

  -- Make sure the configuration file really exists
  if not utils.file_exists(args_config) then
    logger:warn("No config at: "..args_config)
    logger:error_exit("Could not find a configuration file.")
  end

  logger:log("Using config: "..args_config.."\n")

  -- TODO: validate configuration
  --[[local status, res = pcall(require, "kong.dao."..config.database..".factory")
    if not status then
      cutils.logger:error("Wrong config")
      os.exit(1)
    end]]

  return args_config
end

return {
  CONSTANTS = CLI_CONSTANTS,
  path = path,
  colors = colors,
  logger = logger,

  get_infos = get_infos,
  find_nginx = find_nginx,
  is_openresty = is_openresty,
  read_file = utils.read_file,
  retrieve_files = retrieve_files,
  file_exists = utils.file_exists,
  write_to_file = utils.write_to_file,
  get_kong_config_path = get_kong_config_path,
  prepare_nginx_working_dir = prepare_nginx_working_dir,
  load_configuration_and_dao = utils.load_configuration_and_dao
}

{'fraction_non_alphanumeric': 0.12240341681226946, 'fraction_numerical': 0.0021355076684139, 'mean_word_length': 3.920248328557784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5478139', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3698, 'n_words': 835}