config loading is now done via bottle class method, save function also exists
This commit is contained in:
		@@ -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;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,6 @@
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#include "json.hpp"
 | 
			
		||||
 | 
			
		||||
#include "bottles.hpp"
 | 
			
		||||
#include "internal/bottles.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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user