config loading is now done via bottle class method, save function also exists
This commit is contained in:
parent
04f2c46def
commit
55787584e2
@ -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
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
@ -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";
|
|
||||||
if (boost::filesystem::exists(jsonpath)) {
|
|
||||||
try {
|
try {
|
||||||
json config;
|
if (output.load_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 {
|
||||||
|
output.type = bottle_anonymous;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (const exception &exc) {
|
catch (const exception &exc) {
|
||||||
output.type = bottle_error;
|
output.type = bottle_error;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
|
||||||
output.type = bottle_anonymous;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result[item] = output;
|
result[item] = output;
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
49
src/bottles/config/save_load.cpp
Normal file
49
src/bottles/config/save_load.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user