This commit is contained in:
Your Name
2025-04-26 09:09:20 +12:00
parent 9e6281c846
commit cf8a7db01d
9 changed files with 178 additions and 157 deletions

View File

@ -7,6 +7,8 @@
#include "templates.hpp"
#include "utils/utils.hpp"
#include "utils/readmes.hpp"
#include "autocomplete.hpp"
#include "main_commands.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
@ -74,42 +76,23 @@ int main(int argc, char* argv[]) {
// silently attempt to load the config file.
cfg->load_config();
if (argc == 1) {
if (argc < 2) {
dropshell::print_help();
return 0;
}
std::string cmd = argv[1];
std::vector<std::string> argvec;
for (int i=0; i<argc; i++)
argvec.push_back(argv[i]);
if (cmd == "autocomplete") {
dropshell::autocomplete(argvec);
return 0;
}
// don't load old config if we're initializing
if (cmd == "init") {
std::string lcd;
if (argc < 3) {
std::cerr << "Error: init command requires a directory argument" << std::endl;
return 1;
}
try {
if (!cfg->add_local_config_directory(argv[2]))
return 1; // error already reported
cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
if (cfg->get_local_config_directories().size() ==1)
std::cout << "DropShell is now initialised and you can add a server with 'dropshell create-server <server-name>'" << std::endl;
else
{
std::cout << "DropShell will now use all of the following directories for configuration:" << std::endl;
for (const auto& dir : cfg->get_local_config_directories()) {
std::cout << " " << dir << std::endl;
}
std::cout << "You can edit the config file manually at: " << dropshell::get_local_dropshell_config_path() << std::endl;
}
return 0;
} catch (const std::exception& e) {
std::cerr << "Error in init: " << e.what() << std::endl;
return 1;
}
return dropshell::main_commands::init(argvec);
}
if (cmd == "help" || cmd == "-h" || cmd == "--help" || cmd== "h" || cmd=="halp") {
@ -117,69 +100,6 @@ int main(int argc, char* argv[]) {
return 0;
}
// auto completion stuff.
std::set<std::string> template_shell_commands, full_command_set;
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)
template_shell_commands.merge(dropshell::get_used_commands(server.name, service.service_name));
}
if (cmd == "autocomplete_list_commands") { // add in commands handled here, not by the template shell scripts.
std::set<std::string> full_command_set = template_shell_commands;
full_command_set.merge(std::set<std::string>{
"help","init" // these are always available.
});
if (cfg->is_config_set())
full_command_set.merge(std::set<std::string>{
"server","templates","create-service","create-template","create-server","edit","ssh",
"view" // only if we have a config.
});
for (const auto& command : full_command_set) {
std::cout << command << std::endl;
}
return 0;
}
if (cmd == "autocomplete_list_servers") {
if (cfg->is_config_set())
{
auto servers = dropshell::get_configured_servers();
for (const auto& server : servers)
std::cout << server.name << 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;
}
if (cfg->is_config_set()) {
auto services = dropshell::get_server_services_info(argv[2]);
for (const auto& service : services)
std::cout << service.service_name << std::endl;
}
return 0;
}
if (cmd == "autocomplete_list_backups") {
if (argc < 4) {
std::cerr << "Error: autocomplete_list_backups requires a server name and service name" << std::endl;
return 1;
}
if (cfg->is_config_set()) {
auto backups = dropshell::list_backups(argv[2], argv[3]);
for (const auto& backup : backups)
std::cout << backup << std::endl;
}
return 0;
}
// ------------------------------------------------------------
// from here we require the config file to be loaded.
if (!cfg->is_config_set()) {
@ -187,17 +107,11 @@ int main(int argc, char* argv[]) {
return 1;
}
std::string lcd;
const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories();
std::cout << "Config directories: ";
for (auto & dir : local_config_directories)
std::cout << "["<< dir << "] " << std::endl;
std::cout << std::endl;;
// No arguments provided
if (argc < 2) {
dropshell::print_help();
return 0;
}
std::cout << std::endl;
if (cmd == "server" || cmd == "servers" || cmd == "view" || cmd == "views" || cmd == "v")
switch (argc)
@ -268,7 +182,8 @@ int main(int argc, char* argv[]) {
}
// handle running a command.
std::set<std::string> commands = template_shell_commands;
std::set<std::string> commands;
dropshell::get_all_used_commands(commands);
commands.merge(std::set<std::string>{"ssh","edit"}); // handled by service_runner, but not in template_shell_commands.
for (const auto& command : commands) {
if (cmd == command) {