Skip to content

Commit

Permalink
Use top_level_folder after it is completely set up (#1090)
Browse files Browse the repository at this point in the history
* fix top_level_folder setting

* separate program settings functions
  • Loading branch information
jolopezl authored Apr 28, 2022
1 parent be96389 commit 9a00203
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
75 changes: 41 additions & 34 deletions src/Main/ProgramSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,9 @@ ProgramSettings::ProgramSettings(int argc, char** argv) {
debug_msgs.push_back("Using command-line settings");
}
ApplyCommandLineSettings(argc, argv);

user_directory = fs::path(getenv("HOME")) / CARTA_USER_FOLDER_PREFIX;
const fs::path user_settings_path = user_directory / "backend.json";
const fs::path system_settings_path = "/etc/carta/backend.json";

json settings;
std::error_code error_code;

if (!no_system_config && fs::exists(system_settings_path, error_code)) {
settings = JSONSettingsFromFile(system_settings_path.string());
system_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading system settings from {}.", system_settings_path.string()));
}

if (!no_user_config && fs::exists(user_settings_path, error_code)) {
auto user_settings = JSONSettingsFromFile(user_settings_path.string());
user_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading user settings from {}.", user_settings_path.string()));
settings.merge_patch(user_settings); // user on top of system
}

if (system_settings_json_exists || user_settings_json_exists) {
settings.merge_patch(command_line_settings); // force command-line on top of user and sytem
SetSettingsFromJSON(settings);
}
ApplyJSONSettings();
// Push files after all settings are applied
PushFilePaths();

// Apply deprecated no_http flag
if (no_http) {
Expand Down Expand Up @@ -342,7 +320,6 @@ global configuration files, respectively.

// base will be overridden by the positional argument if it exists and is a folder
applyOptionalArgument(starting_folder, "base", result);
std::vector<fs::path> file_paths;

for (const auto& arg : positional_arguments) {
fs::path p(arg);
Expand Down Expand Up @@ -372,14 +349,6 @@ global configuration files, respectively.
file_paths.clear();
}
}
if (file_paths.size()) {
// Calculate paths relative to top level folder
auto top_level_path = fs::absolute(top_level_folder).lexically_normal();
for (const auto& p : file_paths) {
auto relative_path = fs::absolute(p).lexically_normal().lexically_relative(top_level_path);
files.push_back(relative_path.string());
}
}

// produce JSON for overridding system and user configuration;
// Options here need to match all options available for system and user settings
Expand All @@ -406,6 +375,44 @@ global configuration files, respectively.
}
}

void ProgramSettings::ApplyJSONSettings() {
user_directory = fs::path(getenv("HOME")) / CARTA_USER_FOLDER_PREFIX;
const fs::path user_settings_path = user_directory / "backend.json";
const fs::path system_settings_path = "/etc/carta/backend.json";

json settings;
std::error_code error_code;

if (!no_system_config && fs::exists(system_settings_path, error_code)) {
settings = JSONSettingsFromFile(system_settings_path.string());
system_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading system settings from {}.", system_settings_path.string()));
}

if (!no_user_config && fs::exists(user_settings_path, error_code)) {
auto user_settings = JSONSettingsFromFile(user_settings_path.string());
user_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading user settings from {}.", user_settings_path.string()));
settings.merge_patch(user_settings); // user on top of system
}

if (system_settings_json_exists || user_settings_json_exists) {
settings.merge_patch(command_line_settings); // force command-line on top of user and sytem
SetSettingsFromJSON(settings);
}
}

void ProgramSettings::PushFilePaths() {
if (file_paths.size()) {
// Calculate paths relative to top level folder
auto top_level_path = fs::absolute(top_level_folder).lexically_normal();
for (const auto& p : file_paths) {
auto relative_path = fs::absolute(p).lexically_normal().lexically_relative(top_level_path);
files.push_back(relative_path.string());
}
}
}

void ProgramSettings::AddDeprecationWarning(const std::string& option, std::string where) {
auto message = deprecated_options.at(option);
warning_msgs.push_back(fmt::format("Option {} found in {} is deprecated. {}", option, where, message));
Expand Down
9 changes: 6 additions & 3 deletions src/Main/ProgramSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
SPDX-License-Identifier: GPL-3.0-or-later
*/

#ifndef CARTA_BACKEND_SRC_SESSIONMANAGER_PROGRAMSETTINGS_H_
#define CARTA_BACKEND_SRC_SESSIONMANAGER_PROGRAMSETTINGS_H_
#ifndef CARTA_BACKEND_SRC_MAIN_PROGRAMSETTINGS_H_
#define CARTA_BACKEND_SRC_MAIN_PROGRAMSETTINGS_H_

#include <iostream>
#include <string>
Expand Down Expand Up @@ -40,6 +40,7 @@ struct ProgramSettings {
std::string starting_folder = ".";
std::string host = "0.0.0.0";
std::vector<std::string> files;
std::vector<fs::path> file_paths;
std::string frontend_folder;
bool no_http = false; // Deprecated
bool no_frontend = false;
Expand Down Expand Up @@ -112,9 +113,11 @@ struct ProgramSettings {
ProgramSettings() = default;
ProgramSettings(int argc, char** argv);
void ApplyCommandLineSettings(int argc, char** argv);
void ApplyJSONSettings();
void AddDeprecationWarning(const std::string& option, std::string where);
nlohmann::json JSONSettingsFromFile(const std::string& fsp);
void SetSettingsFromJSON(const nlohmann::json& j);
void PushFilePaths();

// TODO: this is outdated. It's used by the equality operator, which is used by a test.
auto GetTuple() const {
Expand All @@ -135,4 +138,4 @@ struct ProgramSettings {
}
};
} // namespace carta
#endif // CARTA_BACKEND_SRC_SESSIONMANAGER_PROGRAMSETTINGS_H_
#endif // CARTA_BACKEND_SRC_MAIN_PROGRAMSETTINGS_H_
1 change: 1 addition & 0 deletions test/TestProgramSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ProgramSettingsTest : public ::testing::Test, public FileFinder {

carta::ProgramSettings settings;
settings.ApplyCommandLineSettings(argVector.size(), cstrings.data());
settings.PushFilePaths();
return std::move(settings);
}

Expand Down

0 comments on commit 9a00203

Please sign in to comment.