From 66bc230f71f095e29237cba92b83b0065929fc25 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 27 Apr 2025 21:45:54 +1200 Subject: [PATCH] Tidying --- src/main.cpp | 55 +++++++++++++++++++--------------------------- src/utils/hash.cpp | 10 +++++++++ src/utils/hash.hpp | 2 ++ 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cbe2dde..8cad30a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,14 @@ #include "utils/readmes.hpp" #include "autocomplete.hpp" #include "main_commands.hpp" +#include "utils/hash.hpp" #include #include #include #include #include +#include namespace dropshell { @@ -57,6 +59,10 @@ void print_help() { } // 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& servicelist) { @@ -116,10 +122,8 @@ int main(int argc, char* argv[]) { // ------------------------------------------------------------ // from here we require the config file to be loaded. - if (!cfg->is_config_set()) { - std::cerr << "Please run 'dropshell init ' to initialise the user directory and create a configuration file." << std::endl; - return 1; - } + if (!cfg->is_config_set()) + return die("Please run 'dropshell init ' to initialise the user directory and create a configuration file."); const std::vector & local_config_directories = cfg->get_local_config_directories(); std::cout << "Config directories: "; @@ -140,8 +144,7 @@ int main(int argc, char* argv[]) { cmd="logs"; break; default: - std::cerr << "dropshell server: too many arguments" << std::endl; - return 1; + return die("dropshell server: too many arguments"); } if (cmd == "templates") { @@ -150,39 +153,25 @@ int main(int argc, char* argv[]) { } if (cmd == "create-template") { - if (argc < 3) { - std::cerr << "Error: create-template requires a template name" << std::endl; - return 1; - } + if (argc < 3) return die("Error: create-template requires a template name"); dropshell::create_template(argv[2]); return 0; } if (cmd == "create-server") { - if (argc < 3) { - std::cerr << "Error: create-server requires a server name" << std::endl; - return 1; - } + if (argc < 3) return die("Error: create-server requires a server name"); dropshell::create_server(argv[2]); return 0; } if (cmd == "create-service") { - if (argc < 5) { - std::cerr << "Error: not enough arguments." << std::endl; - std::cerr << "dropshell create-service server template service" << std::endl; - return 1; - } + if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service"); dropshell::create_service(argv[2], argv[3], argv[4]); return 0; } - if (cmd == "ssh" && argc < 4) { - if (argc < 3) - { - std::cerr << "Error: ssh requires a server name and optionally service name" << std::endl; - return 1; - } + if (cmd == "ssh" && argc < 4) { + if (argc < 3) return die("Error: ssh requires a server name and optionally service name"); dropshell::interactive_ssh(argv[2], "bash"); return 0; } @@ -194,21 +183,21 @@ int main(int argc, char* argv[]) { } if (cmd == "backup" || cmd=="backups") { - if (argc < 4) { - std::cerr << "Error: backup requires a target server and target service to back up" << std::endl; - return 1; - } + if (argc < 4) return die("Error: backup requires a target server and target service to back up"); return dropshell::main_commands::backup(argvec); } if (cmd == "restore") { - if (argc < 4) { - std::cerr << "Error: restore requires a target server, target service the backup file to restore" << std::endl; - return 1; - } + if (argc < 4) return die("Error: restore requires a target server, target service the backup file to restore"); 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. std::set commands; dropshell::get_all_used_commands(commands); diff --git a/src/utils/hash.cpp b/src/utils/hash.cpp index 234a7c0..8dfc47c 100644 --- a/src/utils/hash.cpp +++ b/src/utils/hash.cpp @@ -96,4 +96,14 @@ XXH64_hash_t hash_directory_recursive(const std::string &path) { 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(end - start); + std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl; +} + } // namespace dropshell diff --git a/src/utils/hash.hpp b/src/utils/hash.hpp index de96840..4e50537 100644 --- a/src/utils/hash.hpp +++ b/src/utils/hash.hpp @@ -10,6 +10,8 @@ namespace dropshell { XXH64_hash_t hash_directory_recursive(const std::string &path); + void hash_demo(const std::string & path); + } // namespace dropshell