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 add_library(bottles SHARED ${src}/bottles/bottles.cpp
${src}/bottles/active.cpp ${src}/bottles/commands.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_dependencies(bottles cog)
add_library(launch SHARED ${src}/launch/launch.cpp ${src}/launch/commands.cpp add_library(launch SHARED ${src}/launch/launch.cpp ${src}/launch/commands.cpp

View File

@ -27,6 +27,8 @@ namespace cellar {
string path; string path;
string canonical_path; string canonical_path;
Bottle(); Bottle();
bool load_config();
bool save_config();
}; };
extern map<string, Bottle> get_bottles(); 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; output.type = bottle_symlink;
} else { } else {
output.canonical_path = fullitem; output.canonical_path = fullitem;
string jsonpath = fullitem + "/cellar.json"; try {
if (boost::filesystem::exists(jsonpath)) { if (output.load_config()) {
try {
json config;
ifstream configstream(jsonpath);
stringstream sstr_config;
sstr_config << configstream.rdbuf();
config = json::parse(sstr_config.str());
output.config = config;
output.type = bottle_labelled; output.type = bottle_labelled;
} } else {
catch (const exception &exc) { output.type = bottle_anonymous;
output.type = bottle_error; }
} }
} catch (const exception &exc) {
else { output.type = bottle_error;
output.type = bottle_anonymous; }
}
} }
result[item] = output; result[item] = output;
} }

View File

@ -1,8 +1,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "json.hpp"
#include "bottles.hpp" #include "bottles.hpp"
#include "internal/bottles.hpp" #include "internal/bottles.hpp"
#include "output.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;
}