2017-03-24 15:57:49 -07:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-14 22:40:58 -07:00
|
|
|
#include "tclap/CmdLine.h"
|
|
|
|
|
2017-03-24 15:57:49 -07:00
|
|
|
#include "bottles.hpp"
|
2017-03-24 21:50:32 -07:00
|
|
|
#include "cellar.hpp"
|
2025-01-20 14:49:17 -08:00
|
|
|
#include "config.hpp"
|
2017-04-13 15:02:50 -07:00
|
|
|
#include "internal/config.hpp"
|
2017-03-24 15:57:49 -07:00
|
|
|
#include "output.hpp"
|
2017-04-14 22:40:58 -07:00
|
|
|
#include "version.hpp"
|
2017-03-24 15:57:49 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cellar;
|
|
|
|
using namespace cellar::bottles;
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2017-04-13 15:02:50 -07:00
|
|
|
void cellar::config::config_command(int argc, vector<string> argv) {
|
2017-04-14 22:40:58 -07:00
|
|
|
const string desc = "Change cellar configuration options";
|
|
|
|
const string versionstr = version::short_version();
|
|
|
|
TCLAP::CmdLine cmdparse(desc, ' ', versionstr, false);
|
|
|
|
|
|
|
|
TCLAP::SwitchArg globalarg("g", "global", "Use global user configuration (~/.local/share/cellar/cellar.json)");
|
|
|
|
cmdparse.add(globalarg);
|
|
|
|
|
|
|
|
TCLAP::UnlabeledValueArg<string> commandarg("command", "", true, "get", "command");
|
|
|
|
cmdparse.add(commandarg);
|
|
|
|
|
|
|
|
TCLAP::UnlabeledValueArg<string> keyarg("key", "", true, "", "key");
|
|
|
|
cmdparse.add(keyarg);
|
|
|
|
|
|
|
|
TCLAP::UnlabeledValueArg<string> valarg("value", "", false, "", "value");
|
|
|
|
cmdparse.add(valarg);
|
|
|
|
|
|
|
|
cmdparse.parse(argv);
|
|
|
|
|
|
|
|
bool global = globalarg.getValue();
|
|
|
|
string command = commandarg.getValue();
|
|
|
|
string key = keyarg.getValue();
|
|
|
|
string value = valarg.getValue();
|
2017-03-24 15:57:49 -07:00
|
|
|
|
2017-03-24 18:45:50 -07:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2025-01-18 23:18:12 -08:00
|
|
|
string myval;
|
|
|
|
if (global) {
|
|
|
|
if (config::global_config.find(key) != config::global_config.end()) {
|
|
|
|
myval = config::global_config[key];
|
|
|
|
} else if (config::compiled_config.find(key) != config::compiled_config.end()) {
|
|
|
|
myval = config::compiled_config[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { myval = active_bottle.get_config(key); }
|
2017-06-09 13:12:48 -07:00
|
|
|
|
|
|
|
if (myval != "") {
|
|
|
|
output::statement(key + ": " + myval);
|
2017-03-24 18:45:50 -07:00
|
|
|
} else {
|
2017-03-24 20:12:57 -07:00
|
|
|
output::error(key + " is not defined");
|
2017-03-24 18:45:50 -07:00
|
|
|
}
|
|
|
|
} else if (command == "set") {
|
2017-03-24 20:12:57 -07:00
|
|
|
if (argc < 4) {
|
|
|
|
output::statement("usage: " + argv[0] + " set <key> <value>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 12:16:59 -07:00
|
|
|
string newvalue = value;
|
2025-01-20 14:49:17 -08:00
|
|
|
string oldvalue;
|
2025-01-18 23:18:12 -08:00
|
|
|
if (global) {
|
|
|
|
if (config::global_config.find(key) != config::global_config.end()) {
|
|
|
|
oldvalue = config::global_config[key];
|
|
|
|
} else if (config::compiled_config.find(key) != config::compiled_config.end()) {
|
|
|
|
oldvalue = config::compiled_config[key];
|
|
|
|
}
|
|
|
|
} else { oldvalue = active_bottle.get_config(key); }
|
2017-03-24 20:12:57 -07:00
|
|
|
|
|
|
|
if (active_bottle.set_config(key, newvalue)) {
|
|
|
|
output::statement(key + ": " + newvalue + " (was " + oldvalue + ")");
|
|
|
|
} else {
|
|
|
|
output::error(key + ": " + oldvalue);
|
|
|
|
}
|
2017-03-24 18:45:50 -07:00
|
|
|
} else {
|
|
|
|
output::statement("usage is one of:");
|
|
|
|
output::statement("\t" + argv[0] + " get <key>");
|
|
|
|
output::statement("\t" + argv[0] + " set <key> <value>");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-24 15:57:49 -07:00
|
|
|
}
|