179 lines
5.9 KiB
C++
179 lines
5.9 KiB
C++
#include "command_registry.hpp"
|
|
#include "config.hpp"
|
|
#include "utils/utils.hpp"
|
|
#include "utils/directories.hpp"
|
|
#include "shared_commands.hpp"
|
|
#include "version.hpp"
|
|
|
|
#include <unistd.h>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <filesystem>
|
|
#include "utils/assert.hpp"
|
|
|
|
namespace dropshell {
|
|
|
|
void help_autocomplete(const CommandContext& ctx);
|
|
int help_handler(const CommandContext& ctx);
|
|
|
|
static std::vector<std::string> help_name_list={"help","h","--help","-h"};
|
|
|
|
// Static registration
|
|
struct HelpCommandRegister {
|
|
HelpCommandRegister() {
|
|
CommandRegistry::instance().register_command({
|
|
help_name_list,
|
|
help_handler,
|
|
help_autocomplete,
|
|
false, // hidden
|
|
false, // requires_config
|
|
false, // requires_install
|
|
0, // min_args (after command)
|
|
1, // max_args (after command)
|
|
"help [COMMAND]",
|
|
"Show help for dropshell, or detailed help for a specific command.",
|
|
// heredoc
|
|
R"(
|
|
Show help for dropshell, or detailed help for a specific command.
|
|
If you want to see documentation, please visit the DropShell website:
|
|
https://dropshell.app
|
|
)"
|
|
});
|
|
}
|
|
} help_command_register;
|
|
|
|
|
|
void help_autocomplete(const CommandContext& ctx) {
|
|
if (ctx.args.size() == 1) {
|
|
// list all commands
|
|
for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) {
|
|
std::cout << cmd << std::endl;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void show_command(const std::string& cmd) {
|
|
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
|
if (!cmd_info)
|
|
std::cout << "Unknown command: " << cmd << std::endl;
|
|
|
|
std::cout << " ";
|
|
print_left_aligned(cmd_info->help_usage, 30);
|
|
std::cout << cmd_info->help_description << std::endl;
|
|
}
|
|
|
|
extern const std::string VERSION;
|
|
extern const std::string RELEASE_DATE;
|
|
extern const std::string AUTHOR;
|
|
extern const std::string LICENSE;
|
|
|
|
|
|
int show_command_help(const std::string& cmd) {
|
|
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
|
if (!cmd_info)
|
|
{
|
|
std::cout << "Unknown command: " << cmd << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
std::cout << "Usage: " << std::endl;
|
|
std::cout << " ";
|
|
print_left_aligned(cmd_info->help_usage, 30);
|
|
std::cout << cmd_info->help_description << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << " Equivalent names: ";
|
|
bool first = true;
|
|
for (const auto& name : cmd_info->names) {
|
|
if (!first) std::cout << ", ";
|
|
std::cout << name;
|
|
first = false;
|
|
}
|
|
std::cout << std::endl << std::endl;
|
|
|
|
std::cout << cmd_info->full_help << std::endl << std::endl;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int help_handler(const CommandContext& ctx) {
|
|
|
|
if (ctx.args.size() > 0)
|
|
return show_command_help(ctx.args[0]);
|
|
|
|
std::cout << std::endl;
|
|
maketitle("DropShell version " + VERSION);
|
|
std::cout << std::endl;
|
|
std::cout << "A tool for managing remote servers, by " << AUTHOR << std::endl;
|
|
std::cout << std::endl;
|
|
std::cout << "dropshell ..." << std::endl;
|
|
|
|
show_command("help");
|
|
show_command("edit");
|
|
|
|
if (gConfig().is_config_set())
|
|
{
|
|
// show more!
|
|
show_command("list");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// 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);
|
|
// std::cout << std::endl;
|
|
// std::cout << "A tool for managing server configurations" << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << "dropshell ..." << std::endl;
|
|
// show_command("help");
|
|
// show_command("edit");
|
|
|
|
// if (gConfig().is_config_set()) {
|
|
// std::cout << " templates List all available templates" << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl;
|
|
// std::cout << " list [SERVER] [SERVICE] List status/details of all servers/server/service." << std::endl;
|
|
// std::cout << " edit [SERVER] [SERVICE] Edit the configuration of dropshell/server/service." << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Safe/non-destructive." << std::endl;
|
|
// std::cout << " uninstall SERVER [SERVICE] Uninstalls the service on the remote server. Leaves data intact." << std::endl;
|
|
// std::cout << " nuke SERVER SERVICE Nuke the service, deleting ALL local and remote data." << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s), e.g." << std::endl;
|
|
// std::cout << " backup, restore, start, stop, logs" << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << " ssh SERVER SERVICE Launch an interactive shell on a server or service" << std::endl;
|
|
// std::cout << std::endl;
|
|
// std::cout << "Creation commands: (apply to the first local config directory)"<<std::endl;
|
|
// std::cout << " create-template TEMPLATE" << std::endl;
|
|
// std::cout << " create-server SERVER" << std::endl;
|
|
// std::cout << " create-service SERVER TEMPLATE SERVICE" << std::endl;
|
|
// }
|
|
// else {
|
|
// show_command("help");
|
|
// show_command("edit");
|
|
// std::cout << std::endl;
|
|
// std::cout << "Other commands available once initialised." << std::endl;
|
|
// }
|
|
// return true;
|
|
// }
|
|
|
|
|
|
|
|
} // namespace dropshell
|