From 55787584e2ea2479db1a7b4ff52f92ad8430d809 Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Fri, 24 Mar 2017 17:17:43 -0700 Subject: [PATCH] config loading is now done via bottle class method, save function also exists --- CMakeLists.txt | 3 +- include/bottles.hpp | 2 ++ src/bottles/bottles.cpp | 27 ++++++---------- src/bottles/config/cli_handler.cpp | 2 -- src/bottles/config/save_load.cpp | 49 ++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 src/bottles/config/save_load.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bfdd79..099c28c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,8 @@ endforeach(cogfile) add_library(bottles SHARED ${src}/bottles/bottles.cpp ${src}/bottles/active.cpp ${src}/bottles/commands.cpp - ${src}/bottles/activate.cpp ${src}/bottles/config/cli_handler.cpp) + ${src}/bottles/activate.cpp ${src}/bottles/config/cli_handler.cpp + ${src}/bottles/config/save_load.cpp) add_dependencies(bottles cog) add_library(launch SHARED ${src}/launch/launch.cpp ${src}/launch/commands.cpp diff --git a/include/bottles.hpp b/include/bottles.hpp index 8784e9b..bbfa88b 100644 --- a/include/bottles.hpp +++ b/include/bottles.hpp @@ -27,6 +27,8 @@ namespace cellar { string path; string canonical_path; Bottle(); + bool load_config(); + bool save_config(); }; extern map get_bottles(); } diff --git a/src/bottles/bottles.cpp b/src/bottles/bottles.cpp index 6a2d1bd..4d15838 100644 --- a/src/bottles/bottles.cpp +++ b/src/bottles/bottles.cpp @@ -50,25 +50,16 @@ DLL_PUBLIC map cellar::bottles::get_bottles() { output.type = bottle_symlink; } else { output.canonical_path = fullitem; - string jsonpath = fullitem + "/cellar.json"; - if (boost::filesystem::exists(jsonpath)) { - try { - json config; - ifstream configstream(jsonpath); - stringstream sstr_config; - sstr_config << configstream.rdbuf(); - config = json::parse(sstr_config.str()); - - output.config = config; + try { + if (output.load_config()) { output.type = bottle_labelled; - } - catch (const exception &exc) { - output.type = bottle_error; - } - } - else { - output.type = bottle_anonymous; - } + } else { + output.type = bottle_anonymous; + } + } + catch (const exception &exc) { + output.type = bottle_error; + } } result[item] = output; } diff --git a/src/bottles/config/cli_handler.cpp b/src/bottles/config/cli_handler.cpp index a11ffee..ded49cd 100644 --- a/src/bottles/config/cli_handler.cpp +++ b/src/bottles/config/cli_handler.cpp @@ -1,8 +1,6 @@ #include #include -#include "json.hpp" - #include "bottles.hpp" #include "internal/bottles.hpp" #include "output.hpp" diff --git a/src/bottles/config/save_load.cpp b/src/bottles/config/save_load.cpp new file mode 100644 index 0000000..43366c4 --- /dev/null +++ b/src/bottles/config/save_load.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +#include +#include +#include "json.hpp" + +#include "bottles.hpp" + +using namespace std; +using namespace cellar::bottles; + +namespace fs = boost::filesystem; + +using json = nlohmann::json; + +bool Bottle::load_config() { + string jsonpath = this->canonical_path + "/cellar.json"; + if (fs::exists(jsonpath)) { + json config; + ifstream configstream(jsonpath); + stringstream sstr_config; + sstr_config << configstream.rdbuf(); + config = json::parse(sstr_config.str()); + + this->config = config; + return true; + } + else { + return false; + } +} + +bool Bottle::save_config() { + string jsonpath = this->canonical_path + "/cellar.json"; + + // backup the old config in case something bad happens + if (fs::exists(jsonpath)) { + fs::copy_file(jsonpath, jsonpath + ".old"); + + // now make room for the new one + fs::remove(jsonpath); + } + + ofstream configstream(jsonpath); + configstream << this->config.dump(4); + return true; +}