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 "autocomplete.hpp"
#include "main_commands.hpp"
#include "utils/hash.hpp"
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <chrono>
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<dropshell::ServiceInfo>& 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 <path>' 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 <path>' to initialise the user directory and create a configuration file.");
const std::vector<std::string> & 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<std::string> 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;
}
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

View File

@ -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