This commit is contained in:
Your Name
2025-04-21 16:12:06 +12:00
parent 7d1db266b0
commit bd27109a2b
5 changed files with 64 additions and 65 deletions

View File

@ -18,14 +18,14 @@ void print_help() {
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 << " init DIR Set the local directory for all server configs and backups" << std::endl;
std::cout << std::endl;
std::cout << " servers List configured servers" << std::endl;
std::cout << " servers Summary of all 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 << " templates List all 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 << " install SERVER [SERVICE] (Re)install a specific service on a server." << std::endl;
std::cout << " backup SERVER [SERVICE] Backup a specific 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;
@ -118,22 +118,33 @@ int main(int argc, char* argv[]) {
}
if (cmd == "install") {
if (argc < 4) {
std::cerr << "Error: install command requires server name and service name" << std::endl;
if (argc < 3) {
std::cerr << "Error: install command requires server name and optionally 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;
std::vector<std::string> servicelist;
if (argc <= 3) {
servicelist = dropshell::get_server_services(server_name);
if (servicelist.empty()) {
std::cerr << "Error: No services found for server " << server_name << std::endl;
return 1;
}
} else
servicelist.push_back(argv[3]);
for (const auto& service_name : servicelist) {
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;
}
@ -161,22 +172,32 @@ int main(int argc, char* argv[]) {
}
if (cmd == "backup") {
if (argc < 4) {
std::cerr << "Error: backup command requires server name and service name" << std::endl;
if (argc < 3) {
std::cerr << "Error: backup command requires server name and optionally 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;
std::vector<std::string> servicelist;
if (argc <= 3) {
servicelist = dropshell::get_server_services(server_name);
if (servicelist.empty()) {
std::cerr << "Error: No services found for server " << server_name << std::endl;
return 1;
}
} else
servicelist.push_back(argv[3]);
for (const auto& service_name : servicelist) {
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;
}

View File

@ -131,7 +131,14 @@ bool server_service::execute_local_command(const std::string& command, const std
return okay;
}
void server_service::maketitle(const std::string& title) const {
std::cout << std::string(title.length() + 4, '-') << std::endl;
std::cout << "| " << title << " |" << std::endl;
std::cout << std::string(title.length() + 4, '-') << std::endl;
}
bool server_service::install() {
maketitle("Installing " + m_service_name + " on " + m_server_name);
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
@ -226,6 +233,8 @@ bool server_service::run_command(const std::string& command) {
}
bool server_service::backup() {
maketitle("Backing up " + m_service_name + " on " + m_server_name);
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;

View File

@ -66,6 +66,7 @@ class server_service {
bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const;
bool execute_ssh_command(const std::string& command, const std::string& error_msg) const;
bool execute_local_command(const std::string& command, const std::string& error_msg) const;
void maketitle(const std::string& title) const;
};
} // namespace dropshell