From 237ca9d4cded86d7f276e37b5e8079ba95682dc7 Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Fri, 9 Jun 2017 13:12:48 -0700 Subject: [PATCH] global config support (fix #24) --- include/cellar.hpp | 5 +++++ src/config/cli_handler.cpp | 6 ++++-- src/config/save_load.cpp | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/cellar.hpp b/include/cellar.hpp index 1b4c84e..03e3da0 100644 --- a/include/cellar.hpp +++ b/include/cellar.hpp @@ -4,15 +4,20 @@ #include #include +#include "json.hpp" + #include "bottles.hpp" using namespace std; +using nlohmann::json; namespace cellar { extern void print_header(); extern bool dryrun; extern bool verbose; + + extern json global_config; } #endif // __CELLAR_HPP diff --git a/src/config/cli_handler.cpp b/src/config/cli_handler.cpp index 8fc30b9..44003e0 100644 --- a/src/config/cli_handler.cpp +++ b/src/config/cli_handler.cpp @@ -46,8 +46,10 @@ void cellar::config::config_command(int argc, vector argv) { return; } - if (value != "") { - output::statement(key + ": " + value); + string myval = active_bottle.get_config(key); + + if (myval != "") { + output::statement(key + ": " + myval); } else { output::error(key + " is not defined"); } diff --git a/src/config/save_load.cpp b/src/config/save_load.cpp index e0e1697..82db909 100644 --- a/src/config/save_load.cpp +++ b/src/config/save_load.cpp @@ -6,16 +6,23 @@ #include #include "json.hpp" +#include "cellar.hpp" #include "bottles.hpp" +#include "output.hpp" using namespace std; +using namespace cellar; using namespace cellar::bottles; namespace fs = boost::filesystem; using json = nlohmann::json; +json cellar::global_config; + bool Bottle::load_config() { + bool globalconfig = false; + bool localconfig = false; string jsonpath = this->canonical_path + "/cellar.json"; if (fs::exists(jsonpath)) { json config; @@ -25,11 +32,36 @@ bool Bottle::load_config() { config = json::parse(sstr_config.str()); this->config = config; - return true; + localconfig = true; } 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() {