config loading is now done via bottle class method, save function also exists

This commit is contained in:
Nicholas O'Connor 2017-03-24 17:17:43 -07:00
parent 04f2c46def
commit 55787584e2
5 changed files with 62 additions and 21 deletions

View File

@ -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

View File

@ -27,6 +27,8 @@ namespace cellar {
string path;
string canonical_path;
Bottle();
bool load_config();
bool save_config();
};
extern map<string, Bottle> get_bottles();
}

View File

@ -50,25 +50,16 @@ DLL_PUBLIC map<string, Bottle> 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;
}

View File

@ -1,8 +1,6 @@
#include <string>
#include <vector>
#include "json.hpp"
#include "bottles.hpp"
#include "internal/bottles.hpp"
#include "output.hpp"

View File

@ -0,0 +1,49 @@
#include <fstream>
#include <sstream>
#include <string>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#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;
}