This commit is contained in:
@ -1,6 +1,5 @@
|
||||
#include "version.hpp"
|
||||
#include "config.hpp"
|
||||
#include "service_runner.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
@ -148,113 +147,113 @@ auto command_match = [](const std::string& cmd_list, int argc, char* argv[]) ->
|
||||
}
|
||||
|
||||
|
||||
int old_main(int argc, char* argv[]) {
|
||||
HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2)))
|
||||
HAPPYEXIT("version", printversion())
|
||||
BOOLEXIT("test-template", gTemplateManager().test_template(safearg(argc,argv,2)))
|
||||
ASSERT(safearg(argc,argv,1) != "assert", "Hello! Here is an assert.");
|
||||
// int old_main(int argc, char* argv[]) {
|
||||
// HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2)))
|
||||
// HAPPYEXIT("version", printversion())
|
||||
// BOOLEXIT("test-template", gTemplateManager().test_template(safearg(argc,argv,2)))
|
||||
// ASSERT(safearg(argc,argv,1) != "assert", "Hello! Here is an assert.");
|
||||
|
||||
try {
|
||||
// silently attempt to load the config file and templates.
|
||||
gConfig().load_config();
|
||||
if (gConfig().is_config_set())
|
||||
gTemplateManager().load_sources();
|
||||
// try {
|
||||
// // silently attempt to load the config file and templates.
|
||||
// gConfig().load_config();
|
||||
// if (gConfig().is_config_set())
|
||||
// gTemplateManager().load_sources();
|
||||
|
||||
std::string cmd = argv[1];
|
||||
// std::string cmd = argv[1];
|
||||
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// from here we require the config file to be loaded.
|
||||
if (!gConfig().is_config_set())
|
||||
return die("Please run 'dropshell edit' to set up the dropshell configuration.");
|
||||
// // ------------------------------------------------------------
|
||||
// // from here we require the config file to be loaded.
|
||||
// if (!gConfig().is_config_set())
|
||||
// return die("Please run 'dropshell edit' to set up the dropshell configuration.");
|
||||
|
||||
|
||||
const std::vector<std::string> & server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||
if (server_definition_paths.size()>1) { // only show if there are multiple.
|
||||
std::cout << "Server definition paths: ";
|
||||
for (auto & dir : server_definition_paths)
|
||||
std::cout << "["<< dir << "] ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (gTemplateManager().is_loaded() && gTemplateManager().get_source_count() > 0)
|
||||
gTemplateManager().print_sources();
|
||||
// const std::vector<std::string> & server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||
// if (server_definition_paths.size()>1) { // only show if there are multiple.
|
||||
// std::cout << "Server definition paths: ";
|
||||
// for (auto & dir : server_definition_paths)
|
||||
// std::cout << "["<< dir << "] ";
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
// if (gTemplateManager().is_loaded() && gTemplateManager().get_source_count() > 0)
|
||||
// gTemplateManager().print_sources();
|
||||
|
||||
HAPPYEXIT("templates", gTemplateManager().list_templates());
|
||||
// HAPPYEXIT("templates", gTemplateManager().list_templates());
|
||||
|
||||
if (cmd == "create-template") {
|
||||
if (argc < 3) return die("Error: create-template requires a template name");
|
||||
return (gTemplateManager().create_template(argv[2])) ? 0 : 1;
|
||||
}
|
||||
// if (cmd == "create-template") {
|
||||
// if (argc < 3) return die("Error: create-template requires a template name");
|
||||
// return (gTemplateManager().create_template(argv[2])) ? 0 : 1;
|
||||
// }
|
||||
|
||||
if (cmd == "create-server") {
|
||||
if (argc < 3) return die("Error: create-server requires a server name");
|
||||
return (create_server(argv[2])) ? 0 : 1;
|
||||
}
|
||||
// if (cmd == "create-server") {
|
||||
// if (argc < 3) return die("Error: create-server requires a server name");
|
||||
// return (create_server(argv[2])) ? 0 : 1;
|
||||
// }
|
||||
|
||||
if (cmd == "create-service") {
|
||||
if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service");
|
||||
return (create_service(argv[2], argv[3], argv[4])) ? 0 : 1;
|
||||
}
|
||||
// if (cmd == "create-service") {
|
||||
// if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service");
|
||||
// return (create_service(argv[2], argv[3], argv[4])) ? 0 : 1;
|
||||
// }
|
||||
|
||||
if (cmd == "ssh" && argc < 4) {
|
||||
if (argc < 3) return die("Error: ssh requires a server name and optionally service name");
|
||||
service_runner::interactive_ssh(argv[2], "bash");
|
||||
return 0;
|
||||
}
|
||||
// if (cmd == "ssh" && argc < 4) {
|
||||
// if (argc < 3) return die("Error: ssh requires a server name and optionally service name");
|
||||
// service_runner::interactive_ssh(argv[2], "bash");
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
|
||||
// handle running a command.
|
||||
std::set<std::string> commands;
|
||||
get_all_used_commands(commands);
|
||||
// // handle running a command.
|
||||
// std::set<std::string> commands;
|
||||
// get_all_used_commands(commands);
|
||||
|
||||
autocomplete::merge_commands(commands, autocomplete::service_commands_require_config); // handled by service_runner, but not in template_shell_commands.
|
||||
// autocomplete::merge_commands(commands, autocomplete::service_commands_require_config); // handled by service_runner, but not in template_shell_commands.
|
||||
|
||||
if (commands.count(cmd)) {
|
||||
std::set<std::string> safe_commands = {"nuke", "fullnuke"};
|
||||
if (safe_commands.count(cmd) && argc < 4)
|
||||
return die("Error: "+cmd+" requires a server name and service name. For safety, can't run on all services.");
|
||||
// if (commands.count(cmd)) {
|
||||
// std::set<std::string> safe_commands = {"nuke", "fullnuke"};
|
||||
// if (safe_commands.count(cmd) && argc < 4)
|
||||
// return die("Error: "+cmd+" requires a server name and service name. For safety, can't run on all services.");
|
||||
|
||||
// get all the services to run the command on.
|
||||
ServerAndServices server_and_services;
|
||||
if (!getCLIServices(safearg(argc, argv, 2), safearg(argc, argv, 3), server_and_services))
|
||||
return die("Error: "+cmd+" command requires server name and optionally service name");
|
||||
// // get all the services to run the command on.
|
||||
// ServerAndServices server_and_services;
|
||||
// if (!getCLIServices(safearg(argc, argv, 2), safearg(argc, argv, 3), server_and_services))
|
||||
// return die("Error: "+cmd+" command requires server name and optionally service name");
|
||||
|
||||
// run the command on each service.
|
||||
for (const auto& service_info : server_and_services.servicelist) {
|
||||
if (!SIvalid(service_info))
|
||||
std::cerr<<"Error: Unable to get service information."<<std::endl;
|
||||
else {
|
||||
service_runner runner(server_and_services.server_name, service_info.service_name);
|
||||
if (!runner.isValid())
|
||||
return die("Error: Failed to initialize service");
|
||||
// // run the command on each service.
|
||||
// for (const auto& service_info : server_and_services.servicelist) {
|
||||
// if (!SIvalid(service_info))
|
||||
// std::cerr<<"Error: Unable to get service information."<<std::endl;
|
||||
// else {
|
||||
// service_runner runner(server_and_services.server_name, service_info.service_name);
|
||||
// if (!runner.isValid())
|
||||
// return die("Error: Failed to initialize service");
|
||||
|
||||
std::vector<std::string> additional_args;
|
||||
for (int i=4; i<argc; i++)
|
||||
additional_args.push_back(argv[i]);
|
||||
if (!runner.run_command(cmd, additional_args))
|
||||
return die(cmd+" failed on service "+service_info.service_name);
|
||||
}
|
||||
}
|
||||
// std::vector<std::string> additional_args;
|
||||
// for (int i=4; i<argc; i++)
|
||||
// additional_args.push_back(argv[i]);
|
||||
// if (!runner.run_command(cmd, additional_args))
|
||||
// return die(cmd+" failed on service "+service_info.service_name);
|
||||
// }
|
||||
// }
|
||||
|
||||
// success!
|
||||
return 0;
|
||||
}
|
||||
// // success!
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// Unknown command
|
||||
std::cerr << "Error: Unknown command '" << cmd << "'" << std::endl;
|
||||
std::cerr << "Valid commands: ";
|
||||
for (const auto& command : commands) {
|
||||
if (!command.empty() && command[0]!='_')
|
||||
std::cerr << command << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
return 1;
|
||||
// // Unknown command
|
||||
// std::cerr << "Error: Unknown command '" << cmd << "'" << std::endl;
|
||||
// std::cerr << "Valid commands: ";
|
||||
// for (const auto& command : commands) {
|
||||
// if (!command.empty() && command[0]!='_')
|
||||
// std::cerr << command << " ";
|
||||
// }
|
||||
// std::cerr << std::endl;
|
||||
// return 1;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// } catch (const std::exception& e) {
|
||||
// std::cerr << "Error: " << e.what() << std::endl;
|
||||
// return 1;
|
||||
// }
|
||||
// }
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
|
Reference in New Issue
Block a user