dropshell/src/main.cpp
Your Name 3fa5d5076a .
2025-04-21 15:34:18 +12:00

193 lines
6.5 KiB
C++

#include "dropshell.hpp"
#include "server_service.hpp"
#include "autocomplete.hpp"
#include "init_user_directory.hpp"
#include "config.hpp"
#include <iostream>
#include <string>
#include <vector>
namespace dropshell {
void print_help() {
std::cout << "Usage: dropshell [OPTIONS] COMMAND [ARGS]" << std::endl;
std::cout << std::endl;
std::cout << "A tool for managing server configurations" << std::endl;
std::cout << std::endl;
std::cout << "Commands:" << std::endl;
std::cout << " help Show this help message" << std::endl;
std::cout << " version Show version information" << std::endl;
std::cout << " init DIR Initialize the user directory for server configurations" << std::endl;
std::cout << std::endl;
std::cout << " servers List configured servers" << std::endl;
std::cout << " servers NAME Show details for specific server" << std::endl;
std::cout << " templates List available templates" << std::endl;
std::cout << std::endl;
std::cout << " install SERVER SERVICE (Re)install a service on a server." << std::endl;
std::cout << " backup SERVER SERVICE Backup a service on a server." << std::endl;
std::cout << " run SERVER SERVICE COMMAND Run a command on a specific service" << std::endl;
std::cout << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " dropshell servers" << std::endl;
std::cout << " dropshell servers myserver" << std::endl;
std::cout << " dropshell init /path/to/directory" << std::endl;
std::cout << " dropshell templates" << std::endl;
std::cout << " dropshell install myserver myservice" << std::endl;
std::cout << " dropshell run myserver myservice status" << std::endl;
std::cout << " dropshell backup myserver myservice" << std::endl;
}
} // namespace dropshell
int main(int argc, char* argv[]) {
try {
// Load configuration
if (!dropshell::load_config()) {
std::cerr << "Error: Failed to load configuration" << std::endl;
return 1;
}
// No arguments provided
if (argc < 2) {
dropshell::print_help();
return 1;
}
// Handle commands
std::string cmd = argv[1];
if (cmd == "help" || cmd == "-h" || cmd == "--help") {
dropshell::print_help();
return 0;
}
if (cmd == "version" || cmd == "-V" || cmd == "--version") {
dropshell::print_version();
return 0;
}
if (cmd == "servers") {
if (argc > 2) {
// Show details for specific server
dropshell::show_server_details(argv[2]);
} else {
// List all servers
dropshell::list_servers();
}
return 0;
}
if (cmd == "templates") {
dropshell::list_templates();
return 0;
}
if (cmd == "init") {
if (argc < 3) {
std::cerr << "Error: init command requires a directory argument" << std::endl;
return 1;
}
try {
dropshell::init_user_directory(argv[2]);
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}
if (cmd == "autocomplete_list_servers") {
auto servers = dropshell::autocomplete_list_servers();
for (const auto& server : servers) {
std::cout << server << std::endl;
}
return 0;
}
if (cmd == "autocomplete_list_services") {
if (argc < 3) {
std::cerr << "Error: autocomplete_list_services requires a server name" << std::endl;
return 1;
}
auto services = dropshell::autocomplete_list_services(argv[2]);
for (const auto& service : services) {
std::cout << service << std::endl;
}
return 0;
}
if (cmd == "install") {
if (argc < 4) {
std::cerr << "Error: install command requires server name and service name" << std::endl;
return 1;
}
std::string server_name = argv[2];
std::string service_name = argv[3];
dropshell::server_service service;
if (!service.init(server_name, service_name)) {
std::cerr << "Error: Failed to initialize service" << std::endl;
return 1;
}
if (!service.install()) {
std::cerr << "Error: Failed to install service" << std::endl;
return 1;
}
return 0;
}
if (cmd == "run") {
if (argc < 5) {
std::cerr << "Error: run command requires server name, service name, and command" << std::endl;
return 1;
}
std::string server_name = argv[2];
std::string service_name = argv[3];
std::string command = argv[4];
dropshell::server_service service;
if (!service.init(server_name, service_name)) {
std::cerr << "Error: Failed to initialize service" << std::endl;
return 1;
}
if (!service.run_command(command)) {
std::cerr << command +" failed." << std::endl;
return 1;
}
return 0;
}
if (cmd == "backup") {
if (argc < 4) {
std::cerr << "Error: backup command requires server name and service name" << std::endl;
return 1;
}
std::string server_name = argv[2];
std::string service_name = argv[3];
dropshell::server_service service;
if (!service.init(server_name, service_name)) {
std::cerr << "Error: Failed to initialize service" << std::endl;
return 1;
}
if (!service.backup()) {
std::cerr << "Backup failed." << std::endl;
return 1;
}
return 0;
}
// Unknown command
std::cerr << "Error: Unknown command '" << cmd << "'" << std::endl;
dropshell::print_help();
return 1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}