Tidy config file format.

This commit is contained in:
Your Name 2025-04-26 09:51:44 +12:00
parent cf8a7db01d
commit 7cd9c0dd03
4 changed files with 74 additions and 38 deletions

View File

@ -1,11 +1,14 @@
#include "autocomplete.hpp"
#include "servers.hpp"
#include "config.hpp"
#include "templates.hpp"
#include <algorithm>
#include <iostream>
void dropshell::autocomplete(const std::vector<std::string> &args)
{
auto cfg = dropshell::get_global_config();
// std::cerr << "[ "<<args.size()<<" ] : ";
// for (const auto& arg : args)
// std::cerr << arg << " ";
@ -13,6 +16,49 @@ void dropshell::autocomplete(const std::vector<std::string> &args)
if (args.size() < 3) // dropshell autocomplete ???
autocomplete_list_commands();
std::string cmd = args[2];
std::string noargcmds[] = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"};
if (std::find(std::begin(noargcmds), std::end(noargcmds), cmd) != std::end(noargcmds))
return;
if (!cfg->is_config_set())
return; // can't help without working config.
if (args.size()==3) // we have the command but nothing else. dropshell autocomplete command <server>
{
auto servers = dropshell::get_configured_servers();
for (const auto& server : servers)
std::cout << server.name << std::endl;
return;
}
if (args.size()==4) // we have the command and the server. dropshell autocomplete command server <service>
{
std::string server = args[3];
if (cmd=="create-service")
{ // create-service <server> <template> <service>
std::vector<template_info> templates;
get_templates(templates);
for (const auto& t : templates)
std::cout << t.name << std::endl;
return;
}
auto services = dropshell::get_server_services_info(server);
for (const auto& service : services)
std::cout << service.service_name << std::endl;
return;
}
if (args.size()==5) // we have the command and the server and the service. dropshell autocomplete command server service_name <command?>
{
return; // no more autocompletion possible - don't know what the argument is for.
}
return;
}
void dropshell::autocomplete_list_commands()

View File

@ -1,15 +1,11 @@
#include "utils/directories.hpp"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "config.hpp"
#include "utils/envmanager.hpp"
#include "utils/utils.hpp"
namespace fs = boost::filesystem;
namespace pt = boost::property_tree;
#include <filesystem>
namespace dropshell {
@ -27,7 +23,7 @@ config::~config() {
bool config::load_config() {
std::string config_path = get_local_dropshell_config_path();
if (config_path.empty() || !fs::exists(config_path))
if (config_path.empty() || !std::filesystem::exists(config_path))
return false;
envmanager config_env(config_path);
@ -84,28 +80,31 @@ bool config::add_local_config_directory(const std::string &path)
if (path.empty())
return false;
// Convert to canonical path
fs::path abs_path = fs::canonical(path);
// Convert to canonical path, using std::filesystem
std::filesystem::path abs_path = std::filesystem::canonical(path);
// The directory must exist
if (!fs::exists(abs_path)) {
if (!std::filesystem::exists(abs_path)) {
std::cerr << "Error: The local config directory does not exist: " << abs_path.string() << std::endl;
return false;
}
// Add to config paths if not already there
std::string path_str = abs_path.string();
if (std::find(mLocalConfigPaths.begin(), mLocalConfigPaths.end(), path_str) == mLocalConfigPaths.end()) {
mLocalConfigPaths.push_back(path_str);
return true;
for (auto &p : mLocalConfigPaths)
{ // robustly compare the two paths.
if (p == abs_path.string())
{
std::cerr << "Warning: The local config directory is already registered: " << abs_path.string() << std::endl;
std::cerr << "No changes made to the DropShell configuration." << std::endl;
return false;
}
}
mLocalConfigPaths.push_back(abs_path.string());
if (mLocalBackupPath.empty())
mLocalBackupPath = path_str + "/backups";
mLocalBackupPath = abs_path.string() + "/backups";
std::cerr << "Warning: The local config directory is already registered: " << path_str << std::endl;
std::cerr << "No changes made to the DropShell configuration." << std::endl;
return false;
return true;
}
const std::string &config::get_local_backup_path() const

View File

@ -22,9 +22,10 @@ int init(const std::vector<std::string> &args)
try {
if (!cfg->add_local_config_directory(args[2]))
return 1; // error already reported
cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
if (cfg->get_local_config_directories().size() ==1)
std::cout << "DropShell is now initialised and you can add a server with 'dropshell create-server <server-name>'" << std::endl;

View File

@ -77,9 +77,7 @@ std::string multi2string(std::vector<std::string> values)
std::string result;
for (const auto& value : values) {
// remove any " contained in the string value, if present
std::string quoteless_value = value;
quoteless_value.erase(std::remove(quoteless_value.begin(), quoteless_value.end(), '"'), quoteless_value.end());
result += "\"" + trim(quoteless_value) + "\",";
result += dequote(trim(value)) + ",";
}
if (!result.empty())
result.pop_back(); // Remove the last comma
@ -91,6 +89,8 @@ std::vector<std::string> string2multi(std::string values)
{
std::vector<std::string> result;
values = dequote(trim(values));
// Return values separated by commas, but ignore commas within quotes
bool inside_quotes = false;
std::string current_item;
@ -100,14 +100,9 @@ std::vector<std::string> string2multi(std::string values)
inside_quotes = !inside_quotes;
} else if (c == ',' && !inside_quotes) {
if (!current_item.empty()) {
// Remove quotes if present
if (current_item.front() == '"' && current_item.back() == '"') {
current_item = current_item.substr(1, current_item.length() - 2);
}
std::string final = trim(current_item);
if (!final.empty()) {
std::string final = dequote(trim(current_item));
if (!final.empty())
result.push_back(final);
}
current_item.clear();
}
} else {
@ -117,14 +112,9 @@ std::vector<std::string> string2multi(std::string values)
// Add the last item if not empty
if (!current_item.empty()) {
// Remove quotes if present
if (current_item.front() == '"' && current_item.back() == '"') {
current_item = current_item.substr(1, current_item.length() - 2);
}
std::string final = trim(current_item);
if (!final.empty()) {
std::string final = dequote(trim(current_item));
if (!final.empty())
result.push_back(final);
}
}
return result;