Tidy config file format.
This commit is contained in:
parent
cf8a7db01d
commit
7cd9c0dd03
@ -1,11 +1,14 @@
|
|||||||
#include "autocomplete.hpp"
|
#include "autocomplete.hpp"
|
||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
#include "templates.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void dropshell::autocomplete(const std::vector<std::string> &args)
|
void dropshell::autocomplete(const std::vector<std::string> &args)
|
||||||
{
|
{
|
||||||
|
auto cfg = dropshell::get_global_config();
|
||||||
// std::cerr << "[ "<<args.size()<<" ] : ";
|
// std::cerr << "[ "<<args.size()<<" ] : ";
|
||||||
// for (const auto& arg : args)
|
// for (const auto& arg : args)
|
||||||
// std::cerr << arg << " ";
|
// std::cerr << arg << " ";
|
||||||
@ -13,6 +16,49 @@ void dropshell::autocomplete(const std::vector<std::string> &args)
|
|||||||
|
|
||||||
if (args.size() < 3) // dropshell autocomplete ???
|
if (args.size() < 3) // dropshell autocomplete ???
|
||||||
autocomplete_list_commands();
|
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()
|
void dropshell::autocomplete_list_commands()
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
#include "utils/directories.hpp"
|
#include "utils/directories.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <boost/filesystem.hpp>
|
|
||||||
#include <boost/property_tree/ptree.hpp>
|
|
||||||
#include <boost/property_tree/ini_parser.hpp>
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "utils/envmanager.hpp"
|
#include "utils/envmanager.hpp"
|
||||||
#include "utils/utils.hpp"
|
#include "utils/utils.hpp"
|
||||||
|
|
||||||
namespace fs = boost::filesystem;
|
#include <filesystem>
|
||||||
namespace pt = boost::property_tree;
|
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
@ -27,7 +23,7 @@ config::~config() {
|
|||||||
|
|
||||||
bool config::load_config() {
|
bool config::load_config() {
|
||||||
std::string config_path = get_local_dropshell_config_path();
|
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;
|
return false;
|
||||||
|
|
||||||
envmanager config_env(config_path);
|
envmanager config_env(config_path);
|
||||||
@ -84,29 +80,32 @@ bool config::add_local_config_directory(const std::string &path)
|
|||||||
if (path.empty())
|
if (path.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Convert to canonical path
|
// Convert to canonical path, using std::filesystem
|
||||||
fs::path abs_path = fs::canonical(path);
|
std::filesystem::path abs_path = std::filesystem::canonical(path);
|
||||||
|
|
||||||
// The directory must exist
|
// 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;
|
std::cerr << "Error: The local config directory does not exist: " << abs_path.string() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to config paths if not already there
|
// Add to config paths if not already there
|
||||||
std::string path_str = abs_path.string();
|
for (auto &p : mLocalConfigPaths)
|
||||||
if (std::find(mLocalConfigPaths.begin(), mLocalConfigPaths.end(), path_str) == mLocalConfigPaths.end()) {
|
{ // robustly compare the two paths.
|
||||||
mLocalConfigPaths.push_back(path_str);
|
if (p == abs_path.string())
|
||||||
return true;
|
{
|
||||||
}
|
std::cerr << "Warning: The local config directory is already registered: " << abs_path.string() << std::endl;
|
||||||
|
|
||||||
if (mLocalBackupPath.empty())
|
|
||||||
mLocalBackupPath = path_str + "/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;
|
std::cerr << "No changes made to the DropShell configuration." << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
mLocalConfigPaths.push_back(abs_path.string());
|
||||||
|
|
||||||
|
if (mLocalBackupPath.empty())
|
||||||
|
mLocalBackupPath = abs_path.string() + "/backups";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const std::string &config::get_local_backup_path() const
|
const std::string &config::get_local_backup_path() const
|
||||||
{
|
{
|
||||||
|
@ -22,6 +22,7 @@ int init(const std::vector<std::string> &args)
|
|||||||
try {
|
try {
|
||||||
if (!cfg->add_local_config_directory(args[2]))
|
if (!cfg->add_local_config_directory(args[2]))
|
||||||
return 1; // error already reported
|
return 1; // error already reported
|
||||||
|
|
||||||
cfg->save_config();
|
cfg->save_config();
|
||||||
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
|
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());
|
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
|
||||||
|
@ -77,9 +77,7 @@ std::string multi2string(std::vector<std::string> values)
|
|||||||
std::string result;
|
std::string result;
|
||||||
for (const auto& value : values) {
|
for (const auto& value : values) {
|
||||||
// remove any " contained in the string value, if present
|
// remove any " contained in the string value, if present
|
||||||
std::string quoteless_value = value;
|
result += dequote(trim(value)) + ",";
|
||||||
quoteless_value.erase(std::remove(quoteless_value.begin(), quoteless_value.end(), '"'), quoteless_value.end());
|
|
||||||
result += "\"" + trim(quoteless_value) + "\",";
|
|
||||||
}
|
}
|
||||||
if (!result.empty())
|
if (!result.empty())
|
||||||
result.pop_back(); // Remove the last comma
|
result.pop_back(); // Remove the last comma
|
||||||
@ -91,6 +89,8 @@ std::vector<std::string> string2multi(std::string values)
|
|||||||
{
|
{
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
|
|
||||||
|
values = dequote(trim(values));
|
||||||
|
|
||||||
// Return values separated by commas, but ignore commas within quotes
|
// Return values separated by commas, but ignore commas within quotes
|
||||||
bool inside_quotes = false;
|
bool inside_quotes = false;
|
||||||
std::string current_item;
|
std::string current_item;
|
||||||
@ -100,14 +100,9 @@ std::vector<std::string> string2multi(std::string values)
|
|||||||
inside_quotes = !inside_quotes;
|
inside_quotes = !inside_quotes;
|
||||||
} else if (c == ',' && !inside_quotes) {
|
} else if (c == ',' && !inside_quotes) {
|
||||||
if (!current_item.empty()) {
|
if (!current_item.empty()) {
|
||||||
// Remove quotes if present
|
std::string final = dequote(trim(current_item));
|
||||||
if (current_item.front() == '"' && current_item.back() == '"') {
|
if (!final.empty())
|
||||||
current_item = current_item.substr(1, current_item.length() - 2);
|
|
||||||
}
|
|
||||||
std::string final = trim(current_item);
|
|
||||||
if (!final.empty()) {
|
|
||||||
result.push_back(final);
|
result.push_back(final);
|
||||||
}
|
|
||||||
current_item.clear();
|
current_item.clear();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -117,15 +112,10 @@ std::vector<std::string> string2multi(std::string values)
|
|||||||
|
|
||||||
// Add the last item if not empty
|
// Add the last item if not empty
|
||||||
if (!current_item.empty()) {
|
if (!current_item.empty()) {
|
||||||
// Remove quotes if present
|
std::string final = dequote(trim(current_item));
|
||||||
if (current_item.front() == '"' && current_item.back() == '"') {
|
if (!final.empty())
|
||||||
current_item = current_item.substr(1, current_item.length() - 2);
|
|
||||||
}
|
|
||||||
std::string final = trim(current_item);
|
|
||||||
if (!final.empty()) {
|
|
||||||
result.push_back(final);
|
result.push_back(final);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user