cellar/src/config/cli_handler.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

2017-03-24 15:57:49 -07:00
#include <string>
#include <vector>
#include "tclap/CmdLine.h"
2017-03-24 15:57:49 -07:00
#include "bottles.hpp"
#include "cellar.hpp"
#include "internal/config.hpp"
2017-03-24 15:57:49 -07:00
#include "output.hpp"
#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;
void cellar::config::config_command(int argc, vector<string> argv) {
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;
}
2017-06-09 13:12:48 -07:00
string myval = active_bottle.get_config(key);
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;
}
string newvalue = value;
2017-03-24 20:12:57 -07:00
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);
}
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
}