navid/veins / c161f7014d5305a11e443bbb16eb9840cc71c7564c86e03a02b601403eb46558 / xveins/src/cfg.cpp
cfg.cpp
← xveins/src · 9000 bytes · raw · download
#include "local_commands.h"
#include "store.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <vector>
namespace vn {
namespace {
std::string_view cfg_trim(std::string_view text) {
while (!text.empty() && (text.front() == ' ' || text.front() == '\t' || text.front() == '\r' ||
text.front() == '\n'))
text.remove_prefix(1);
while (!text.empty() && (text.back() == ' ' || text.back() == '\t' || text.back() == '\r' ||
text.back() == '\n'))
text.remove_suffix(1);
return text;
}
bool cfg_is_flag(std::string_view arg) {
return !arg.empty() && arg.front() == '-';
}
std::map<std::string, std::string> repo_config_entries(const fs::path& root) {
std::map<std::string, std::string> out;
const auto data = read_file_opt(root / ".veins" / "config");
if (!data)
return out;
std::string_view text(reinterpret_cast<const char*>(data->data()), data->size());
std::size_t start = 0;
while (start <= text.size()) {
std::size_t end = text.find('\n', start);
if (end == std::string_view::npos)
end = text.size();
const std::string_view line = cfg_trim(text.substr(start, end - start));
if (!line.empty() && line.front() != '#') {
const std::size_t eq = line.find('=');
if (eq != std::string_view::npos) {
const std::string_view key = cfg_trim(line.substr(0, eq));
if (!key.empty())
out[std::string(key)] = std::string(cfg_trim(line.substr(eq + 1)));
}
}
if (end == text.size())
break;
start = end + 1;
}
return out;
}
void cfg_usage() {
std::cout << "usage: vn cfg [<key>] [<value>]\n";
std::cout << " vn cfg [<get|set|unset|list>] [<key>] [<value>]\n";
std::cout << " vn cfg -list\n";
std::cout << " vn cfg -d [-i]\n";
std::cout << " vn cfg -unset <key>\n";
std::cout << "\n";
std::cout << "read and write local repository configuration\n";
std::cout << "\n";
std::cout << " vn cfg list all keys\n";
std::cout << " vn cfg <key> print a value\n";
std::cout << " vn cfg <key> <value> set a value\n";
std::cout << " vn cfg -d show defaults and global values\n";
std::cout << " vn cfg -i -d also show internal keys\n";
std::cout << " vn cfg -unset <key> remove a key\n";
std::cout << " vn cfg -global ... use the user config instead\n";
std::cout << "\nthe global editor command is read from 'editor' or 'core.editor'\n";
std::cout << " vn cfg -global editor \"code --wait\"\n";
}
int cfg_defaults(bool internal) {
#if defined(_WIN32)
const std::string editor_default = "notepad";
const std::string pager_default = "more";
#else
const std::string editor_default = "vi";
const std::string pager_default = "less -R";
#endif
std::map<std::string, std::string> defaults = {
{"core.editor", "(unset)"},
{"core.pager", "(unset)"},
{"editor", editor_default},
{"log.default_amount", "20"},
{"merge.adjacent", "strict"},
{"pager", pager_default},
{"rename.autodetect", "similar"},
{"user.email", "(unset)"},
{"user.name", "(unset)"},
};
if (internal) {
defaults["bookmark.current"] = "(unset)";
defaults["promisor"] = "(unset)";
defaults["remote.<name>.key"] = "(unset)";
defaults["remote.<name>.tls-pin"] = "(unset)";
defaults["remote.<name>.url"] = "(unset)";
defaults["remote.current"] = "(unset)";
defaults["remote.default"] = "(unset)";
}
for (const auto& entry : defaults) {
std::cout << entry.first << " = " << entry.second;
if (auto current = user_config_value(entry.first))
std::cout << " (current: " << *current << ")";
std::cout << "\n";
}
return 0;
}
int cfg_list(const fs::path* root, bool global) {
std::map<std::string, std::string> entries;
if (global) {
entries = user_config_entries();
} else {
entries = repo_config_entries(*root);
}
for (const auto& entry : entries)
std::cout << entry.first << " = " << entry.second << "\n";
return 0;
}
int cfg_get(const fs::path* root, bool global, const std::string& key) {
std::optional<std::string> value;
if (global) {
value = user_config_value(key);
} else {
value = repo_config_value(*root, key);
}
if (!value) {
error_prefix();
std::cout << "unknown config key: " << key << "\n";
return 1;
}
std::cout << *value << "\n";
return 0;
}
int cfg_set(const fs::path* root, bool global, const std::string& key, const std::string& value) {
const bool ok = global ? user_config_set(key, value) : repo_config_set(*root, key, value);
if (!ok) {
error_prefix();
std::cout << "cannot write " << (global ? "user" : "repository") << " config\n";
return 1;
}
return 0;
}
int cfg_unset(const fs::path* root, bool global, const std::string& key) {
const bool ok = global ? user_config_remove(key) : repo_config_remove(*root, key);
if (!ok) {
error_prefix();
std::cout << "cannot write " << (global ? "user" : "repository") << " config\n";
return 1;
}
return 0;
}
} // namespace
int cfg_run(const std::vector<std::string>& args) {
if (args.size() == 3 && (args[2] == "help" || args[2] == "-h" || args[2] == "--help")) {
cfg_usage();
return 0;
}
bool global = false;
bool list = false;
bool unset = false;
bool show_defaults = false;
bool internal = false;
bool end_flags = false;
std::vector<std::string> positional;
for (std::size_t i = 2; i < args.size(); ++i) {
const std::string& arg = args[i];
if (!end_flags && arg == "--") {
end_flags = true;
} else if (!end_flags && (arg == "-global" || arg == "--global" || arg == "-g")) {
global = true;
} else if (!end_flags && (arg == "-list" || arg == "--list" || arg == "-l")) {
list = true;
} else if (!end_flags && (arg == "-unset" || arg == "--unset")) {
unset = true;
} else if (!end_flags && (arg == "-d" || arg == "--defaults")) {
show_defaults = true;
} else if (!end_flags && (arg == "-i" || arg == "--internal")) {
internal = true;
} else if (!end_flags && cfg_is_flag(arg)) {
error_prefix();
std::cout << "unknown flag: " << arg << "\n";
return 1;
} else {
positional.push_back(arg);
}
}
if (show_defaults) {
if (list || unset || !positional.empty()) {
error_prefix();
std::cout << "cfg -d takes no operands\n";
return 1;
}
return cfg_defaults(internal);
}
if (internal) {
error_prefix();
std::cout << "cfg -i requires -d\n";
return 1;
}
if (positional.size() == 1 && (positional[0] == "list" || positional[0] == "ls")) {
list = true;
positional.clear();
} else if (positional.size() == 2 && positional[0] == "get") {
positional.erase(positional.begin());
} else if (positional.size() == 3 && positional[0] == "set") {
positional.erase(positional.begin());
} else if (positional.size() == 2 &&
(positional[0] == "unset" || positional[0] == "rm" || positional[0] == "remove")) {
unset = true;
positional.erase(positional.begin());
}
if (list) {
if (!positional.empty()) {
error_prefix();
std::cout << "cfg list takes no operands\n";
return 1;
}
} else if (unset) {
if (positional.size() != 1) {
error_prefix();
std::cout << "cfg unset requires one key\n";
return 1;
}
} else if (positional.size() > 2) {
error_prefix();
std::cout << "cfg accepts a key and one value\n";
return 1;
}
std::optional<fs::path> root;
if (!global) {
root = discover_repo(".");
if (!root) {
error_prefix();
std::cout << "not a veins repository\n";
return 1;
}
}
if (list)
return cfg_list(root ? &*root : nullptr, global);
if (unset)
return cfg_unset(root ? &*root : nullptr, global, positional[0]);
if (positional.empty())
return cfg_list(root ? &*root : nullptr, global);
if (positional.size() == 1)
return cfg_get(root ? &*root : nullptr, global, positional[0]);
return cfg_set(root ? &*root : nullptr, global, positional[0], positional[1]);
}
} // namespace vn