navid/veins / 343097cb4e32be11164082207ca436c707a89ddea70be26cdcdd0158c5f98bf4 / xveins/src/cfg.cpp

cfg.cpp

← xveins/src · 6626 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 -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 -unset <key>         remove a key\n";
  std::cout << "  vn cfg -global ...          use the user config instead\n";
}

int cfg_list(const fs::path *root, bool global) {
  std::map<std::string, std::string> entries;
  if (global) {
    if (auto name = user_config_author_name())
      entries["user.name"] = *name;
    if (auto email = user_config_author_email())
      entries["user.email"] = *email;
  } 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) {
    if (key == "user.name")
      value = user_config_author_name();
    else if (key == "user.email")
      value = user_config_author_email();
  } 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 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 && cfg_is_flag(arg)) {
      error_prefix();
      std::cout << "unknown flag: " << arg << "\n";
      return 1;
    } else {
      positional.push_back(arg);
    }
  }

  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