Skip to content

Commit

Permalink
config manager loads and saves
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Dec 24, 2023
1 parent 4cd5ed7 commit f7b05e2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 10 deletions.
6 changes: 4 additions & 2 deletions include/Frate/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ namespace Frate::Constants {

const std::string config_name = "config.json";
#ifdef _WIN64
const std::filesytem::path CONFIG_PATH = std::filesystem::path(getenv("APPDATA")) / NAME / config_name;
const std::filesystem::path CONFIG_DIR = std::filesystem::path(getenv("APPDATA")) / NAME;
const std::filesystem::path CONFIG_PATH = CONFIG_DIR / config_name;
#else
const std::filesystem::path CONFIG_PATH = std::filesystem::path(getenv("HOME")) / ".config" / NAME / config_name;
const std::filesystem::path CONFIG_DIR = std::filesystem::path(getenv("HOME")) / ".config" / NAME;
const std::filesystem::path CONFIG_PATH = CONFIG_DIR / config_name;
#endif

#ifdef BRANCH_DEV
Expand Down
5 changes: 3 additions & 2 deletions include/Frate/Utils/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ namespace Frate::Config {
class ConfigManager {
public:
bool first_time_setup{false};
std::string current_remote_server{""};
std::vector<Command::RemoteServer> servers;
std::string build_server{""};
std::vector<Command::RemoteServer> build_servers;
System::Capabilities capabilities;
bool load();
bool save();
bool createNew();
friend void from_json(const nlohmann::json &j, ConfigManager& config);
friend void to_json(nlohmann::json &j, const ConfigManager& config);
};
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Helpers/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace Frate::Command {
pro->path = std::filesystem::current_path();
#endif
// config.capabilities.search();
config.load();

}
bool execute(std::shared_ptr<Interface> inter){
Expand Down Expand Up @@ -231,7 +232,7 @@ namespace Frate::Command {
if(handler.requires_project){
inter->pro->save();
}

inter->config.save();
return true;
}
}
Expand Down Expand Up @@ -277,6 +278,7 @@ namespace Frate::Command {
if(handler.requires_project){
inter->pro->save();
}
inter->config.save();
return true;
}
}
Expand Down
94 changes: 89 additions & 5 deletions src/Utils/ConfigManager/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,113 @@
#include <Frate/Utils/Macros.hpp>
#include <Frate/Utils/Logging.hpp>
#include <Frate/Constants.hpp>
#include <cwchar>
#include <fstream>
namespace Frate::Config {

void from_json(const nlohmann::json& j, ConfigManager& config) {
FROM_JSON_FIELD(config, capabilities);
FROM_JSON_FIELD(config, first_time_setup);
FROM_JSON_FIELD(config, current_remote_server);
FROM_JSON_FIELD(config, servers);
FROM_JSON_FIELD(config, build_server);
FROM_JSON_FIELD(config, build_servers);
}
void to_json(nlohmann::json &j, const ConfigManager& config){
TO_JSON_FIELD(config, capabilities);
TO_JSON_FIELD(config, first_time_setup);
TO_JSON_FIELD(config, current_remote_server);
TO_JSON_FIELD(config, servers);
TO_JSON_FIELD(config, build_server);
TO_JSON_FIELD(config, build_servers);
}
bool ConfigManager::createNew(){
//Creating config directory
try{

std::filesystem::create_directories(Constants::CONFIG_DIR);

} catch(std::exception& e){

Utils::error << "Failed to create new config directory: " << e.what() << std::endl;
return false;
}

// Create config file
std::ofstream config_file;
try{

config_file.open(Constants::CONFIG_PATH);

} catch(std::exception& e){

Utils::error << "Failed to create new config file: " << e.what() << std::endl;
return false;
}
try{

nlohmann::json config_json = *this;
config_file << config_json.dump(2);

} catch(std::exception& e){
Utils::error << "Failed to write to new config file: " << e.what() << std::endl;
return false;
}

return true;
}
bool ConfigManager::load(){

// Creates config file if it does not exist
if(!std::filesystem::exists(Constants::CONFIG_PATH)){
Utils::info << "Config file does not exist, creating..." << std::endl;
if(!createNew()){
Utils::error << "Failed to create new config file" << std::endl;
return false;
}
}

// Now we attempt to open the new config file which should exist

std::ifstream config_file;

try{
config_file.open(Constants::CONFIG_PATH);

config_file.open(Constants::CONFIG_PATH);

} catch(std::exception& e){

Utils::error << "Failed to open config file: " << e.what() << std::endl;
return false;
}

try{

nlohmann::json config_json = nlohmann::json::parse(config_file);
from_json(config_json, *this);

} catch(std::exception& e){

Utils::error << "Failed to parse config file: " << e.what() << std::endl;
return false;
}
return true;
} // namespace Frate::Config
bool ConfigManager::save(){
if(!std::filesystem::exists(Constants::CONFIG_PATH)){
Utils::info << "Config file does not exist, creating..." << std::endl;
if(!createNew()){
Utils::error << "Failed to create new config file" << std::endl;
return false;
}
}


std::ofstream config_file;
try{
config_file.open(Constants::CONFIG_PATH);
nlohmann::json config_json = *this;
config_file << config_json.dump(2);
} catch(std::exception& e){
Utils::error << "Failed to open config file: " << e.what() << std::endl;
return false;
}
return true;
}
}

0 comments on commit f7b05e2

Please sign in to comment.