Merge pull request #67135 from azat/local/config-from-home

Read configuration for clickhouse-local from ~/.clickhouse-local
This commit is contained in:
vdimir 2024-08-05 09:44:22 +00:00 committed by GitHub
commit 7add8ecc7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 65 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#include "LocalServer.h"
#include <sys/resource.h>
#include <Common/Config/getLocalConfigPath.h>
#include <Common/logger_useful.h>
#include <Common/formatReadable.h>
#include <Core/UUID.h>
@ -127,10 +128,21 @@ void LocalServer::initialize(Poco::Util::Application & self)
{
Poco::Util::Application::initialize(self);
const char * home_path_cstr = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
if (home_path_cstr)
home_path = home_path_cstr;
/// Load config files if exists
if (getClientConfiguration().has("config-file") || fs::exists("config.xml"))
std::string config_path;
if (getClientConfiguration().has("config-file"))
config_path = getClientConfiguration().getString("config-file");
else if (config_path.empty() && fs::exists("config.xml"))
config_path = "config.xml";
else if (config_path.empty())
config_path = getLocalConfigPath(home_path).value_or("");
if (fs::exists(config_path))
{
const auto config_path = getClientConfiguration().getString("config-file", "config.xml");
ConfigProcessor config_processor(config_path, false, true);
ConfigProcessor::setConfigPath(fs::path(config_path).parent_path());
auto loaded_config = config_processor.loadConfig();

View File

@ -2,6 +2,7 @@ set (SRCS
AbstractConfigurationComparison.cpp
ConfigProcessor.cpp
getClientConfigPath.cpp
getLocalConfigPath.cpp
ConfigReloader.cpp
YAMLParser.cpp
ConfigHelper.cpp

View File

@ -12,7 +12,6 @@ namespace DB
std::optional<std::string> getClientConfigPath(const std::string & home_path)
{
std::string config_path;
bool found = false;
std::vector<std::string> names;
names.emplace_back("./clickhouse-client");
@ -28,18 +27,10 @@ std::optional<std::string> getClientConfigPath(const std::string & home_path)
std::error_code ec;
if (fs::exists(config_path, ec))
{
found = true;
break;
}
return config_path;
}
if (found)
break;
}
if (found)
return config_path;
return std::nullopt;
}

View File

@ -0,0 +1,37 @@
#include <Common/Config/getLocalConfigPath.h>
#include <filesystem>
#include <vector>
namespace fs = std::filesystem;
namespace DB
{
std::optional<std::string> getLocalConfigPath(const std::string & home_path)
{
std::string config_path;
std::vector<std::string> names;
names.emplace_back("./clickhouse-local");
if (!home_path.empty())
names.emplace_back(home_path + "/.clickhouse-local/config");
names.emplace_back("/etc/clickhouse-local/config");
for (const auto & name : names)
{
for (const auto & extension : {".xml", ".yaml", ".yml"})
{
config_path = name + extension;
std::error_code ec;
if (fs::exists(config_path, ec))
return config_path;
}
}
return std::nullopt;
}
}

View File

@ -0,0 +1,12 @@
#pragma once
#include <string>
#include <optional>
namespace DB
{
/// Return path to existing configuration file.
std::optional<std::string> getLocalConfigPath(const std::string & home_path);
}