diff --git a/engine/utils/config_yaml_utils.h b/engine/utils/config_yaml_utils.h index 8e3668292..f7cf42141 100644 --- a/engine/utils/config_yaml_utils.h +++ b/engine/utils/config_yaml_utils.h @@ -45,7 +45,7 @@ inline void DumpYamlConfig(const CortexConfig& config, } inline CortexConfig FromYaml(const std::string& path, - const std::string& variant) { + const CortexConfig& default_cfg) { std::filesystem::path config_file_path{path}; if (!std::filesystem::exists(config_file_path)) { throw std::runtime_error("File not found: " + path); @@ -60,11 +60,20 @@ inline CortexConfig FromYaml(const std::string& path, max_lines = node["maxLogLines"].as(); } CortexConfig config = { - .logFolderPath = node["logFolderPath"].as(), - .dataFolderPath = node["dataFolderPath"].as(), - .maxLogLines = max_lines, - .apiServerHost = node["apiServerHost"].as(), - .apiServerPort = node["apiServerPort"].as(), + .logFolderPath = node["logFolderPath"] + ? node["logFolderPath"].as() + : default_cfg.logFolderPath, + .dataFolderPath = node["dataFolderPath"] + ? node["dataFolderPath"].as() + : default_cfg.dataFolderPath, + .maxLogLines = node["maxLogLines"] ? node["maxLogLines"].as() + : default_cfg.maxLogLines, + .apiServerHost = node["apiServerHost"] + ? node["apiServerHost"].as() + : default_cfg.apiServerHost, + .apiServerPort = node["apiServerPort"] + ? node["apiServerPort"].as() + : default_cfg.apiServerPort, }; return config; } catch (const YAML::BadFile& e) { diff --git a/engine/utils/file_manager_utils.h b/engine/utils/file_manager_utils.h index 57ce2f917..07a2ef151 100644 --- a/engine/utils/file_manager_utils.h +++ b/engine/utils/file_manager_utils.h @@ -109,12 +109,7 @@ inline std::filesystem::path GetConfigurationPath() { return configuration_path; } -inline void CreateConfigFileIfNotExist() { - auto config_path = GetConfigurationPath(); - if (std::filesystem::exists(config_path)) { - // already exists - return; - } +inline std::string GetDefaultDataFolderName() { #ifndef CORTEX_VARIANT #define CORTEX_VARIANT "prod" #endif @@ -128,6 +123,17 @@ inline void CreateConfigFileIfNotExist() { env_postfix.append("-").append(kNightlyVariant); } default_data_folder_name.append(env_postfix); + return default_data_folder_name; +} + +inline void CreateConfigFileIfNotExist() { + auto config_path = GetConfigurationPath(); + if (std::filesystem::exists(config_path)) { + // already exists + return; + } + + auto default_data_folder_name = GetDefaultDataFolderName(); CLI_LOG("Config file not found. Creating one at " + config_path.string()); auto defaultDataFolderPath = @@ -146,8 +152,18 @@ inline void CreateConfigFileIfNotExist() { inline config_yaml_utils::CortexConfig GetCortexConfig() { auto config_path = GetConfigurationPath(); - std::string variant = ""; // TODO: empty for now - return config_yaml_utils::FromYaml(config_path.string(), variant); + auto default_data_folder_name = GetDefaultDataFolderName(); + auto default_data_folder_path = + file_manager_utils::GetHomeDirectoryPath() / default_data_folder_name; + auto default_cfg = config_yaml_utils::CortexConfig{ + .logFolderPath = default_data_folder_path.string(), + .dataFolderPath = default_data_folder_path.string(), + .maxLogLines = config_yaml_utils::kDefaultMaxLines, + .apiServerHost = config_yaml_utils::kDefaultHost, + .apiServerPort = config_yaml_utils::kDefaultPort, + }; + + return config_yaml_utils::FromYaml(config_path.string(), default_cfg); } inline std::filesystem::path GetCortexDataPath() {