cellar/src/config/cli_handler.cpp

59 lines
1.6 KiB
C++
Raw Normal View History

2017-03-24 15:57:49 -07:00
#include <string>
#include <vector>
#include "bottles.hpp"
#include "cellar.hpp"
#include "internal/config.hpp"
2017-03-24 15:57:49 -07:00
#include "output.hpp"
using namespace std;
using namespace cellar;
using namespace cellar::bottles;
using json = nlohmann::json;
void cellar::config::config_command(int argc, vector<string> argv) {
2017-03-24 15:57:49 -07:00
if (argc == 1) {
output::error("not enough arguments");
return;
}
string command = argv[1];
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-03-24 20:12:57 -07:00
string key = argv[2];
string value = active_bottle.get_config(key);
2017-03-24 18:45:50 -07:00
if (value != "") {
2017-03-24 20:12:57 -07:00
output::statement(key + ": " + value);
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 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);
}
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
}