From da91c4b3fd12259487c5f82ac151c9e85c865a2f Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 8 Feb 2022 22:54:57 +0100 Subject: [PATCH] Manager: Add config file parsing --- src/config.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 37 ++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 3c6fa20..ade7463 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -4,7 +4,13 @@ * SPDX-License-Identifier: MIT */ +#include // raise +#include // fprintf +#include // ifstream #include +#include + +#include "nlohmann/json.hpp" #include "config.h" @@ -13,6 +19,7 @@ Config::Config(s) , m_workingDirectorySize(m_workingDirectory.string().size()) { findConfigFile(); + parseConfigFile(); } Config::~Config() @@ -36,3 +43,57 @@ void Config::findConfigFile() printf("Found config file @ %s\n", m_config.c_str() + m_workingDirectorySize + 1); #endif } + +void Config::parseConfigFile() +{ + if (m_config.empty()) { + return; + } + + nlohmann::json json; + + std::ifstream file(m_config); + if (!file.is_open()) { + return; + } + + try { + file >> json; + } + catch (...) { + fprintf(stderr, "\033[31;1mConfig:\033[0m json syntax error\n"); + raise(SIGABRT); + return; + } + + m_settings = json.get(); +} + +// ----------------------------------------- + +void to_json(nlohmann::json& object, const Settings& settings) +{ + object = nlohmann::json { + { "excludePaths", settings.excludePaths }, + { "systemDirectories", settings.systemDirectories } + }; +} + +void from_json(const nlohmann::json& object, Settings& settings) +{ + if (object.find("excludePaths") != object.end()) { + object.at("excludePaths").get_to(settings.excludePaths); + } + + if (object.find("systemDirectories") != object.end()) { + object.at("systemDirectories").get_to(settings.systemDirectories); + } + + // Check for correct exclude type values + for (const auto& path : settings.excludePaths) { + if (path.second != "file" && path.second != "directory" && path.second != "endsWith") { + fprintf(stderr, "\033[31;1mConfig:\033[0m invalid exclude type '%s'\n", path.second.c_str()); + raise(SIGABRT); + } + } +} diff --git a/src/config.h b/src/config.h index 7b98f56..6e8f90d 100644 --- a/src/config.h +++ b/src/config.h @@ -7,11 +7,31 @@ #ifndef CONFIG_H #define CONFIG_H -#include // size_t -#include +#include // size_t +#include // path +#include +#include +#include + +#include "nlohmann/json.hpp" #include "util/singleton.h" +struct Settings { + std::map excludePaths { + { ".git", "directory" }, + { ".md", "endsWith" }, + { "packages", "file" }, + { "README.org", "endsWith" }, + { "screenshot.png", "file" }, + }; + std::vector systemDirectories { + "/boot", + "/etc", + "/usr/share" + }; +}; + class Config : public Util::Singleton { public: Config(s); @@ -19,12 +39,17 @@ public: void setVerbose(bool verbose) { m_verbose = verbose; } + const std::map& excludePaths() const { return m_settings.excludePaths; } + const std::vector& systemDirectories() const { return m_settings.systemDirectories; } + const std::filesystem::path& workingDirectory() const { return m_workingDirectory; } size_t workingDirectorySize() const { return m_workingDirectorySize; } + bool verbose() const { return m_verbose; } private: void findConfigFile(); + void parseConfigFile(); bool m_verbose { false }; @@ -32,6 +57,14 @@ private: size_t m_workingDirectorySize { 0 }; std::filesystem::path m_config; + Settings m_settings; }; +// ----------------------------------------- + +// nlohmann::json arbitrary type conversion functions + +void to_json(nlohmann::json& object, const Settings& settings); +void from_json(const nlohmann::json& object, Settings& settings); + #endif // CONFIG_H