main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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> [--limit N] [--exists FIELD]"); return false; }
  75. smartbotic::database::Client::QueryOptions opts;
  76. opts.limit = 100;
  77. for (size_t i = 1; i < params.size(); ++i) {
  78. if (params[i] == "--limit" && i + 1 < params.size()) {
  79. opts.limit = static_cast<uint32_t>(std::stoul(params[++i]));
  80. } else if (params[i] == "--exists" && i + 1 < params.size()) {
  81. opts.filters.emplace_back(params[++i], smartbotic::database::Client::FilterOp::EXISTS, true);
  82. }
  83. }
  84. auto docs = client.find(params[0], opts);
  85. std::cout << C_DIM << "(" << docs.size() << " documents)" << C_RESET << "\n";
  86. for (const auto& doc : docs) {
  87. auto id = doc.value("_id", "");
  88. // Print compact summary line
  89. std::cout << C_GREEN << id << C_RESET;
  90. // Show a few key fields
  91. for (const auto& [k, v] : doc.items()) {
  92. if (k == "_id" || k == "_created_at" || k == "_updated_at") continue;
  93. auto val = v.dump();
  94. if (val.size() > 60) val = val.substr(0, 57) + "...";
  95. std::cout << " " << C_DIM << k << "=" << C_RESET << val;
  96. // Limit to 3 fields per line
  97. static int field_count = 0;
  98. if (++field_count >= 3) { field_count = 0; break; }
  99. }
  100. std::cout << "\n";
  101. }
  102. return true;
  103. }
  104. if (cmd == "get") {
  105. if (params.size() < 2) { printError("usage: get <collection> <id>"); return false; }
  106. auto doc = client.get(params[0], params[1]);
  107. if (!doc) { printError("not found: " + params[0] + "/" + params[1]); return false; }
  108. printJson(*doc);
  109. return true;
  110. }
  111. if (cmd == "upsert") {
  112. if (params.size() < 3) { printError("usage: upsert <collection> <id> '<json>'"); return false; }
  113. auto data = json::parse(params[2], nullptr, false);
  114. if (!data.is_object()) { printError("invalid JSON: " + params[2]); return false; }
  115. client.upsert(params[0], data, params[1]);
  116. std::cout << C_GREEN << "ok" << C_RESET << " " << params[0] << "/" << params[1] << "\n";
  117. return true;
  118. }
  119. if (cmd == "remove" || cmd == "delete") {
  120. if (params.size() < 2) { printError("usage: remove <collection> <id>"); return false; }
  121. client.remove(params[0], params[1]);
  122. std::cout << C_GREEN << "ok" << C_RESET << " removed " << params[0] << "/" << params[1] << "\n";
  123. return true;
  124. }
  125. if (cmd == "count") {
  126. if (params.empty()) { printError("usage: count <collection>"); return false; }
  127. auto info = client.getCollectionInfo(params[0]);
  128. if (!info) { printError("collection not found: " + params[0]); return false; }
  129. std::cout << info->documentCount << "\n";
  130. return true;
  131. }
  132. if (cmd == "help" || cmd == "?") {
  133. printUsage();
  134. return true;
  135. }
  136. printError("unknown command: " + cmd + " (try 'help')");
  137. return false;
  138. } catch (const std::exception& e) {
  139. printError(e.what());
  140. return false;
  141. }
  142. }
  143. // Tokenize a line, respecting single/double quotes and JSON braces
  144. std::vector<std::string> tokenize(const std::string& line) {
  145. std::vector<std::string> tokens;
  146. std::string current;
  147. int brace_depth = 0;
  148. char in_quote = 0;
  149. for (size_t i = 0; i < line.size(); ++i) {
  150. char c = line[i];
  151. if (in_quote) {
  152. current += c;
  153. if (c == in_quote && (i == 0 || line[i-1] != '\\')) {
  154. in_quote = 0;
  155. // Strip surrounding quotes for simple string tokens
  156. if (brace_depth == 0 && current.size() >= 2
  157. && (current.front() == '\'' || current.front() == '"')
  158. && current.front() == current.back()) {
  159. current = current.substr(1, current.size() - 2);
  160. }
  161. }
  162. continue;
  163. }
  164. if (c == '\'' || c == '"') {
  165. in_quote = c;
  166. current += c;
  167. continue;
  168. }
  169. if (c == '{') { brace_depth++; current += c; continue; }
  170. if (c == '}') {
  171. brace_depth--;
  172. current += c;
  173. if (brace_depth <= 0) {
  174. brace_depth = 0;
  175. tokens.push_back(current);
  176. current.clear();
  177. }
  178. continue;
  179. }
  180. if (brace_depth > 0) { current += c; continue; }
  181. if (c == ' ' || c == '\t') {
  182. if (!current.empty()) {
  183. tokens.push_back(current);
  184. current.clear();
  185. }
  186. continue;
  187. }
  188. current += c;
  189. }
  190. if (!current.empty()) tokens.push_back(current);
  191. return tokens;
  192. }
  193. } // anonymous namespace
  194. int main(int argc, char* argv[]) {
  195. Args args;
  196. // Parse flags
  197. std::vector<std::string> positional;
  198. for (int i = 1; i < argc; ++i) {
  199. std::string arg = argv[i];
  200. if (arg == "--address" && i + 1 < argc) {
  201. args.address = argv[++i];
  202. } else if (arg == "--help" || arg == "-h") {
  203. printUsage();
  204. return 0;
  205. } else {
  206. positional.push_back(arg);
  207. }
  208. }
  209. if (!positional.empty()) {
  210. args.command = positional[0];
  211. args.params.assign(positional.begin() + 1, positional.end());
  212. }
  213. // Connect
  214. smartbotic::database::Client client({.address = args.address});
  215. client.connect();
  216. // Scriptable mode: single command
  217. if (!args.command.empty()) {
  218. return execCommand(client, args.command, args.params) ? 0 : 1;
  219. }
  220. // Interactive mode
  221. std::cout << C_BOLD << "smartbotic-db-cli" << C_RESET
  222. << " connected to " << C_CYAN << args.address << C_RESET << "\n"
  223. << C_DIM << "Type 'help' for commands, 'exit' to quit." << C_RESET << "\n";
  224. std::string line;
  225. while (true) {
  226. std::cout << C_YELLOW << "> " << C_RESET;
  227. if (!std::getline(std::cin, line)) break;
  228. // Trim
  229. auto start = line.find_first_not_of(" \t");
  230. if (start == std::string::npos) continue;
  231. line = line.substr(start);
  232. if (line == "exit" || line == "quit" || line == "q") break;
  233. if (line.empty()) continue;
  234. auto tokens = tokenize(line);
  235. if (tokens.empty()) continue;
  236. auto cmd = tokens[0];
  237. std::vector<std::string> params(tokens.begin() + 1, tokens.end());
  238. execCommand(client, cmd, params);
  239. }
  240. return 0;
  241. }