global config support (fix #24)

This commit is contained in:
Nicholas O'Connor 2017-06-09 13:12:48 -07:00
parent d873416602
commit 237ca9d4cd
3 changed files with 43 additions and 4 deletions

View File

@ -4,15 +4,20 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "json.hpp"
#include "bottles.hpp" #include "bottles.hpp"
using namespace std; using namespace std;
using nlohmann::json;
namespace cellar { namespace cellar {
extern void print_header(); extern void print_header();
extern bool dryrun; extern bool dryrun;
extern bool verbose; extern bool verbose;
extern json global_config;
} }
#endif // __CELLAR_HPP #endif // __CELLAR_HPP

View File

@ -46,8 +46,10 @@ void cellar::config::config_command(int argc, vector<string> argv) {
return; return;
} }
if (value != "") { string myval = active_bottle.get_config(key);
output::statement(key + ": " + value);
if (myval != "") {
output::statement(key + ": " + myval);
} else { } else {
output::error(key + " is not defined"); output::error(key + " is not defined");
} }

View File

@ -6,16 +6,23 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include "json.hpp" #include "json.hpp"
#include "cellar.hpp"
#include "bottles.hpp" #include "bottles.hpp"
#include "output.hpp"
using namespace std; using namespace std;
using namespace cellar;
using namespace cellar::bottles; using namespace cellar::bottles;
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
using json = nlohmann::json; using json = nlohmann::json;
json cellar::global_config;
bool Bottle::load_config() { bool Bottle::load_config() {
bool globalconfig = false;
bool localconfig = false;
string jsonpath = this->canonical_path + "/cellar.json"; string jsonpath = this->canonical_path + "/cellar.json";
if (fs::exists(jsonpath)) { if (fs::exists(jsonpath)) {
json config; json config;
@ -25,11 +32,36 @@ bool Bottle::load_config() {
config = json::parse(sstr_config.str()); config = json::parse(sstr_config.str());
this->config = config; this->config = config;
return true; localconfig = true;
} }
else { else {
return false; output::warning("local config for this bottle doesn't exist", true);
} }
// Get path to global cellar.json
// (which should be ~/.local/share/cellar/cellar.json unless the user set XDG_DATA_HOME)
stringstream sstr_globalpath;
char* xdgdir = getenv("XDG_DATA_HOME");
if (xdgdir == nullptr || xdgdir == "") {
sstr_globalpath << getenv("HOME") << "/.local/share/cellar";
} else {
sstr_globalpath << xdgdir << "/cellar";
}
sstr_globalpath << "/cellar.json";
string globaljsonpath = sstr_globalpath.str();
if (fs::exists(globaljsonpath)) {
ifstream configstream(globaljsonpath);
stringstream sstr_config;
sstr_config << configstream.rdbuf();
cellar::global_config = json::parse(sstr_config.str());
globalconfig = true;
} else {
output::warning("global cellar config doesn't exist", true);
}
return localconfig || globalconfig; // should return true if either one of these do
} }
bool Bottle::save_config() { bool Bottle::save_config() {