move config to its own subdir, preparation for #24

This commit is contained in:
Nicholas O'Connor
2017-04-13 15:02:50 -07:00
parent 404076539b
commit fc814d1cb3
9 changed files with 42 additions and 8 deletions

View File

@@ -1,7 +1,6 @@
list print_bottles List all available WINE bottles.
active print_active_bottle Get the currently active WINE bottle.
activate switch_active_bottle Switch the active WINE bottle.
config config_command Change configuration options.
create create_bottle Create a new WINE bottle.
remove remove_bottle Remove a WINE bottle.
cork cork_command Cork a bottle, to be "uncorked" later.

View File

@@ -1,58 +0,0 @@
#include <string>
#include <vector>
#include "bottles.hpp"
#include "cellar.hpp"
#include "internal/bottles.hpp"
#include "output.hpp"
using namespace std;
using namespace cellar;
using namespace cellar::bottles;
using json = nlohmann::json;
void cellar::bottles::config_command(int argc, vector<string> argv) {
if (argc == 1) {
output::error("not enough arguments");
return;
}
string command = argv[1];
// BULLSHIT: switch can't be used for strings, according to gcc
if (command == "get") {
if (argc < 3) {
output::error("usage: " + argv[0] + " get <key>");
return;
}
string key = argv[2];
string value = active_bottle.get_config(key);
if (value != "") {
output::statement(key + ": " + value);
} else {
output::error(key + " is not defined");
}
} else if (command == "set") {
if (argc < 4) {
output::statement("usage: " + argv[0] + " set <key> <value>");
return;
}
string key = argv[2];
string newvalue = argv[3];
string oldvalue = active_bottle.get_config(key);
if (active_bottle.set_config(key, newvalue)) {
output::statement(key + ": " + newvalue + " (was " + oldvalue + ")");
} else {
output::error(key + ": " + oldvalue);
}
} else {
output::statement("usage is one of:");
output::statement("\t" + argv[0] + " get <key>");
output::statement("\t" + argv[0] + " set <key> <value>");
return;
}
}

View File

@@ -1,69 +0,0 @@
#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);
configstream.close();
if (fs::exists(jsonpath + ".old")) {
// at this point it should be safe to remove the old config
fs::remove(jsonpath + ".old");
}
return true;
}
string Bottle::get_config(string key) {
if (this->config.find(key) != this->config.end()) {
return this->config[key];
} else {
return "";
}
}
bool Bottle::set_config(string key, string value) {
this->config[key] = value;
this->save_config();
return true;
}

View File

@@ -1,5 +0,0 @@
cellar config get <key>
cellar config set <key> <value>
The first command prints the contents of <key> from cellar.json in the active
WINE bottle. The second command sets <key> in the same file to <value>.