diff --git a/frontends/cli/src/commands/command_dispatch.cpp b/frontends/cli/src/commands/command_dispatch.cpp new file mode 100644 index 000000000..73903a2e6 --- /dev/null +++ b/frontends/cli/src/commands/command_dispatch.cpp @@ -0,0 +1,134 @@ +#include "command_dispatch.h" + +#include +#include + +namespace { + +void print_help() { + std::cout << R"(Usage: metabuilder-cli [options] +Available commands: + auth session Show the current authentication session + auth login Authenticate with credentials + user list List all users + user get Get a user by ID + tenant list List all tenants + tenant get Get a tenant by ID +)"; +} + +void print_response(const cpr::Response &response) { + std::cout << "status: " << response.status_code << '\n'; + if (response.error) { + std::cout << "error: " << response.error.message << '\n'; + } + std::cout << response.text << '\n'; +} + +int handle_auth(const HttpClient &client, const std::vector &args) { + if (args.size() < 2) { + std::cout << "auth command requires a subcommand\n"; + print_help(); + return 1; + } + + if (args[1] == "session") { + print_response(client.get("/api/auth/session")); + return 0; + } + + if (args[1] == "login") { + if (args.size() != 4) { + std::cout << "auth login requires email and password\n"; + return 1; + } + std::string body = + "{\"email\":\"" + args[2] + "\",\"password\":\"" + args[3] + "\"}"; + print_response(client.post("/api/auth/login", body)); + return 0; + } + + std::cout << "unknown auth subcommand\n"; + print_help(); + return 1; +} + +int handle_user(const HttpClient &client, const std::vector &args) { + if (args.size() < 2) { + std::cout << "user command requires a subcommand\n"; + print_help(); + return 1; + } + + if (args[1] == "list") { + print_response(client.get("/api/users")); + return 0; + } + + if (args[1] == "get") { + if (args.size() != 3) { + std::cout << "user get requires a user ID\n"; + return 1; + } + print_response(client.get("/api/users/" + args[2])); + return 0; + } + + std::cout << "unknown user subcommand\n"; + print_help(); + return 1; +} + +int handle_tenant(const HttpClient &client, const std::vector &args) { + if (args.size() < 2) { + std::cout << "tenant command requires a subcommand\n"; + print_help(); + return 1; + } + + if (args[1] == "list") { + print_response(client.get("/api/tenants")); + return 0; + } + + if (args[1] == "get") { + if (args.size() != 3) { + std::cout << "tenant get requires a tenant ID\n"; + return 1; + } + print_response(client.get("/api/tenants/" + args[2])); + return 0; + } + + std::cout << "unknown tenant subcommand\n"; + print_help(); + return 1; +} + +} // namespace + +namespace commands { + +int dispatch(const HttpClient &client, const std::vector &args) { + if (args.empty()) { + print_help(); + return 0; + } + + if (args[0] == "auth") { + return handle_auth(client, args); + } + + if (args[0] == "user") { + return handle_user(client, args); + } + + if (args[0] == "tenant") { + return handle_tenant(client, args); + } + + print_help(); + return 1; +} + +} // namespace commands diff --git a/frontends/cli/src/commands/command_dispatch.h b/frontends/cli/src/commands/command_dispatch.h new file mode 100644 index 000000000..51236a0e6 --- /dev/null +++ b/frontends/cli/src/commands/command_dispatch.h @@ -0,0 +1,11 @@ +#pragma once + +#include "../utils/http_client.h" +#include +#include + +namespace commands { + +int dispatch(const HttpClient &client, const std::vector &args); + +} // namespace commands