This commit is contained in:
John 2025-04-27 21:45:54 +12:00
parent b1e51b91d2
commit 66bc230f71
3 changed files with 34 additions and 33 deletions

View File

@ -9,12 +9,14 @@
#include "utils/readmes.hpp" #include "utils/readmes.hpp"
#include "autocomplete.hpp" #include "autocomplete.hpp"
#include "main_commands.hpp" #include "main_commands.hpp"
#include "utils/hash.hpp"
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <iomanip> #include <iomanip>
#include <chrono>
namespace dropshell { namespace dropshell {
@ -57,6 +59,10 @@ void print_help() {
} // namespace dropshell } // namespace dropshell
int die(const std::string & msg) {
std::cerr << msg << std::endl;
return 1;
}
bool parseargs(std::string arg2, std::string arg3, std::string & server_name, std::vector<dropshell::ServiceInfo>& servicelist) bool parseargs(std::string arg2, std::string arg3, std::string & server_name, std::vector<dropshell::ServiceInfo>& servicelist)
{ {
@ -116,10 +122,8 @@ int main(int argc, char* argv[]) {
// ------------------------------------------------------------ // ------------------------------------------------------------
// from here we require the config file to be loaded. // from here we require the config file to be loaded.
if (!cfg->is_config_set()) { if (!cfg->is_config_set())
std::cerr << "Please run 'dropshell init <path>' to initialise the user directory and create a configuration file." << std::endl; return die("Please run 'dropshell init <path>' to initialise the user directory and create a configuration file.");
return 1;
}
const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories(); const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories();
std::cout << "Config directories: "; std::cout << "Config directories: ";
@ -140,8 +144,7 @@ int main(int argc, char* argv[]) {
cmd="logs"; cmd="logs";
break; break;
default: default:
std::cerr << "dropshell server: too many arguments" << std::endl; return die("dropshell server: too many arguments");
return 1;
} }
if (cmd == "templates") { if (cmd == "templates") {
@ -150,39 +153,25 @@ int main(int argc, char* argv[]) {
} }
if (cmd == "create-template") { if (cmd == "create-template") {
if (argc < 3) { if (argc < 3) return die("Error: create-template requires a template name");
std::cerr << "Error: create-template requires a template name" << std::endl;
return 1;
}
dropshell::create_template(argv[2]); dropshell::create_template(argv[2]);
return 0; return 0;
} }
if (cmd == "create-server") { if (cmd == "create-server") {
if (argc < 3) { if (argc < 3) return die("Error: create-server requires a server name");
std::cerr << "Error: create-server requires a server name" << std::endl;
return 1;
}
dropshell::create_server(argv[2]); dropshell::create_server(argv[2]);
return 0; return 0;
} }
if (cmd == "create-service") { if (cmd == "create-service") {
if (argc < 5) { if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service");
std::cerr << "Error: not enough arguments." << std::endl;
std::cerr << "dropshell create-service server template service" << std::endl;
return 1;
}
dropshell::create_service(argv[2], argv[3], argv[4]); dropshell::create_service(argv[2], argv[3], argv[4]);
return 0; return 0;
} }
if (cmd == "ssh" && argc < 4) { if (cmd == "ssh" && argc < 4) {
if (argc < 3) if (argc < 3) return die("Error: ssh requires a server name and optionally service name");
{
std::cerr << "Error: ssh requires a server name and optionally service name" << std::endl;
return 1;
}
dropshell::interactive_ssh(argv[2], "bash"); dropshell::interactive_ssh(argv[2], "bash");
return 0; return 0;
} }
@ -194,21 +183,21 @@ int main(int argc, char* argv[]) {
} }
if (cmd == "backup" || cmd=="backups") { if (cmd == "backup" || cmd=="backups") {
if (argc < 4) { if (argc < 4) return die("Error: backup requires a target server and target service to back up");
std::cerr << "Error: backup requires a target server and target service to back up" << std::endl;
return 1;
}
return dropshell::main_commands::backup(argvec); return dropshell::main_commands::backup(argvec);
} }
if (cmd == "restore") { if (cmd == "restore") {
if (argc < 4) { if (argc < 4) return die("Error: restore requires a target server, target service the backup file to restore");
std::cerr << "Error: restore requires a target server, target service the backup file to restore" << std::endl;
return 1;
}
return dropshell::main_commands::restore(argvec); return dropshell::main_commands::restore(argvec);
} }
if (cmd == "hash") {
if (argc < 3) return die("Error: hash requires a directory to hash");
dropshell::hash_demo(argvec[2]);
return 0;
}
// handle running a command. // handle running a command.
std::set<std::string> commands; std::set<std::string> commands;
dropshell::get_all_used_commands(commands); dropshell::get_all_used_commands(commands);

View File

@ -96,4 +96,14 @@ XXH64_hash_t hash_directory_recursive(const std::string &path) {
return hash; return hash;
} }
void hash_demo(const std::string & path)
{
std::cout << "Hashing directory: " << path << std::endl;
auto start = std::chrono::high_resolution_clock::now();
XXH64_hash_t hash = hash_directory_recursive(path);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
}
} // namespace dropshell } // namespace dropshell

View File

@ -10,6 +10,8 @@ namespace dropshell {
XXH64_hash_t hash_directory_recursive(const std::string &path); XXH64_hash_t hash_directory_recursive(const std::string &path);
void hash_demo(const std::string & path);
} // namespace dropshell } // namespace dropshell