Tidy
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
#include "servers.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "config.hpp"
|
||||
#include "templates.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
@ -19,7 +19,6 @@ void print_help(const boost::program_options::options_description& desc);
|
||||
void print_version();
|
||||
void check_status();
|
||||
void list_servers();
|
||||
void list_templates();
|
||||
void show_server_details(const std::string& server_name);
|
||||
void interactive_mode();
|
||||
|
||||
|
@ -64,23 +64,7 @@ void list_servers() {
|
||||
for (const auto& service : services) {
|
||||
service_runner ss;
|
||||
if (ss.init(server.name, service.service_name))
|
||||
{
|
||||
switch (ss.is_healthy())
|
||||
{
|
||||
case service_runner::HealthStatus::HEALTHY:
|
||||
serviceticks += ":tick: ";
|
||||
break;
|
||||
case service_runner::HealthStatus::UNHEALTHY:
|
||||
serviceticks += ":cross: ";
|
||||
break;
|
||||
case service_runner::HealthStatus::NOTINSTALLED:
|
||||
serviceticks += ":warning: ";
|
||||
break;
|
||||
case service_runner::HealthStatus::ERROR:
|
||||
serviceticks += ":error: ";
|
||||
break;
|
||||
}
|
||||
}
|
||||
serviceticks += ss.healthmark() + " ";
|
||||
else std::cout<<"Error: Failed to initialise service runner for server: ["<<server.name<<"] and service: ["<<service.service_name<<"]"<<std::endl;
|
||||
std::vector<int> ports = ss.get_ports();
|
||||
ports_used.insert(ports_used.end(), ports.begin(), ports.end());
|
||||
@ -156,21 +140,7 @@ void show_server_details(const std::string& server_name) {
|
||||
service_runner ss;
|
||||
if (ss.init(server_name, service.service_name))
|
||||
{
|
||||
switch (ss.is_healthy())
|
||||
{
|
||||
case service_runner::HealthStatus::HEALTHY:
|
||||
healthy = ":check:";
|
||||
break;
|
||||
case service_runner::HealthStatus::UNHEALTHY:
|
||||
healthy = ":cross:";
|
||||
break;
|
||||
case service_runner::HealthStatus::NOTINSTALLED:
|
||||
healthy = ":warning:";
|
||||
break;
|
||||
default:
|
||||
healthy = ":error:";
|
||||
break;
|
||||
}
|
||||
healthy = ss.healthmark();
|
||||
ports = ss.get_ports();
|
||||
}
|
||||
bool first = true;
|
||||
|
@ -100,9 +100,8 @@ bool service_runner::install() {
|
||||
}
|
||||
|
||||
// Check if template exists
|
||||
template_manager tm;
|
||||
template_info tinfo;
|
||||
if (!tm.get_template_info(m_service_info.template_name, tinfo)) {
|
||||
if (!get_template_info(m_service_info.template_name, tinfo)) {
|
||||
std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@ -164,6 +163,11 @@ bool service_runner::run_command(const std::string& command) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!template_command_exists(m_service_info.template_name, command)) {
|
||||
std::cout << "No command script for " << m_service_info.template_name << " : " << command << std::endl;
|
||||
return true; // nothing to run.
|
||||
}
|
||||
|
||||
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
// Check if service directory exists
|
||||
@ -195,7 +199,13 @@ bool service_runner::backup() {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string script_path = mRemote_service_template_path + "/_backup.sh";
|
||||
std::string command = "_backup";
|
||||
|
||||
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
|
||||
if (!template_command_exists(m_service_info.template_name, command)) {
|
||||
std::cout << "No backup script for " << m_service_info.template_name << std::endl;
|
||||
return true; // nothing to back up.
|
||||
}
|
||||
|
||||
// Check if basic installed stuff is in place.
|
||||
if (!check_remote_dir_exists(mRemote_service_path) || !check_remote_file_exists(script_path) || !check_remote_file_exists(mRemote_service_env_file))
|
||||
@ -252,7 +262,13 @@ service_runner::HealthStatus service_runner::is_healthy()
|
||||
}
|
||||
|
||||
// Check if status script exists
|
||||
std::string script_path = mRemote_service_template_path + "/_status.sh";
|
||||
std::string command = "_status";
|
||||
|
||||
if (!template_command_exists(m_service_info.template_name, command)) {
|
||||
return HealthStatus::UNKNOWN;
|
||||
}
|
||||
|
||||
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
|
||||
if (!check_remote_file_exists(script_path)) {
|
||||
return HealthStatus::NOTINSTALLED;
|
||||
}
|
||||
@ -270,16 +286,34 @@ std::string service_runner::healthtick()
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
std::string yellow_exclamation = "\033[33m!\033[0m";
|
||||
|
||||
std::string unknown = "\033[33m?\033[0m";
|
||||
|
||||
HealthStatus status = is_healthy();
|
||||
if (status == HealthStatus::HEALTHY)
|
||||
return green_tick;
|
||||
else if (status == HealthStatus::UNHEALTHY)
|
||||
return red_cross;
|
||||
else if (status == HealthStatus::UNKNOWN)
|
||||
return unknown;
|
||||
else
|
||||
return yellow_exclamation;
|
||||
}
|
||||
|
||||
std::string service_runner::healthmark()
|
||||
{
|
||||
HealthStatus status = is_healthy();
|
||||
if (status == HealthStatus::HEALTHY)
|
||||
return ":tick:";
|
||||
else if (status == HealthStatus::UNHEALTHY)
|
||||
return ":cross:";
|
||||
else if (status == HealthStatus::UNKNOWN)
|
||||
return ":question:";
|
||||
else if (status == HealthStatus::NOTINSTALLED)
|
||||
return ":warning:";
|
||||
else
|
||||
return ":error:";
|
||||
}
|
||||
|
||||
std::vector<int> service_runner::get_ports()
|
||||
{
|
||||
std::vector<int> ports;
|
||||
|
@ -51,7 +51,8 @@ class service_runner {
|
||||
HEALTHY,
|
||||
UNHEALTHY,
|
||||
NOTINSTALLED,
|
||||
ERROR
|
||||
ERROR,
|
||||
UNKNOWN
|
||||
};
|
||||
HealthStatus is_healthy();
|
||||
|
||||
@ -61,7 +62,7 @@ class service_runner {
|
||||
std::vector<int> get_ports();
|
||||
|
||||
std::string healthtick();
|
||||
|
||||
std::string healthmark();
|
||||
private:
|
||||
std::string m_server_name;
|
||||
ServiceInfo m_service_info;
|
||||
|
@ -67,8 +67,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string &
|
||||
}
|
||||
|
||||
template_info tinfo;
|
||||
template_manager tm;
|
||||
if (!tm.get_template_info(service.template_name, tinfo)) {
|
||||
if (!get_template_info(service.template_name, tinfo)) {
|
||||
std::cerr << "Error: Template '" << service.template_name << "' not found" << std::endl;
|
||||
return ServiceInfo();
|
||||
}
|
||||
|
@ -35,7 +35,8 @@ const std::map<std::string, coloredText> kReplacements = {
|
||||
{":info:", {"i", kTextColor_Blue}},
|
||||
{":check:", {"+", kTextColor_Green}},
|
||||
{":x:", {"x", kTextColor_Red}},
|
||||
{":error:", {"!", kTextColor_Red}}
|
||||
{":error:", {"!", kTextColor_Red}},
|
||||
{":question:", {"?", kTextColor_DarkGrey}}
|
||||
};
|
||||
|
||||
// Helper function to get ANSI color code
|
||||
|
@ -10,15 +10,8 @@
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
template_manager::template_manager() {
|
||||
// Constructor implementation
|
||||
}
|
||||
|
||||
template_manager::~template_manager() {
|
||||
// Destructor implementation
|
||||
}
|
||||
|
||||
bool template_manager::get_templates(std::vector<template_info>& templates) {
|
||||
bool get_templates(std::vector<template_info>& templates) {
|
||||
templates.clear();
|
||||
|
||||
// Helper function to add templates from a directory
|
||||
@ -34,10 +27,13 @@ bool template_manager::get_templates(std::vector<template_info>& templates) {
|
||||
info.path = entry.path().string();
|
||||
|
||||
// Check if template with same name already exists
|
||||
bool found = false;
|
||||
auto it = std::find_if(templates.begin(), templates.end(),
|
||||
[&info](const template_info& t) { return t.name == info.name; });
|
||||
|
||||
if (it == templates.end()) {
|
||||
found = (it!=templates.end());
|
||||
found |= info.name=="example"; // don't include the example template!
|
||||
|
||||
if (!found) {
|
||||
templates.push_back(info);
|
||||
}
|
||||
}
|
||||
@ -50,7 +46,7 @@ bool template_manager::get_templates(std::vector<template_info>& templates) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool template_manager::get_template_info(const std::string& name, template_info& info) {
|
||||
bool get_template_info(const std::string& name, template_info& info) {
|
||||
std::vector<template_info> templates;
|
||||
if (!get_templates(templates)) {
|
||||
return false;
|
||||
@ -67,13 +63,21 @@ bool template_manager::get_template_info(const std::string& name, template_info&
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool template_command_exists(const std::string &template_name, const std::string &command)
|
||||
{
|
||||
template_info info;
|
||||
if (!get_template_info(template_name, info)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string path = info.path + "/" + command + ".sh";
|
||||
return (std::filesystem::exists(path));
|
||||
}
|
||||
|
||||
void list_templates() {
|
||||
template_manager tm;
|
||||
std::vector<template_info> templates;
|
||||
|
||||
if (!tm.get_templates(templates)) {
|
||||
if (!get_templates(templates)) {
|
||||
std::cerr << "Error: Failed to get templates" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
@ -17,13 +17,11 @@ class template_info {
|
||||
// if a template exists in both locations, the one in the user directory takes precedence.
|
||||
// the template name is just the subfolder name in the templates directory.
|
||||
// the template path is the path of that subfolder.
|
||||
class template_manager {
|
||||
public:
|
||||
template_manager();
|
||||
~template_manager();
|
||||
|
||||
bool get_templates(std::vector<template_info>& templates);
|
||||
bool get_template_info(const std::string& name, template_info& info);
|
||||
};
|
||||
|
||||
bool get_templates(std::vector<template_info>& templates);
|
||||
bool get_template_info(const std::string& name, template_info& info);
|
||||
bool template_command_exists(const std::string& template_name,const std::string& command);
|
||||
void list_templates();
|
||||
|
||||
} // namespace dropshell
|
||||
|
Reference in New Issue
Block a user