This commit is contained in:
56
src/main.cpp
56
src/main.cpp
@ -8,6 +8,7 @@
|
||||
#include "utils/utils.hpp"
|
||||
#include "autocomplete.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
@ -17,7 +18,7 @@
|
||||
#include <chrono>
|
||||
#include <libassert/assert.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#include <algorithm>
|
||||
namespace dropshell {
|
||||
|
||||
extern const std::string VERSION;
|
||||
@ -25,6 +26,15 @@ extern const std::string RELEASE_DATE;
|
||||
extern const std::string AUTHOR;
|
||||
extern const std::string LICENSE;
|
||||
|
||||
void show_command(const std::string& cmd) {
|
||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
if (cmd_info) {
|
||||
std::cout << " " << cmd_info->help_usage
|
||||
<< std::string(' ', std::min(1,(int)(30-cmd_info->help_usage.length())))
|
||||
<< cmd_info->help_description << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool print_help() {
|
||||
std::cout << std::endl;
|
||||
maketitle("DropShell version " + VERSION);
|
||||
@ -32,11 +42,10 @@ bool print_help() {
|
||||
std::cout << "A tool for managing server configurations" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "dropshell ..." << std::endl;
|
||||
std::cout << " help Show this help message" << std::endl;
|
||||
std::cout << " edit Edit the configuration of dropshell" << std::endl;
|
||||
show_command("help");
|
||||
show_command("edit");
|
||||
|
||||
if (gConfig().is_config_set()) {
|
||||
std::cout << " server NAME Show details for specific server" << std::endl;
|
||||
std::cout << " templates List all available templates" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
@ -59,19 +68,14 @@ bool print_help() {
|
||||
std::cout << " create-service SERVER TEMPLATE SERVICE" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << " edit Edit the configuration of dropshell" << std::endl;
|
||||
show_command("help");
|
||||
show_command("edit");
|
||||
std::cout << std::endl;
|
||||
std::cout << "Other commands available once initialised." << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int die(const std::string & msg) {
|
||||
std::cerr << msg << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct ServerAndServices {
|
||||
std::string server_name;
|
||||
std::vector<LocalServiceInfo> servicelist;
|
||||
@ -91,12 +95,6 @@ bool getCLIServices(const std::string & arg2, const std::string & arg3,
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string safearg(int argc, char *argv[], int index)
|
||||
{
|
||||
if (index >= argc) return "";
|
||||
return argv[index];
|
||||
}
|
||||
|
||||
void printversion() {
|
||||
maketitle("DropShell version " + VERSION);
|
||||
std::cout << "Release date: " << RELEASE_DATE << std::endl;
|
||||
@ -128,23 +126,6 @@ auto command_match = [](const std::string& cmd_list, int argc, char* argv[]) ->
|
||||
} \
|
||||
}
|
||||
|
||||
int edit_config()
|
||||
{
|
||||
if (!gConfig().is_config_set())
|
||||
gConfig().save_config(false); // save defaults.
|
||||
|
||||
std::string config_file = localfile::dropshell_json();
|
||||
if (!service_runner::edit_file(config_file) || !std::filesystem::exists(config_file))
|
||||
return die("Error: Failed to edit config file.");
|
||||
|
||||
gConfig().load_config();
|
||||
if (!gConfig().is_config_set())
|
||||
return die("Error: Failed to load and parse edited config file!");
|
||||
|
||||
gConfig().save_config(true);
|
||||
std::cout << "Successfully edited config file at " << config_file << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2)))
|
||||
@ -173,8 +154,6 @@ int main(int argc, char* argv[]) {
|
||||
return autocomplete::autocomplete(argvec) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (cmd == "edit" && argc < 3)
|
||||
return edit_config();
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// from here we require the config file to be loaded.
|
||||
@ -231,11 +210,6 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "edit" && argc < 4) {
|
||||
ASSERT(argc>=3, "Error: logic error!");
|
||||
service_runner::edit_server(safearg(argc,argv,2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// handle running a command.
|
||||
std::set<std::string> commands;
|
||||
|
Reference in New Issue
Block a user