main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <smartbotic/database/client.hpp>
  2. #include <nlohmann/json.hpp>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. using json = nlohmann::json;
  8. namespace {
  9. struct Args {
  10. std::string address = "localhost:9004";
  11. std::string command;
  12. std::vector<std::string> params;
  13. };
  14. // ANSI colors
  15. constexpr auto C_RESET = "\033[0m";
  16. constexpr auto C_BOLD = "\033[1m";
  17. constexpr auto C_DIM = "\033[2m";
  18. constexpr auto C_CYAN = "\033[36m";
  19. constexpr auto C_GREEN = "\033[32m";
  20. constexpr auto C_RED = "\033[31m";
  21. constexpr auto C_YELLOW = "\033[33m";
  22. void printJson(const json& j) {
  23. std::cout << j.dump(2) << "\n";
  24. }
  25. void printError(const std::string& msg) {
  26. std::cerr << C_RED << "error: " << C_RESET << msg << "\n";
  27. }
  28. void printUsage() {
  29. std::cout << C_BOLD << "smartbotic-db-cli" << C_RESET << " — admin tool for smartbotic-database\n\n"
  30. << C_BOLD << "Usage:" << C_RESET << "\n"
  31. << " smartbotic-db-cli [--address HOST:PORT] <command> [args...]\n"
  32. << " smartbotic-db-cli [--address HOST:PORT] " << C_DIM << "# interactive mode" << C_RESET << "\n\n"
  33. << C_BOLD << "Commands:" << C_RESET << "\n"
  34. << " " << C_CYAN << "collections" << C_RESET << " List all collections\n"
  35. << " " << C_CYAN << "info" << C_RESET << " <collection> Collection info\n"
  36. << " " << C_CYAN << "find" << C_RESET << " <collection> List documents\n"
  37. << " " << C_CYAN << "get" << C_RESET << " <collection> <id> Get a document\n"
  38. << " " << C_CYAN << "upsert" << C_RESET << " <collection> <id> '<json>' Insert or update\n"
  39. << " " << C_CYAN << "remove" << C_RESET << " <collection> <id> Delete a document\n"
  40. << " " << C_CYAN << "count" << C_RESET << " <collection> Count documents\n"
  41. << " " << C_CYAN << "help" << C_RESET << " Show this help\n\n"
  42. << C_BOLD << "Options:" << C_RESET << "\n"
  43. << " --address HOST:PORT Database address (default: localhost:9004)\n";
  44. }
  45. bool execCommand(smartbotic::database::Client& client,
  46. const std::string& cmd, const std::vector<std::string>& params) {
  47. try {
  48. if (cmd == "collections") {
  49. auto collections = client.listCollections();
  50. std::cout << C_BOLD << "Collections:" << C_RESET << "\n";
  51. for (const auto& c : collections) {
  52. auto info = client.getCollectionInfo(c);
  53. int64_t count = 0;
  54. if (info) count = info->documentCount;
  55. std::cout << " " << C_CYAN << c << C_RESET
  56. << C_DIM << " (" << count << " docs)" << C_RESET << "\n";
  57. }
  58. return true;
  59. }
  60. if (cmd == "info") {
  61. if (params.empty()) { printError("usage: info <collection>"); return false; }
  62. auto info = client.getCollectionInfo(params[0]);
  63. if (!info) { printError("collection not found: " + params[0]); return false; }
  64. std::cout << C_BOLD << params[0] << C_RESET << ":\n"
  65. << " documents: " << info->documentCount << "\n"
  66. << " size: " << info->sizeBytes << " bytes\n"
  67. << " encrypted: " << (info->encrypted ? "yes" : "no") << "\n"
  68. << " max_versions: " << info->maxVersions << "\n";
  69. if (info->defaultTtlSeconds > 0)
  70. std::cout << " ttl: " << info->defaultTtlSeconds << "s\n";
  71. return true;
  72. }
  73. if (cmd == "find") {
  74. if (params.empty()) { printError("usage: find <collection>"); return false; }
  75. auto docs = client.find(params[0]);
  76. std::cout << C_DIM << "(" << docs.size() << " documents)" << C_RESET << "\n";
  77. for (const auto& doc : docs) {
  78. auto id = doc.value("_id", "");
  79. // Print compact summary line
  80. std::cout << C_GREEN << id << C_RESET;
  81. // Show a few key fields
  82. for (const auto& [k, v] : doc.items()) {
  83. if (k == "_id" || k == "_created_at" || k == "_updated_at") continue;
  84. auto val = v.dump();
  85. if (val.size() > 60) val = val.substr(0, 57) + "...";
  86. std::cout << " " << C_DIM << k << "=" << C_RESET << val;
  87. // Limit to 3 fields per line
  88. static int field_count = 0;
  89. if (++field_count >= 3) { field_count = 0; break; }
  90. }
  91. std::cout << "\n";
  92. }
  93. return true;
  94. }
  95. if (cmd == "get") {
  96. if (params.size() < 2) { printError("usage: get <collection> <id>"); return false; }
  97. auto doc = client.get(params[0], params[1]);
  98. if (!doc) { printError("not found: " + params[0] + "/" + params[1]); return false; }
  99. printJson(*doc);
  100. return true;
  101. }
  102. if (cmd == "upsert") {
  103. if (params.size() < 3) { printError("usage: upsert <collection> <id> '<json>'"); return false; }
  104. auto data = json::parse(params[2], nullptr, false);
  105. if (!data.is_object()) { printError("invalid JSON: " + params[2]); return false; }
  106. client.upsert(params[0], data, params[1]);
  107. std::cout << C_GREEN << "ok" << C_RESET << " " << params[0] << "/" << params[1] << "\n";
  108. return true;
  109. }
  110. if (cmd == "remove" || cmd == "delete") {
  111. if (params.size() < 2) { printError("usage: remove <collection> <id>"); return false; }
  112. client.remove(params[0], params[1]);
  113. std::cout << C_GREEN << "ok" << C_RESET << " removed " << params[0] << "/" << params[1] << "\n";
  114. return true;
  115. }
  116. if (cmd == "count") {
  117. if (params.empty()) { printError("usage: count <collection>"); return false; }
  118. auto info = client.getCollectionInfo(params[0]);
  119. if (!info) { printError("collection not found: " + params[0]); return false; }
  120. std::cout << info->documentCount << "\n";
  121. return true;
  122. }
  123. if (cmd == "help" || cmd == "?") {
  124. printUsage();
  125. return true;
  126. }
  127. printError("unknown command: " + cmd + " (try 'help')");
  128. return false;
  129. } catch (const std::exception& e) {
  130. printError(e.what());
  131. return false;
  132. }
  133. }
  134. // Tokenize a line, respecting single/double quotes and JSON braces
  135. std::vector<std::string> tokenize(const std::string& line) {
  136. std::vector<std::string> tokens;
  137. std::string current;
  138. int brace_depth = 0;
  139. char in_quote = 0;
  140. for (size_t i = 0; i < line.size(); ++i) {
  141. char c = line[i];
  142. if (in_quote) {
  143. current += c;
  144. if (c == in_quote && (i == 0 || line[i-1] != '\\')) {
  145. in_quote = 0;
  146. // Strip surrounding quotes for simple string tokens
  147. if (brace_depth == 0 && current.size() >= 2
  148. && (current.front() == '\'' || current.front() == '"')
  149. && current.front() == current.back()) {
  150. current = current.substr(1, current.size() - 2);
  151. }
  152. }
  153. continue;
  154. }
  155. if (c == '\'' || c == '"') {
  156. in_quote = c;
  157. current += c;
  158. continue;
  159. }
  160. if (c == '{') { brace_depth++; current += c; continue; }
  161. if (c == '}') {
  162. brace_depth--;
  163. current += c;
  164. if (brace_depth <= 0) {
  165. brace_depth = 0;
  166. tokens.push_back(current);
  167. current.clear();
  168. }
  169. continue;
  170. }
  171. if (brace_depth > 0) { current += c; continue; }
  172. if (c == ' ' || c == '\t') {
  173. if (!current.empty()) {
  174. tokens.push_back(current);
  175. current.clear();
  176. }
  177. continue;
  178. }
  179. current += c;
  180. }
  181. if (!current.empty()) tokens.push_back(current);
  182. return tokens;
  183. }
  184. } // anonymous namespace
  185. int main(int argc, char* argv[]) {
  186. Args args;
  187. // Parse flags
  188. std::vector<std::string> positional;
  189. for (int i = 1; i < argc; ++i) {
  190. std::string arg = argv[i];
  191. if (arg == "--address" && i + 1 < argc) {
  192. args.address = argv[++i];
  193. } else if (arg == "--help" || arg == "-h") {
  194. printUsage();
  195. return 0;
  196. } else {
  197. positional.push_back(arg);
  198. }
  199. }
  200. if (!positional.empty()) {
  201. args.command = positional[0];
  202. args.params.assign(positional.begin() + 1, positional.end());
  203. }
  204. // Connect
  205. smartbotic::database::Client client({.address = args.address});
  206. client.connect();
  207. // Scriptable mode: single command
  208. if (!args.command.empty()) {
  209. return execCommand(client, args.command, args.params) ? 0 : 1;
  210. }
  211. // Interactive mode
  212. std::cout << C_BOLD << "smartbotic-db-cli" << C_RESET
  213. << " connected to " << C_CYAN << args.address << C_RESET << "\n"
  214. << C_DIM << "Type 'help' for commands, 'exit' to quit." << C_RESET << "\n";
  215. std::string line;
  216. while (true) {
  217. std::cout << C_YELLOW << "> " << C_RESET;
  218. if (!std::getline(std::cin, line)) break;
  219. // Trim
  220. auto start = line.find_first_not_of(" \t");
  221. if (start == std::string::npos) continue;
  222. line = line.substr(start);
  223. if (line == "exit" || line == "quit" || line == "q") break;
  224. if (line.empty()) continue;
  225. auto tokens = tokenize(line);
  226. if (tokens.empty()) continue;
  227. auto cmd = tokens[0];
  228. std::vector<std::string> params(tokens.begin() + 1, tokens.end());
  229. execCommand(client, cmd, params);
  230. }
  231. return 0;
  232. }