Rework backup/restore
This commit is contained in:
75
src/main.cpp
75
src/main.cpp
@ -8,7 +8,6 @@
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/readmes.hpp"
|
||||
#include "autocomplete.hpp"
|
||||
#include "main_commands.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
@ -69,17 +68,55 @@ int die(const std::string & msg) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool parseargs(std::string arg2, std::string arg3, std::string & server_name, std::vector<LocalServiceInfo>& servicelist)
|
||||
int init(const std::vector<std::string> &args)
|
||||
{
|
||||
std::string lcd;
|
||||
|
||||
if (args.size() < 3) {
|
||||
std::cerr << "Error: init command requires a directory argument" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
if (!gConfig().add_local_config_directory(args[2]))
|
||||
return 1; // error already reported
|
||||
|
||||
gConfig().save_config();
|
||||
std::cout << "Config directory added: " << gConfig().get_local_config_directories().back() << std::endl;
|
||||
dropshell::create_readme_local_config_dir(gConfig().get_local_config_directories().back());
|
||||
|
||||
if (gConfig().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 : gConfig().get_local_config_directories()) {
|
||||
std::cout << " " << dir << std::endl;
|
||||
}
|
||||
std::cout << "You can edit the config file manually with: dropshell edit" << std::endl;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error in init: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ServerAndServices {
|
||||
std::string server_name;
|
||||
std::vector<LocalServiceInfo> servicelist;
|
||||
};
|
||||
|
||||
bool getCLIServices(const std::string & arg2, const std::string & arg3,
|
||||
ServerAndServices & server_and_services)
|
||||
{
|
||||
if (arg2.empty()) return false;
|
||||
server_name = arg2;
|
||||
server_and_services.server_name = arg2;
|
||||
|
||||
if (arg3.empty()) {
|
||||
servicelist = get_server_services_info(server_name);
|
||||
server_and_services.servicelist = get_server_services_info(arg2);
|
||||
} else {
|
||||
servicelist.push_back(get_service_info(server_name, arg3));
|
||||
server_and_services.servicelist.push_back(get_service_info(arg2, arg3));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -126,7 +163,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
if (cmd == "init") {
|
||||
return main_commands::init(argvec);
|
||||
return init(argvec);
|
||||
}
|
||||
|
||||
if (cmd == "help" || cmd == "-h" || cmd == "--help" || cmd== "h" || cmd=="halp") {
|
||||
@ -203,36 +240,28 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "backup" || cmd=="backups") {
|
||||
if (argc < 4) return die("Error: backup requires a target server and target service to back up");
|
||||
return main_commands::backup(argvec);
|
||||
}
|
||||
|
||||
if (cmd == "restore") {
|
||||
if (argc < 4) return die("Error: restore requires a target server, target service the backup file to restore");
|
||||
return main_commands::restore(argvec);
|
||||
}
|
||||
|
||||
// handle running a command.
|
||||
std::set<std::string> commands;
|
||||
get_all_used_commands(commands);
|
||||
commands.merge(std::set<std::string>{"ssh","edit","_allservicesstatus"}); // handled by service_runner, but not in template_shell_commands.
|
||||
for (const auto& command : commands) {
|
||||
if (cmd == command) {
|
||||
std::string server_name;
|
||||
std::vector<LocalServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
ServerAndServices server_and_services;
|
||||
if (!getCLIServices(safearg(argc, argv, 2), safearg(argc, argv, 3), server_and_services)) {
|
||||
std::cerr << "Error: " << command << " command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const auto& service_info : servicelist) {
|
||||
service_runner runner(server_name, service_info.service_name);
|
||||
for (const auto& service_info : server_and_services.servicelist) {
|
||||
service_runner runner(server_and_services.server_name, service_info.service_name);
|
||||
if (!runner.isValid()) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (!runner.run_command(command)) {
|
||||
std::vector<std::string> additional_args;
|
||||
for (int i=4; i<argc; i++)
|
||||
additional_args.push_back(argv[i]);
|
||||
if (!runner.run_command(command, additional_args)) {
|
||||
std::cerr << command +" failed." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user