Fairly big refactor...
This commit is contained in:
97
src/main.cpp
97
src/main.cpp
@ -1,7 +1,9 @@
|
||||
#include "dropshell.hpp"
|
||||
#include "server_service.hpp"
|
||||
#include "autocomplete.hpp"
|
||||
#include "init_user_directory.hpp"
|
||||
#include "main.hpp"
|
||||
#include "config.hpp"
|
||||
#include "service_runner.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
@ -28,28 +30,20 @@ void print_help() {
|
||||
std::cout << " backup SERVER [SERVICE] Backup service(s)." << std::endl;
|
||||
std::cout << " COMMAND SERVER [SERVICE] Run a custom command on service(s)." << 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
|
||||
|
||||
|
||||
bool parseargs(std::string arg2, std::string arg3, std::string & server_name, std::vector<std::string>& servicelist)
|
||||
bool parseargs(std::string arg2, std::string arg3, std::string & server_name, std::vector<dropshell::ServiceInfo>& servicelist)
|
||||
{
|
||||
if (arg2.empty()) return false;
|
||||
server_name = arg2;
|
||||
|
||||
if (arg3.empty()) {
|
||||
servicelist = dropshell::get_server_services(server_name);
|
||||
servicelist = dropshell::get_server_services_info(server_name);
|
||||
} else {
|
||||
servicelist.push_back(arg3);
|
||||
servicelist.push_back(dropshell::get_service_info(server_name, arg3));
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -63,6 +57,7 @@ std::string safearg(int argc, char *argv[], int index)
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
dropshell::config *cfg = dropshell::get_global_config();
|
||||
|
||||
// Handle commands
|
||||
std::string cmd;
|
||||
@ -77,7 +72,7 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
dropshell::init_user_directory(argv[2]);
|
||||
cfg->init_local_config_directory(argv[2]);
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
@ -96,27 +91,27 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
// silently attempt to load the config file.
|
||||
dropshell::load_config();
|
||||
cfg->load_config();
|
||||
|
||||
// auto completion stuff.
|
||||
std::set<std::string> commands;
|
||||
std::vector<dropshell::ServerInfo> servers = dropshell::get_configured_servers();
|
||||
for (const auto& server : servers)
|
||||
{
|
||||
std::vector<dropshell::ServiceInfo> services = dropshell::get_server_services_info(server.name);
|
||||
for (const auto& service : services)
|
||||
commands.merge(dropshell::get_used_commands(server.name, service.service_name));
|
||||
}
|
||||
|
||||
// auto compeltion stuff.
|
||||
auto commands = dropshell::autocomplete_list_commands();
|
||||
if (cmd == "autocomplete_list_commands") {
|
||||
// add in standard commands.
|
||||
commands.insert(commands.end(), {
|
||||
"help",
|
||||
"version",
|
||||
"init"
|
||||
commands.merge(std::set<std::string>{
|
||||
"help","version","init"
|
||||
});
|
||||
if (dropshell::is_config_loaded()) { // these only work if the config is loaded.
|
||||
commands.insert(commands.end(), {
|
||||
"servers",
|
||||
"templates",
|
||||
"install",
|
||||
"backup"
|
||||
if (cfg->is_config_set())
|
||||
commands.merge(std::set<std::string>{
|
||||
"servers","templates","install","backup"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
for (const auto& command : commands) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
@ -124,11 +119,11 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_servers") {
|
||||
if (dropshell::is_config_loaded())
|
||||
if (cfg->is_config_set())
|
||||
{
|
||||
auto servers = dropshell::autocomplete_list_servers();
|
||||
auto servers = dropshell::get_configured_servers();
|
||||
for (const auto& server : servers)
|
||||
std::cout << server << std::endl;
|
||||
std::cout << server.name << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -138,17 +133,17 @@ int main(int argc, char* argv[]) {
|
||||
std::cerr << "Error: autocomplete_list_services requires a server name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (dropshell::is_config_loaded()) {
|
||||
auto services = dropshell::autocomplete_list_services(argv[2]);
|
||||
if (cfg->is_config_set()) {
|
||||
auto services = dropshell::get_server_services_info(argv[2]);
|
||||
for (const auto& service : services)
|
||||
std::cout << service << std::endl;
|
||||
std::cout << service.service_name << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// from here we require the config file to be loaded.
|
||||
if (!dropshell::is_config_loaded()) {
|
||||
if (!cfg->is_config_set()) {
|
||||
std::cerr << "Error: Failed to load configuration." << std::endl << "Please run 'dropshell init <path>' to initialise the user directory and create a configuration file." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -177,14 +172,14 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
if (cmd == "install") {
|
||||
std::string server_name;
|
||||
std::vector<std::string> servicelist;
|
||||
std::vector<dropshell::ServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
std::cerr << "Error: install command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
for (const auto& service_name : servicelist) {
|
||||
dropshell::server_service service;
|
||||
if (!service.init(server_name, service_name)) {
|
||||
for (const auto& service_info : servicelist) {
|
||||
dropshell::service_runner service;
|
||||
if (!service.init(server_name, service_info.service_name)) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -199,15 +194,15 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
if (cmd == "backup") {
|
||||
std::string server_name;
|
||||
std::vector<std::string> servicelist;
|
||||
std::vector<dropshell::ServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
std::cerr << "Error: backup command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const auto& service_name : servicelist) {
|
||||
dropshell::server_service service;
|
||||
if (!service.init(server_name, service_name)) {
|
||||
for (const auto& service_info : servicelist) {
|
||||
dropshell::service_runner service;
|
||||
if (!service.init(server_name, service_info.service_name)) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -224,15 +219,15 @@ int main(int argc, char* argv[]) {
|
||||
for (const auto& command : commands) {
|
||||
if (cmd == command) {
|
||||
std::string server_name;
|
||||
std::vector<std::string> servicelist;
|
||||
std::vector<dropshell::ServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
std::cerr << "Error: " << command << " command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const auto& service_name : servicelist) {
|
||||
dropshell::server_service service;
|
||||
if (!service.init(server_name, service_name)) {
|
||||
for (const auto& service_info : servicelist) {
|
||||
dropshell::service_runner service;
|
||||
if (!service.init(server_name, service_info.service_name)) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user